E-MailRelay
gdatetime.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2018 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.h
19 ///
20 
21 #ifndef G_DATE_TIME_H
22 #define G_DATE_TIME_H
23 
24 #include "gdef.h"
25 #include "gexception.h"
26 #include <ctime>
27 #include <string>
28 #include <iostream>
29 
30 namespace G
31 {
32  class DateTime ;
33  class EpochTime ;
34 }
35 
36 /// \class G::EpochTime
37 /// A subsecond-resolution timestamp based on a time_t.
38 ///
40 {
41 public:
42  explicit EpochTime( std::time_t ) ;
43  ///< Constructor.
44 
45  EpochTime( std::time_t , unsigned long us ) ;
46  ///< Constructor. The 'us' parameter can be more than 10^6.
47 
48  void streamOut( std::ostream & ) const ;
49  ///< Used by operator<<.
50 
51 public:
52  std::time_t s ;
53  unsigned int us ;
54 } ;
55 
56 namespace G
57 {
58  EpochTime operator+( EpochTime et , std::time_t s ) ;
59  EpochTime operator+( EpochTime lhs , EpochTime rhs ) ;
60  EpochTime operator-( EpochTime big , EpochTime small_ ) ;
61  bool operator<( EpochTime lhs , EpochTime rhs ) ;
62  bool operator==( EpochTime lhs , EpochTime rhs ) ;
63  bool operator!=( EpochTime lhs , EpochTime rhs ) ;
64  bool operator<=( EpochTime lhs , EpochTime rhs ) ;
65  bool operator>=( EpochTime lhs , EpochTime rhs ) ;
66  bool operator>( EpochTime lhs , EpochTime rhs ) ;
67  std::ostream & operator<<( std::ostream & s , const EpochTime & et ) ;
68 }
69 
70 /// \class G::DateTime
71 /// A low-level static class used by Date and Time.
72 ///
74 {
75 public:
76  G_EXCEPTION( Error , "date/time error" ) ;
77  typedef struct std::tm BrokenDownTime ;
78  typedef std::pair<bool,unsigned int> Offset ;
79 
80  static EpochTime now() ;
81  ///< Returns the current epoch time.
82 
83  static EpochTime epochTime( const BrokenDownTime & broken_down_time ) ;
84  ///< Converts from UTC broken-down-time to epoch time.
85 
86  static BrokenDownTime utc( EpochTime epoch_time ) ;
87  ///< Converts from epoch time to UTC broken-down-time.
88 
89  static BrokenDownTime local( EpochTime epoch_time ) ;
90  ///< Converts from epoch time to local broken-down-time.
91 
92  static Offset offset( EpochTime epoch_time ) ;
93  ///< Returns the offset between UTC and localtime as at
94  ///< 'epoch_time'. The returned pair has 'first' set to
95  ///< true if localtime is ahead of (ie. east of) UTC.
96  ///<
97  ///< (Note that this may be a relatively expensive
98  ///< operation.)
99 
100  static std::string offsetString( Offset offset ) ;
101  ///< Converts the given utc/localtime offset into a five-character
102  ///< "+/-hhmm" string.
103  ///< See also RFC-2822.
104 
105  static std::string offsetString( int hh ) ;
106  ///< Overload for a signed integer timezone.
107 
108 private:
109  static bool equivalent( EpochTime , const BrokenDownTime & ) ;
110  static bool equivalent( const BrokenDownTime & , const BrokenDownTime & ) ;
111  static std::tm * gmtime_imp( const std::time_t * , std::tm * ) ;
112  static std::tm * localtime_imp( const std::time_t * , std::tm * ) ;
113  DateTime() ; // not implemented
114 } ;
115 
116 inline
117 G::EpochTime::EpochTime( std::time_t t ) :
118  s(t) ,
119  us(0UL)
120 {
121 }
122 
123 inline
124 G::EpochTime G::operator+( G::EpochTime et , std::time_t s )
125 {
126  et.s += s ;
127  return et ;
128 }
129 
130 inline
131 bool G::operator<( G::EpochTime lhs , G::EpochTime rhs )
132 {
133  return lhs.s < rhs.s || (lhs.s == rhs.s && lhs.us < rhs.us ) ;
134 }
135 
136 inline
137 bool G::operator==( G::EpochTime lhs , G::EpochTime rhs )
138 {
139  return lhs.s == rhs.s && lhs.us == rhs.us ;
140 }
141 
142 inline
143 bool G::operator!=( G::EpochTime lhs , G::EpochTime rhs )
144 {
145  return !(lhs == rhs) ;
146 }
147 
148 inline
149 bool G::operator<=( G::EpochTime lhs , G::EpochTime rhs )
150 {
151  return lhs == rhs || lhs < rhs ;
152 }
153 
154 inline
155 bool G::operator>=( G::EpochTime lhs , G::EpochTime rhs )
156 {
157  return !(lhs < rhs) ;
158 }
159 
160 inline
161 bool G::operator>( G::EpochTime lhs , G::EpochTime rhs )
162 {
163  return lhs >= rhs && lhs != rhs ;
164 }
165 
166 inline
167 std::ostream & G::operator<<( std::ostream & s , const G::EpochTime & t )
168 {
169  t.streamOut( s ) ;
170  return s ;
171 }
172 
173 #endif
static BrokenDownTime utc(EpochTime epoch_time)
Converts from epoch time to UTC broken-down-time.
Definition: gdatetime.cpp:123
A subsecond-resolution timestamp based on a time_t.
Definition: gdatetime.h:39
static Offset offset(EpochTime epoch_time)
Returns the offset between UTC and localtime as at 'epoch_time'.
Definition: gdatetime.cpp:139
static BrokenDownTime local(EpochTime epoch_time)
Converts from epoch time to local broken-down-time.
Definition: gdatetime.cpp:131
static EpochTime now()
Returns the current epoch time.
void streamOut(std::ostream &) const
Used by operator<<.
Definition: gdatetime.cpp:70
Low-level classes.
Definition: garg.h:29
A low-level static class used by Date and Time.
Definition: gdatetime.h:73
static std::string offsetString(Offset offset)
Converts the given utc/localtime offset into a five-character "+/-hhmm" string.
Definition: gdatetime.cpp:158
EpochTime(std::time_t)
Constructor.
Definition: gdatetime.h:117
static EpochTime epochTime(const BrokenDownTime &broken_down_time)
Converts from UTC broken-down-time to epoch time.
Definition: gdatetime.cpp:83