Motiv
Marvelous OTF2 Traces Interactive Visualizer
Loading...
Searching...
No Matches
TimeUnit.cpp
1/*
2 * Marvelous OTF2 Traces Interactive Visualizer (MOTIV)
3 * Copyright (C) 2023 Florian Gallrein, Björn Gehrke
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18#include "TimeUnit.hpp"
19
21
22QString TimeUnit::str() const {
23 switch (this->unit) {
24 case NanoSecond:
25 return "ns";
26 case MicroSecond:
27 return "μs";
28 case MilliSecond:
29 return "ms";
30 case Second:
31 return "s";
32 case Minute:
33 return "m";
34 case Hour:
35 return "h";
36 default:
37 throw std::invalid_argument("Unknown TimeUnit");
38 }
39}
40
41TimeUnit::TimeUnit(QString unit) {
42 std::map<QString, Unit> lut = {
43 {"ns", NanoSecond},
44 {"μs", MicroSecond},
45 {"ms", MilliSecond},
46 {"s", Second},
47 {"m", Minute},
48 {"h", Hour},
49 };
50
51 auto it = lut.find(unit);
52 if (it == lut.end()) {
53 throw std::invalid_argument("Unknown TimeUnit");
54 }
55
56 this->unit = it->second;
57}
58
59double TimeUnit::multiplier() const {
60 switch (this->unit) {
62 return 1;
64 return 1e3;
66 return 1e6;
68 return 1e9;
70 return 60e9;
71 case TimeUnit::Hour:
72 return 60 * 60e9;
73 default:
74 // This should be caught by the constructors
75 __builtin_unreachable();
76 }
77}
@ MicroSecond
Definition: TimeUnit.hpp:32
@ MilliSecond
Definition: TimeUnit.hpp:33
@ NanoSecond
Definition: TimeUnit.hpp:31
TimeUnit(Unit unit)
Constructs or implicitly converts a TimeUnit from a Unit.
Definition: TimeUnit.cpp:20
QString str() const
Definition: TimeUnit.cpp:22
double multiplier() const
Definition: TimeUnit.cpp:59