E-MailRelay

gdatetime.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file gdatetime.cpp
19///
20
21#include "gdef.h"
22#include "gdatetime.h"
23#include "gstr.h"
24#include "gassert.h"
25#include <sstream>
26#include <iomanip>
27#include <utility>
28#include <vector>
29#include <type_traits>
30
31 namespace G
32{
33 namespace DateTimeImp
34 {
35 static constexpr const char * good_format = "%ntYyCGgmUWVjdwuHIMSDFRT" ;
36 static constexpr unsigned int million = 1000000U ;
37
38 template <typename Tp> TimeInterval interval( Tp start , Tp end )
39 {
40 using namespace std::chrono ;
41 if( end <= start )
42 return TimeInterval::zero() ;
43 auto d = end - start ;
44
45 auto s = (duration_cast<seconds>(d)).count() ; G_ASSERT( s >= 0 ) ;
46 typename std::make_unsigned<decltype(s)>::type su = s ;
47 if( sizeof(su) > sizeof(TimeInterval::s_type) &&
48 su > std::numeric_limits<TimeInterval::s_type>::max() )
49 return TimeInterval::limit() ;
50
51 auto us = (duration_cast<microseconds>(d) % seconds(1)).count() ; G_ASSERT( us >= 0 ) ;
52 typename std::make_unsigned<decltype(us)>::type usu = us ;
53 G_ASSERT( us <= std::numeric_limits<TimeInterval::us_type>::max() ) ;
54
55 return TimeInterval( static_cast<TimeInterval::s_type>(su) , static_cast<TimeInterval::us_type>(usu) ) ;
56 }
57 }
58}
59
60 G::BrokenDownTime::BrokenDownTime() :
61 m_tm{}
62{
63 m_tm.tm_isdst = -1 ;
64}
65
66 G::BrokenDownTime::BrokenDownTime( const struct std::tm & tm_in ) :
67 m_tm(tm_in)
68{
69 m_tm.tm_isdst = -1 ;
70 if( std::time_t(-1) == std::mktime( &m_tm ) ) // set wday and yday
71 throw DateTime::Error() ;
72}
73
74std::time_t G::BrokenDownTime::epochTimeFromLocal() const
75{
76 struct std::tm tm = m_tm ;
77 tm.tm_isdst = -1 ;
78 std::time_t t = std::mktime( &tm ) ;
79 if( t == std::time_t(-1) )
80 throw DateTime::Error() ;
81 return t ;
82}
83
84std::time_t G::BrokenDownTime::epochTimeFromUtc() const
85{
86 BrokenDownTime copy( *this ) ;
87 copy.m_tm.tm_sec = 0 ; // simplify for leap-seconds, add back below
88 return copy.epochTimeFromUtcImp() + m_tm.tm_sec ;
89}
90
91std::time_t G::BrokenDownTime::epochTimeFromUtcImp() const
92{
93 static bool diff_set = false ;
94 static std::time_t diff = 0 ;
95 return epochTimeFromUtcImp( diff_set , diff ) ;
96}
97
98std::time_t G::BrokenDownTime::epochTimeFromUtcImp( bool & diff_set , std::time_t & diff ) const
99{
100 constexpr int day = 24 * 60 * 60 ;
101 constexpr std::time_t dt = 60 * 15 ; // india 30mins, nepal 45mins // NOLINT
102 constexpr std::time_t day_and_a_bit = day + dt ;
103 constexpr std::time_t t_rounding = 30 ;
104
105 // use mktime() for a rough starting point
106 std::time_t t_base = epochTimeFromLocal() ;
107
108 // see if the previous diff result is still valid (no change of dst etc),
109 if( diff_set && !sameMinute( SystemTime(t_base+diff+t_rounding).utc() ) )
110 diff_set = false ;
111
112 // find the timezone diff
113 if( !diff_set )
114 {
115 // iterate over all possible timezones modifying the epoch time until
116 // its utc broken-down-time (gmtime()) matches ours
117 //
118 auto t = t_base - day_and_a_bit ;
119 auto end = t_base + day_and_a_bit ;
120 for( diff = -day_and_a_bit ; t <= end ; t += dt , diff += dt )
121 {
122 if( sameMinute( SystemTime(t+t_rounding).utc() ) )
123 {
124 diff_set = true ;
125 break ;
126 }
127 }
128 }
129
130 if( !diff_set )
131 throw DateTime::Error( "unsupported timezone" ) ;
132
133 return t_base + diff ;
134}
135
136 G::BrokenDownTime G::BrokenDownTime::local( SystemTime t )
137{
138 BrokenDownTime tm ;
139 std::time_t s = t.s() ;
140 if( localtime_r( &s , &tm.m_tm ) == nullptr )
141 throw DateTime::Error() ;
142 return tm ;
143}
144
145 G::BrokenDownTime G::BrokenDownTime::utc( SystemTime t )
146{
147 BrokenDownTime tm ;
148 std::time_t s = t.s() ;
149 if( gmtime_r( &s , &tm.m_tm ) == nullptr )
150 throw DateTime::Error() ;
151 return tm ;
152}
153
154 G::BrokenDownTime::BrokenDownTime( int y , int mon , int d , int h , int min , int sec ) :
155 m_tm{}
156{
157 m_tm.tm_year = y - 1900 ;
158 m_tm.tm_mon = mon - 1 ;
159 m_tm.tm_mday = d ;
160 m_tm.tm_hour = h ;
161 m_tm.tm_min = min ;
162 m_tm.tm_sec = sec ;
163 m_tm.tm_isdst = -1 ;
164 if( std::time_t(-1) == std::mktime( &m_tm ) ) // set wday and yday
165 throw DateTime::Error() ;
166}
167
168 G::BrokenDownTime G::BrokenDownTime::midday( int year , int month , int day )
169{
170 return { year , month , day , 12 , 0 , 0 } ;
171}
172
173 void G::BrokenDownTime::format( std::vector<char> & out , const char * fmt ) const
174{
175 for( const char * p = std::strchr(fmt,'%') ; p && p[1] ; p = std::strchr(p+1,'%') )
176 {
177 if( !std::strchr(DateTimeImp::good_format,p[1]) )
178 throw DateTime::Error() ;
179 }
180
181 if( std::strftime( &out[0] , out.size() , fmt , &m_tm ) == 0U )
182 throw DateTime::Error() ;
183}
184
185std::string G::BrokenDownTime::str() const
186{
187 return str( "%F %T" ) ;
188}
189
190std::string G::BrokenDownTime::str( const char * fmt ) const
191{
192 std::size_t n = std::strlen( fmt ) ;
193 for( const char * p = std::strchr(fmt,'%') ; p && p[1] ; p = std::strchr(p+1,'%') )
194 n += 10U ; // biggest allowed format is eg. %F -> "2001-12-31"
195
196 std::vector<char> buffer( n ) ;
197 format( buffer , fmt ) ;
198 buffer.at(buffer.size()-1U) = '\0' ; // just in case
199 return std::string( &buffer[0] ) ;
200}
201
202 int G::BrokenDownTime::hour() const
203{
204 return m_tm.tm_hour ;
205}
206
207 int G::BrokenDownTime::min() const
208{
209 return m_tm.tm_min ;
210}
211
212 int G::BrokenDownTime::sec() const
213{
214 return m_tm.tm_sec ;
215}
216
217 int G::BrokenDownTime::year() const
218{
219 return m_tm.tm_year + 1900 ;
220}
221
222 int G::BrokenDownTime::month() const
223{
224 return m_tm.tm_mon + 1 ;
225}
226
227 int G::BrokenDownTime::day() const
228{
229 return m_tm.tm_mday ;
230}
231
232 int G::BrokenDownTime::wday() const
233{
234 return m_tm.tm_wday ;
235}
236
237 bool G::BrokenDownTime::sameMinute( const BrokenDownTime & other ) const
238{
239 return
240 year() == other.year() &&
241 month() == other.month() &&
242 day() == other.day() &&
243 hour() == other.hour() &&
244 min() == other.min() ;
245}
246
247// ==
248
249 G::SystemTime::SystemTime( time_point_type tp ) :
250 m_tp(tp)
251{
252}
253
254 G::SystemTime::SystemTime( std::time_t t , unsigned long us ) noexcept
255{
256 m_tp = std::chrono::system_clock::from_time_t(t) ;
257 m_tp += std::chrono::microseconds( us ) ;
258}
259
260 G::SystemTime G::SystemTime::now()
261{
262 return SystemTime( std::chrono::system_clock::now() ) ;
263}
264
265 G::TimeInterval G::SystemTime::operator-( const SystemTime & start ) const
266{
267 return start.interval( *this ) ;
268}
269
270 G::TimeInterval G::SystemTime::interval( const SystemTime & end ) const
271{
272 return DateTimeImp::interval( m_tp , end.m_tp ) ;
273}
274
275 G::SystemTime & G::SystemTime::add( unsigned long us )
276{
277 m_tp += std::chrono::microseconds( us ) ;
278 return *this ;
279}
280
281 bool G::SystemTime::sameSecond( const SystemTime & t ) const noexcept
282{
283 return s() == t.s() ;
284}
285
286 G::BrokenDownTime G::SystemTime::local() const
287{
288 return BrokenDownTime::local( *this ) ;
289}
290
291 G::BrokenDownTime G::SystemTime::utc() const
292{
293 return BrokenDownTime::utc( *this ) ;
294}
295
296 unsigned int G::SystemTime::ms() const
297{
298 using namespace std::chrono ;
299 return static_cast<unsigned int>((duration_cast<milliseconds>(m_tp.time_since_epoch()) % seconds(1)).count()) ;
300}
301
302 unsigned int G::SystemTime::us() const
303{
304 using namespace std::chrono ;
305 return static_cast<unsigned int>((duration_cast<microseconds>(m_tp.time_since_epoch()) % seconds(1)).count()) ;
306}
307
308std::time_t G::SystemTime::s() const noexcept
309{
310 using namespace std::chrono ;
311 G_ASSERT( duration_cast<seconds>(m_tp.time_since_epoch()).count() == system_clock::to_time_t(m_tp) ) ; // as per c++17
312 return system_clock::to_time_t( m_tp ) ;
313}
314
315 G::SystemTime G::SystemTime::zero()
316{
317 duration_type zero{0} ;
318 G_ASSERT( SystemTime(time_point_type(zero)).s() == 0 ) ; // assert 1970 epoch as per c++17
319 return SystemTime( time_point_type(zero) ) ;
320}
321
322 bool G::SystemTime::operator<( const SystemTime & other ) const
323{
324 return m_tp < other.m_tp ;
325}
326
327 bool G::SystemTime::operator<=( const SystemTime & other ) const
328{
329 return m_tp <= other.m_tp ;
330}
331
332 bool G::SystemTime::operator==( const SystemTime & other ) const
333{
334 return m_tp == other.m_tp ;
335}
336
337 bool G::SystemTime::operator!=( const SystemTime & other ) const
338{
339 return !( *this == other ) ;
340}
341
342 bool G::SystemTime::operator>( const SystemTime & other ) const
343{
344 return m_tp > other.m_tp ;
345}
346
347 bool G::SystemTime::operator>=( const SystemTime & other ) const
348{
349 return m_tp >= other.m_tp ;
350}
351
352 G::SystemTime G::SystemTime::operator+( TimeInterval interval ) const
353{
354 SystemTime t( *this ) ;
355 t += interval ;
356 return t ;
357}
358
359 void G::SystemTime::operator+=( TimeInterval i )
360{
361 using namespace std::chrono ;
362 m_tp += seconds(i.s()) ;
363 m_tp += microseconds(i.us()) ;
364}
365
366 void G::SystemTime::streamOut( std::ostream & stream ) const
367{
368 int w = static_cast<int>( stream.width() ) ;
369 char c = stream.fill() ;
370 stream
371 << s() << "."
372 << std::setw(6) << std::setfill('0')
373 << us()
374 << std::setw(w) << std::setfill(c) ;
375}
376
377std::ostream & G::operator<<( std::ostream & stream , const SystemTime & t )
378{
379 t.streamOut( stream ) ;
380 return stream ;
381}
382
383// ==
384
385 G::TimerTime G::TimerTime::now()
386{
387 return TimerTime( std::chrono::steady_clock::now() , false ) ;
388}
389
390 G::TimerTime G::TimerTime::zero()
391{
392 duration_type zero_duration{0} ;
393 return TimerTime( time_point_type(zero_duration) , true ) ;
394}
395
396 G::TimerTime G::TimerTime::test( int s , int us )
397{
398 return TimerTime( time_point_type(std::chrono::seconds(s)+std::chrono::microseconds(us)) , s==0 && us==0 ) ;
399}
400
401G::TimerTime::TimerTime( time_point_type tp , bool is_zero ) :
402 m_is_zero(is_zero) ,
403 m_tp(tp)
404{
405}
406
407 unsigned long G::TimerTime::test_s() const
408{
409 using namespace std::chrono ;
410 return static_cast<unsigned long>(duration_cast<seconds>(m_tp.time_since_epoch()).count()) ;
411}
412
413 unsigned long G::TimerTime::test_us() const
414{
415 using namespace std::chrono ;
416 return static_cast<unsigned long>((duration_cast<microseconds>(m_tp.time_since_epoch()) % seconds(1)).count()) ;
417}
418
419 G::TimerTime G::TimerTime::operator+( const TimeInterval & interval ) const
420{
421 TimerTime t( *this ) ;
422 t += interval ;
423 t.m_is_zero = m_is_zero && interval.s() == 0U && interval.us() == 0U ;
424 return t ;
425}
426
427 void G::TimerTime::operator+=( TimeInterval i )
428{
429 using namespace std::chrono ;
430 m_tp += seconds(i.s()) ;
431 m_tp += microseconds(i.us()) ;
432 m_is_zero = m_is_zero && i.s() == 0U && i.us() == 0U ;
433}
434
435 G::TimeInterval G::TimerTime::operator-( const TimerTime & start ) const
436{
437 return start.interval( *this ) ;
438}
439
440 G::TimeInterval G::TimerTime::interval( const TimerTime & end ) const
441{
442 return DateTimeImp::interval( m_tp , end.m_tp ) ;
443}
444
445 bool G::TimerTime::sameSecond( const TimerTime & t ) const
446{
447 using namespace std::chrono ;
448 return
449 duration_cast<seconds>(m_tp.time_since_epoch()) ==
450 duration_cast<seconds>(t.m_tp.time_since_epoch()) ;
451}
452
453 bool G::TimerTime::operator<( const TimerTime & other ) const
454{
455 return m_tp < other.m_tp ;
456}
457
458 bool G::TimerTime::operator<=( const TimerTime & other ) const
459{
460 return m_tp <= other.m_tp ;
461}
462
463 bool G::TimerTime::operator==( const TimerTime & other ) const
464{
465 return m_tp == other.m_tp ;
466}
467
468 bool G::TimerTime::operator!=( const TimerTime & other ) const
469{
470 return m_tp != other.m_tp ;
471}
472
473 bool G::TimerTime::operator>( const TimerTime & other ) const
474{
475 return m_tp > other.m_tp ;
476}
477
478 bool G::TimerTime::operator>=( const TimerTime & other ) const
479{
480 return m_tp >= other.m_tp ;
481}
482
483// ==
484
485 G::TimeInterval::TimeInterval( s_type s , us_type us ) :
486 m_s(s) ,
487 m_us(us)
488{
489 normalise() ;
490}
491
492 G::TimeInterval::TimeInterval( const SystemTime & start , const SystemTime & end ) :
493 m_s(0) ,
494 m_us(0)
495{
496 TimeInterval i = start.interval( end ) ;
497 m_s = i.m_s ;
498 m_us = i.m_us ;
499 normalise() ;
500}
501
502 G::TimeInterval::TimeInterval( const TimerTime & start , const TimerTime & end ) :
503 m_s(0) ,
504 m_us(0)
505{
506 TimeInterval i = start.interval( end ) ;
507 m_s = i.m_s ;
508 m_us = i.m_us ;
509 normalise() ;
510}
511
512 void G::TimeInterval::normalise()
513{
514 using namespace G::DateTimeImp ;
515 if( m_us >= million )
516 {
517 m_us -= million ;
518 increase( m_s ) ;
519 if( m_us >= million ) // still
520 {
521 increase( m_s , m_us / million ) ;
522 m_us = m_us % million ;
523 }
524 }
525}
526
527 G::TimeInterval G::TimeInterval::limit()
528{
529 using namespace G::DateTimeImp ;
530 return TimeInterval( std::numeric_limits<s_type>::max() , million-1U ) ;
531}
532
533 G::TimeInterval G::TimeInterval::zero()
534{
535 return TimeInterval( 0UL , 0U ) ;
536}
537
538G::TimeInterval::s_type G::TimeInterval::s() const
539{
540 return m_s ;
541}
542
543G::TimeInterval::us_type G::TimeInterval::us() const
544{
545 return m_us ;
546}
547
548 bool G::TimeInterval::operator==( const TimeInterval & other ) const
549{
550 return m_s == other.m_s && m_us == other.m_us ;
551}
552
553 bool G::TimeInterval::operator!=( const TimeInterval & other ) const
554{
555 return !( *this == other ) ;
556}
557
558 bool G::TimeInterval::operator<( const TimeInterval & other ) const
559{
560 return m_s < other.m_s || ( m_s == other.m_s && m_us < other.m_us ) ;
561}
562
563 bool G::TimeInterval::operator<=( const TimeInterval & other ) const
564{
565 return *this == other || *this < other ;
566}
567
568 bool G::TimeInterval::operator>( const TimeInterval & other ) const
569{
570 return m_s > other.m_s || ( m_s == other.m_s && m_us > other.m_us ) ;
571}
572
573 bool G::TimeInterval::operator>=( const TimeInterval & other ) const
574{
575 return *this == other || *this > other ;
576}
577
578 G::TimeInterval G::TimeInterval::operator+( const TimeInterval & other ) const
579{
580 TimeInterval t( *this ) ;
581 t += other ;
582 return t ;
583}
584
585 G::TimeInterval G::TimeInterval::operator-( const TimeInterval & other ) const
586{
587 TimeInterval t( *this ) ;
588 t -= other ;
589 return t ;
590}
591
592 void G::TimeInterval::increase( unsigned int & s , unsigned int ds )
593{
594 const auto old = s ;
595 s += ds ;
596 const bool overflow = s < old ;
597 if( overflow )
598 throw DateTime::Error( "overflow" ) ;
599}
600
601 void G::TimeInterval::operator+=( TimeInterval i )
602{
603 using namespace G::DateTimeImp ;
604 m_us += i.m_us ;
605 if( m_us >= million )
606 {
607 m_us -= million ;
608 increase( m_s ) ;
609 }
610 increase( m_s , i.m_s ) ;
611}
612
613 void G::TimeInterval::decrease( unsigned int & s , unsigned int ds )
614{
615 if( s < ds )
616 throw DateTime::Error( "underflow" ) ;
617 s -= ds ;
618}
619
620 void G::TimeInterval::operator-=( TimeInterval i )
621{
622 using namespace G::DateTimeImp ;
623 if( m_us < i.m_us )
624 {
625 decrease( m_s ) ;
626 m_us += million ;
627 }
628 m_us -= i.m_us ;
629 decrease( m_s , i.m_s ) ;
630}
631
632 void G::TimeInterval::streamOut( std::ostream & stream ) const
633{
634 int w = static_cast<int>( stream.width() ) ;
635 char c = stream.fill() ;
636 stream
637 << s() << "."
638 << std::setw(6) << std::setfill('0')
639 << us()
640 << std::setw(w) << std::setfill(c) ;
641}
642
643std::ostream & G::operator<<( std::ostream & stream , const TimeInterval & ti )
644{
645 ti.streamOut( stream ) ;
646 return stream ;
647}
648
649// ==
650
651G::DateTime::Offset G::DateTime::offset( SystemTime t_in )
652{
653 G_ASSERT( !(t_in == SystemTime::zero()) ) ;
654 SystemTime t_zone( BrokenDownTime::local(t_in).epochTimeFromUtc() ) ;
655 bool ahead = t_in < t_zone ; // ie. east-of
656 TimeInterval i = ahead ? (t_zone-t_in) : (t_in-t_zone) ;
657 return Offset{ ahead , i.s() } ;
658}
659
660std::string G::DateTime::offsetString( int tz )
661{
662 std::ostringstream ss ;
663 ss << ( tz < 0 ? "-" : "+" ) ;
664 if( tz < 0 ) tz = -tz ;
665 ss << (tz/10) << (tz%10) << "00" ;
666 return ss.str() ;
667}
668
669std::string G::DateTime::offsetString( Offset offset )
670{
671 unsigned int hh = (offset.second+30U) / 3600U ;
672 unsigned int mm = ((offset.second+30U) / 60U) % 60 ;
673
674 std::ostringstream ss ;
675 char sign = (offset.first || (hh==0&&mm==0)) ? '+' : '-' ;
676 ss << sign << (hh/10U) << (hh%10U) << (mm/10) << (mm%10) ;
677 return ss.str() ;
678}
679
G::BrokenDownTime
An encapsulation of 'struct std::tm'.
Definition: gdatetime.h:45
G::BrokenDownTime::wday
int wday() const
Returns week day where sunday=0 and saturday=6.
Definition: gdatetime.cpp:232
G::BrokenDownTime::day
int day() const
Returns the 1..31 month-day value.
Definition: gdatetime.cpp:227
G::BrokenDownTime::epochTimeFromLocal
std::time_t epochTimeFromLocal() const
Uses std::mktime() to convert this locale-dependent local broken-down time into epoch time.
Definition: gdatetime.cpp:74
G::BrokenDownTime::sameMinute
bool sameMinute(const BrokenDownTime &other) const
Returns true if this and the other broken-down times are the same, at minute resolution with no round...
Definition: gdatetime.cpp:237
G::BrokenDownTime::str
std::string str() const
Returns str() using a "%F %T" format.
Definition: gdatetime.cpp:185
G::BrokenDownTime::BrokenDownTime
BrokenDownTime(const struct std::tm &)
Constructor.
Definition: gdatetime.cpp:66
G::BrokenDownTime::sec
int sec() const
Returns the 0..59 or 0..60 seconds value.
Definition: gdatetime.cpp:212
G::BrokenDownTime::utc
static BrokenDownTime utc(SystemTime)
Factory function for the utc time of the given epoch time.
Definition: gdatetime.cpp:145
G::BrokenDownTime::midday
static BrokenDownTime midday(int year, int month, int day)
Factory function for midday on the given date.
Definition: gdatetime.cpp:168
G::BrokenDownTime::month
int month() const
Returns the 1..12 month value.
Definition: gdatetime.cpp:222
G::BrokenDownTime::format
void format(std::vector< char > &out, const char *fmt) const
Puts the formatted date, including a terminating null character, into the given buffer.
Definition: gdatetime.cpp:173
G::BrokenDownTime::local
static BrokenDownTime local(SystemTime)
Factory function for the locale-dependent local time of the given epoch time.
Definition: gdatetime.cpp:136
G::BrokenDownTime::hour
int hour() const
Returns the 0..23 hour value.
Definition: gdatetime.cpp:202
G::BrokenDownTime::year
int year() const
Returns the four-digit year value.
Definition: gdatetime.cpp:217
G::BrokenDownTime::epochTimeFromUtc
std::time_t epochTimeFromUtc() const
Searches std::mktime() over the range of all timezones to convert this utc broken-down time into epoc...
Definition: gdatetime.cpp:84
G::BrokenDownTime::min
int min() const
Returns the 0..59 minute value.
Definition: gdatetime.cpp:207
G::DateTime::offset
static Offset offset(SystemTime)
Returns the offset in seconds between UTC and localtime as at the given system time.
Definition: gdatetime.cpp:651
G::DateTime::offsetString
static std::string offsetString(Offset offset)
Converts the given utc/localtime offset into a five-character "+/-hhmm" string.
Definition: gdatetime.cpp:669
G::SystemTime
Represents a unix-epoch time with microsecond resolution.
Definition: gdatetime.h:125
G::SystemTime::operator-
TimeInterval operator-(const SystemTime &start) const
Returns the given start time's interval() compared to this end time.
Definition: gdatetime.cpp:265
G::SystemTime::now
static SystemTime now()
Factory function for the current time.
Definition: gdatetime.cpp:260
G::SystemTime::s
std::time_t s() const noexcept
Returns the number of seconds since the start of the epoch.
Definition: gdatetime.cpp:308
G::SystemTime::operator+
SystemTime operator+(TimeInterval) const
Returns this time with given interval added.
Definition: gdatetime.cpp:352
G::SystemTime::ms
unsigned int ms() const
Returns the millisecond fraction.
Definition: gdatetime.cpp:296
G::SystemTime::us
unsigned int us() const
Returns the microsecond fraction.
Definition: gdatetime.cpp:302
G::SystemTime::operator!=
bool operator!=(const SystemTime &) const
Comparison operator.
Definition: gdatetime.cpp:337
G::SystemTime::local
BrokenDownTime local() const
Returns the locale-dependent local broken-down time.
Definition: gdatetime.cpp:286
G::SystemTime::operator>
bool operator>(const SystemTime &) const
Comparison operator.
Definition: gdatetime.cpp:342
G::SystemTime::streamOut
void streamOut(std::ostream &) const
Streams out the time comprised of the s() value, a decimal point, and then the six-digit us() value.
Definition: gdatetime.cpp:366
G::SystemTime::interval
TimeInterval interval(const SystemTime &end) const
Returns the interval between this time and the given end time.
Definition: gdatetime.cpp:270
G::SystemTime::sameSecond
bool sameSecond(const SystemTime &other) const noexcept
Returns true if this time and the other time are the same, at second resolution.
Definition: gdatetime.cpp:281
G::SystemTime::operator<=
bool operator<=(const SystemTime &) const
Comparison operator.
Definition: gdatetime.cpp:327
G::SystemTime::operator>=
bool operator>=(const SystemTime &) const
Comparison operator.
Definition: gdatetime.cpp:347
G::SystemTime::zero
static SystemTime zero()
Factory function for the start of the epoch.
Definition: gdatetime.cpp:315
G::SystemTime::SystemTime
SystemTime(std::time_t, unsigned long us=0UL) noexcept
Constructor.
Definition: gdatetime.cpp:254
G::SystemTime::utc
BrokenDownTime utc() const
Returns the utc broken-down time.
Definition: gdatetime.cpp:291
G::SystemTime::operator+=
void operator+=(TimeInterval)
Adds the given interval. Throws on overflow.
Definition: gdatetime.cpp:359
G::SystemTime::operator<
bool operator<(const SystemTime &) const
Comparison operator.
Definition: gdatetime.cpp:322
G::SystemTime::operator==
bool operator==(const SystemTime &) const
Comparison operator.
Definition: gdatetime.cpp:332
G::TimeInterval
An interval between two G::SystemTime values or two G::TimerTime values.
Definition: gdatetime.h:289
G::TimeInterval::TimeInterval
TimeInterval(unsigned int s, unsigned int us=0U)
Constructor.
Definition: gdatetime.cpp:485
G::TimeInterval::operator+
TimeInterval operator+(const TimeInterval &) const
Returns the combined interval. Throws on overflow.
Definition: gdatetime.cpp:578
G::TimeInterval::zero
static TimeInterval zero()
Factory function for the zero interval.
Definition: gdatetime.cpp:533
G::TimeInterval::operator-=
void operator-=(TimeInterval)
Subtracts the given interval. Throws on underflow.
Definition: gdatetime.cpp:620
G::TimeInterval::operator==
bool operator==(const TimeInterval &) const
Comparison operator.
Definition: gdatetime.cpp:548
G::TimeInterval::limit
static TimeInterval limit()
Factory function for the maximum valid interval.
Definition: gdatetime.cpp:527
G::TimeInterval::s
unsigned int s() const
Returns the number of seconds.
Definition: gdatetime.cpp:538
G::TimeInterval::us
unsigned int us() const
Returns the fractional microseconds part.
Definition: gdatetime.cpp:543
G::TimeInterval::operator+=
void operator+=(TimeInterval)
Adds the given interval. Throws on overflow.
Definition: gdatetime.cpp:601
G::TimeInterval::operator>
bool operator>(const TimeInterval &) const
Comparison operator.
Definition: gdatetime.cpp:568
G::TimeInterval::operator-
TimeInterval operator-(const TimeInterval &) const
Returns the interval difference. Throws on underflow.
Definition: gdatetime.cpp:585
G::TimeInterval::operator<
bool operator<(const TimeInterval &) const
Comparison operator.
Definition: gdatetime.cpp:558
G::TimeInterval::operator!=
bool operator!=(const TimeInterval &) const
Comparison operator.
Definition: gdatetime.cpp:553
G::TimeInterval::streamOut
void streamOut(std::ostream &) const
Streams out the interval.
Definition: gdatetime.cpp:632
G::TimeInterval::operator>=
bool operator>=(const TimeInterval &) const
Comparison operator.
Definition: gdatetime.cpp:573
G::TimeInterval::operator<=
bool operator<=(const TimeInterval &) const
Comparison operator.
Definition: gdatetime.cpp:563
G::TimerTime
A monotonically increasing subsecond-resolution timestamp, notionally unrelated to time_t.
Definition: gdatetime.h:213
G::TimerTime::now
static TimerTime now()
Factory function for the current steady-clock time.
Definition: gdatetime.cpp:385
G::TimerTime::operator+
TimerTime operator+(const TimeInterval &) const
Returns this time with given interval added.
Definition: gdatetime.cpp:419
G::TimerTime::operator>
bool operator>(const TimerTime &) const
Comparison operator.
Definition: gdatetime.cpp:473
G::TimerTime::interval
TimeInterval interval(const TimerTime &end) const
Returns the interval between this time and the given end time.
Definition: gdatetime.cpp:440
G::TimerTime::operator==
bool operator==(const TimerTime &) const
Comparison operator.
Definition: gdatetime.cpp:463
G::TimerTime::operator!=
bool operator!=(const TimerTime &) const
Comparison operator.
Definition: gdatetime.cpp:468
G::TimerTime::operator-
TimeInterval operator-(const TimerTime &start) const
Returns the given start time's interval() compared to this end time.
Definition: gdatetime.cpp:435
G::TimerTime::sameSecond
bool sameSecond(const TimerTime &other) const
Returns true if this time and the other time are the same, at second resolution.
Definition: gdatetime.cpp:445
G::TimerTime::operator+=
void operator+=(TimeInterval)
Adds an interval.
Definition: gdatetime.cpp:427
G::TimerTime::zero
static TimerTime zero()
Factory function for the start of the epoch.
Definition: gdatetime.cpp:390
G::TimerTime::operator<
bool operator<(const TimerTime &) const
Comparison operator.
Definition: gdatetime.cpp:453
G::TimerTime::operator<=
bool operator<=(const TimerTime &) const
Comparison operator.
Definition: gdatetime.cpp:458
G::TimerTime::operator>=
bool operator>=(const TimerTime &) const
Comparison operator.
Definition: gdatetime.cpp:478
G::format
A simple version of boost::format for formatting strings in an i18n-friendly way.
Definition: gformat.h:46
gassert.h
gdatetime.h
gstr.h
G
Low-level classes.
Definition: galign.h:28
G::BrokenDownTime::m_tm
Definition: gdatetime.h:118