1 //
2 // Copyright (C) 2001-2013 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 
29 /// \namespace G
30 namespace G
31 {
32 	class DateTime ;
33 }
34 
35 /// \class G::DateTime
36 /// A low-level static class used by Date and Time.
37 ///
38 class G::DateTime
39 {
40 public:
41 	G_EXCEPTION( Error , "date/time error" ) ;
42 	typedef std::time_t EpochTime ;
43 	typedef struct std::tm BrokenDownTime ;
44 	typedef std::pair<bool,unsigned int> Offset ;
45 
46 	static EpochTime now() ;
47 		///< Returns the current epoch time.
48 
49 	static EpochTime epochTime( const BrokenDownTime & broken_down_time ) ;
50 		///< Converts from UTC broken-down-time to epoch time.
51 
52 	static BrokenDownTime utc( EpochTime epoch_time ) ;
53 		///< Converts from epoch time to UTC broken-down-time.
54 
55 	static BrokenDownTime local( EpochTime epoch_time ) ;
56 		///< Converts from epoch time to local broken-down-time.
57 
58 	static Offset offset( EpochTime epoch_time ) ;
59 		///< Returns the offset between UTC and localtime as at
60 		///< 'epoch_time'. The returned pair has 'first' set to
61 		///< true if localtime is ahead of (ie. east of) UTC.
62 		///<
63 		///< (Note that this may be a relatively expensive
64 		///< operation.)
65 
66 	static std::string offsetString( Offset offset ) ;
67 		///< Converts the given utc/localtime offset into a five-character
68 		///< "+/-hhmm" string.
69 		///< See also RFC2822.
70 
71 private:
72 	static bool equivalent( EpochTime , const BrokenDownTime & ) ;
73 	static bool same( const BrokenDownTime & , const BrokenDownTime & ) ;
74 	static std::tm * gmtime_r( const std::time_t * , std::tm * ) ;
75 	static std::tm * localtime_r( const std::time_t * , std::tm * ) ;
76 	DateTime() ; // not implemented
77 } ;
78 
79 #endif
80