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 <stdexcept>
41#include <iostream>
42
43#ifndef TIME_H_
44#define TIME_H_
45
46namespace drain {
47
49
54class Time : private tm {
55public:
56
57 inline
58 Time(){
59 reset();
60 };
61
62 inline
63 Time(time_t unixSeconds){
64 reset();
65 setTime(unixSeconds);
66 };
67
68 inline
69 Time(const std::string &time, const std::string &format, bool STRICT=true){
70 reset();
71 setTime(time, format, STRICT);
72 };
73
74 inline
75 ~Time(){};
76
77
79 inline
80 void setTime(){
81 setTime(time(NULL));
82 };
83
85
88 inline
89 void setTime(time_t time){
90 tm *gt = gmtime(&time);
91 *(tm *)this = *gt; // = *gmtime(&time);
92 //delete gt;
93 };
94
96
108 void setTime(const std::string &time, const std::string &format, bool STRICT=true);
109
110 inline
111 void setTime(const tm &time){
112 (tm &)*this = time;
113 };
114
116 /*
117 inline
118 void setLocalTime(const time_t &time){
119 _time = time;
120 this->_tm = *localtime(&_time);
121 };
122 */
123
125 inline
126 const tm & getTm() const {
127 return *this;
128 };
129
131 inline
132 time_t getTime() const { return timegm((tm *)this);};
133
134
136 const std::string & str(const std::string & format = "") const;
137
138 void reset();
139
140 inline
141 void debug(){
142 std::cerr << "H=" << this->tm_hour << ", DST=" << this->tm_isdst << '\n';
143 }
144
145
146protected:
147
148 mutable std::string timeStr;
149
150};
151
152}
153
154#endif /* TIME_H_ */
155
156// Drain
Utility for handling time. Internally, uses tm (C time structure).
Definition Time.h:54
const tm & getTm() const
Sets time to UTC time.
Definition Time.h:126
const std::string & str(const std::string &format="") const
Returns the std::string using formatting as defined by strftime()
Definition Time.cpp:76
void setTime(time_t time)
Sets time to UTC time.
Definition Time.h:89
time_t getTime() const
Returns time in seconds.
Definition Time.h:132
void setTime()
Sets time to current UTC time.
Definition Time.h:80
Definition DataSelector.cpp:1277