Loading...
Searching...
No Matches
Time.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30*/
31/*
32 * Time.h
33 *
34 * Created on: Aug 31, 2010
35 * Author: mpeura
36 */
37
38#include <ctime>
39#include <string>
40#include <iostream>
41
42#ifndef TIME_H_
43#define TIME_H_
44
45namespace drain {
46
48
53class Time : private tm {
54public:
55
56 inline
57 Time(){
58 reset();
59 };
60
61 inline
62 Time(time_t unixSeconds){
63 reset();
64 setTime(unixSeconds);
65 };
66
67 inline
68 Time(const std::string &time, const std::string &format, bool STRICT=true){
69 reset();
70 setTime(time, format, STRICT);
71 };
72
73 inline
74 ~Time(){};
75
76
78 inline
79 void setTime(){
80 setTime(time(NULL));
81 };
82
84
87 inline
88 void setTime(time_t time){
89 tm *gt = gmtime(&time);
90 *(tm *)this = *gt; // = *gmtime(&time);
91 //delete gt;
92 };
93
95
107 void setTime(const std::string &time, const std::string &format, bool STRICT=true);
108
109 inline
110 void setTime(const tm &time){
111 (tm &)*this = time;
112 };
113
115 /*
116 inline
117 void setLocalTime(const time_t &time){
118 _time = time;
119 this->_tm = *localtime(&_time);
120 };
121 */
122
124 inline
125 const tm & getTm() const {
126 return *this;
127 };
128
130 inline
131 time_t getTime() const { return timegm((tm *)this);};
132
133
135 const std::string & str(const std::string & format = "") const;
136
137 void reset();
138
139 inline
140 void debug(){
141 std::cerr << "H=" << this->tm_hour << ", DST=" << this->tm_isdst << '\n';
142 }
143
144
145protected:
146
147 mutable std::string timeStr;
148
149};
150
151}
152
153#endif /* TIME_H_ */
154
155// Drain
Utility for handling time. Internally, uses tm (C time structure).
Definition Time.h:53
const tm & getTm() const
Sets time to UTC time.
Definition Time.h:125
const std::string & str(const std::string &format="") const
Returns the std::string using formatting as defined by strftime()
Definition Time.cpp:75
void setTime(time_t time)
Sets time to UTC time.
Definition Time.h:88
time_t getTime() const
Returns time in seconds.
Definition Time.h:131
void setTime()
Sets time to current UTC time.
Definition Time.h:79
Definition DataSelector.cpp:1277