b2api
B2000++ API Reference Manual, VERSION 4.6
 
Loading...
Searching...
No Matches
b2time.H
1//------------------------------------------------------------------------
2// b2time.H --
3//
4//
5// written by Mathias Doreille
6//
7// Copyright (c) 2004-2012 SMR Engineering & Development SA
8// 2502 Bienne, Switzerland
9//
10// All Rights Reserved. Proprietary source code. The contents of
11// this file may not be disclosed to third parties, copied or
12// duplicated in any form, in whole or in part, without the prior
13// written permission of SMR.
14//------------------------------------------------------------------------
15
16#ifndef B2TIME_H_
17#define B2TIME_H_
18
19#include <chrono>
20#include <iomanip>
21#include <ostream>
22#include <sstream>
23#include <string>
24
25#include "b2ppconfig.h"
26
27namespace b2000 {
28
29class Time;
30class DiffTime;
31
32class Time {
33public:
34 Time() {
35 t = std::chrono::high_resolution_clock::now();
36 t_sys = std::chrono::system_clock::now();
37 }
38
39 void reinit() {
40 t = std::chrono::high_resolution_clock::now();
41 t_sys = std::chrono::system_clock::now();
42 }
43
44 Time(const Time& time_) { t = time_.t; }
45
48 std::string time() { return time_formats("%X"); }
49
52 std::string date() { return time_formats("%c"); }
53
57 std::string time_formats(const char* format) const {
58 char time_buffer[256];
59 std::time_t t_c = std::chrono::system_clock::to_time_t(t_sys);
60 strftime(time_buffer, 256, format, std::localtime(&t_c));
61 return time_buffer;
62 }
63
64 friend DiffTime operator-(const Time& t1, const Time& t2);
65
66private:
67 friend class DiffTime;
68 std::chrono::time_point<std::chrono::high_resolution_clock> t;
69 std::chrono::time_point<std::chrono::system_clock> t_sys;
70};
71
72class DiffTime {
73public:
74 DiffTime() { t = std::chrono::duration<double>(0.0); }
75
76 DiffTime(const DiffTime& dt) : t(dt.t) {}
77
78 bool operator<(const DiffTime& dt) const { return t < dt.t; }
79
82 std::string time() {
83 long tt = long(std::chrono::floor<std::chrono::seconds>(t).count());
84 int sec = int(tt % 60);
85 tt /= 60;
86 int min = tt % 60;
87 tt /= 60;
88 std::ostringstream o;
89 if (tt > 0) { o << tt << "h"; }
90 o << min << "m" << sec << "s";
91 return o.str();
92 }
93
94 friend DiffTime operator-(const Time& t1, const Time& t2) {
95 DiffTime tmp;
96 tmp.t = std::chrono::duration_cast<std::chrono::duration<double>>(t1.t - t2.t);
97 return tmp;
98 }
99
100 operator double() { return t.count(); }
101
102private:
103 std::chrono::duration<double> t;
104};
105
106} // namespace b2000
107
108#endif // B2TIME_H_
bool operator<(const csda< T1 > &a, const csda< T2 > &b)
Comparison of two csda numbers is performed on the real part only.
Definition b2csda.H:262
Contains the base classes for implementing Finite Elements.
Definition b2boundary_condition.H:32