VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gdatetime.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
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 private:
56  friend EpochTime operator+( EpochTime et , std::time_t s ) ;
57  friend EpochTime operator+( EpochTime lhs , EpochTime rhs ) ;
58  friend EpochTime operator-( EpochTime big , EpochTime small_ ) ;
59  friend bool operator<( EpochTime lhs , EpochTime rhs ) ;
60  friend bool operator==( EpochTime lhs , EpochTime rhs ) ;
61 } ;
62 
63 namespace G
64 {
65  EpochTime operator+( EpochTime et , std::time_t s ) ;
66  EpochTime operator+( EpochTime lhs , EpochTime rhs ) ;
67  EpochTime operator-( EpochTime big , EpochTime small_ ) ;
68  bool operator<( EpochTime lhs , EpochTime rhs ) ;
69  bool operator==( EpochTime lhs , EpochTime rhs ) ;
70  bool operator!=( EpochTime lhs , EpochTime rhs ) ;
71  bool operator<=( EpochTime lhs , EpochTime rhs ) ;
72  bool operator>=( EpochTime lhs , EpochTime rhs ) ;
73  bool operator>( EpochTime lhs , EpochTime rhs ) ;
74  std::ostream & operator<<( std::ostream & s , const EpochTime & et ) ;
75 }
76 
77 /// \class G::DateTime
78 /// A low-level static class used by Date and Time.
79 ///
81 {
82 public:
83  G_EXCEPTION( Error , "date/time error" ) ;
84  typedef struct std::tm BrokenDownTime ;
85  typedef std::pair<bool,unsigned int> Offset ;
86 
87  static EpochTime now() ;
88  ///< Returns the current epoch time.
89 
90  static EpochTime epochTime( const BrokenDownTime & broken_down_time ) ;
91  ///< Converts from UTC broken-down-time to epoch time.
92 
93  static BrokenDownTime utc( EpochTime epoch_time ) ;
94  ///< Converts from epoch time to UTC broken-down-time.
95 
96  static BrokenDownTime local( EpochTime epoch_time ) ;
97  ///< Converts from epoch time to local broken-down-time.
98 
99  static Offset offset( EpochTime epoch_time ) ;
100  ///< Returns the offset between UTC and localtime as at
101  ///< 'epoch_time'. The returned pair has 'first' set to
102  ///< true if localtime is ahead of (ie. east of) UTC.
103  ///<
104  ///< (Note that this may be a relatively expensive
105  ///< operation.)
106 
107  static std::string offsetString( Offset offset ) ;
108  ///< Converts the given utc/localtime offset into a five-character
109  ///< "+/-hhmm" string.
110  ///< See also RFC2822.
111 
112  static std::string offsetString( int hh ) ;
113  ///< Overload for a signed integer timezone.
114 
115 private:
116  static bool equivalent( EpochTime , const BrokenDownTime & ) ;
117  static bool equivalent( const BrokenDownTime & , const BrokenDownTime & ) ;
118  static std::tm * gmtime_imp( const std::time_t * , std::tm * ) ;
119  static std::tm * localtime_imp( const std::time_t * , std::tm * ) ;
120  DateTime() ; // not implemented
121 } ;
122 
123 inline
124 G::EpochTime::EpochTime( std::time_t t ) :
125  s(t) ,
126  us(0UL)
127 {
128 }
129 
130 inline
131 G::EpochTime G::operator+( G::EpochTime et , std::time_t s )
132 {
133  et.s += s ;
134  return et ;
135 }
136 
137 inline
138 bool G::operator<( G::EpochTime lhs , G::EpochTime rhs )
139 {
140  return lhs.s < rhs.s || (lhs.s == rhs.s && lhs.us < rhs.us ) ;
141 }
142 
143 inline
144 bool G::operator==( G::EpochTime lhs , G::EpochTime rhs )
145 {
146  return lhs.s == rhs.s && lhs.us == rhs.us ;
147 }
148 
149 inline
150 bool G::operator!=( G::EpochTime lhs , G::EpochTime rhs )
151 {
152  return !(lhs == rhs) ;
153 }
154 
155 inline
156 bool G::operator<=( G::EpochTime lhs , G::EpochTime rhs )
157 {
158  return lhs == rhs || lhs < rhs ;
159 }
160 
161 inline
162 bool G::operator>=( G::EpochTime lhs , G::EpochTime rhs )
163 {
164  return !(lhs < rhs) ;
165 }
166 
167 inline
168 bool G::operator>( G::EpochTime lhs , G::EpochTime rhs )
169 {
170  return lhs >= rhs && lhs != rhs ;
171 }
172 
173 inline
174 std::ostream & G::operator<<( std::ostream & s , const G::EpochTime & t )
175 {
176  t.streamOut( s ) ;
177  return s ;
178 }
179 
180 #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
A low-level static class used by Date and Time.
Definition: gdatetime.h:80
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:124
static EpochTime epochTime(const BrokenDownTime &broken_down_time)
Converts from UTC broken-down-time to epoch time.
Definition: gdatetime.cpp:83