- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

gdatetime.cpp
Go to the documentation of this file.
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 // gdatetime.cpp
19 //
20
21 #include "gdef.h"
22 #include "gdatetime.h"
23 #include "gstr.h"
24 #include "gassert.h"
25 #include <sstream>
26
27 namespace
28 {
29 const std::time_t minute = 60U ;
30 const std::time_t hour = 60U * minute ;
31 const std::time_t day = 24U * hour ;
32 }
33
34 G::DateTime::EpochTime G::DateTime::now()
35 {
36 return std::time(NULL) ;
37 }
38
39 G::DateTime::EpochTime G::DateTime::epochTime( const BrokenDownTime & bdt_in )
40 {
41 // get a rough starting point
42 BrokenDownTime bdt( bdt_in ) ;
43 EpochTime start = std::mktime( &bdt ) ; // localtime
44
45 // iterate over all timezones -- brute force for now
46 const std::time_t delta = minute * 30U ;
47 for( EpochTime t = (start-day-delta) ; t <= (start+day+delta) ; t += delta )
48 {
49 if( equivalent( t , bdt_in ) )
50 return t ;
51 }
52 throw Error() ;
53 }
54
55 G::DateTime::BrokenDownTime G::DateTime::utc( EpochTime epoch_time )
56 {
57 static BrokenDownTime zero ;
58 BrokenDownTime result = zero ;
59 G::DateTime::gmtime_r( &epoch_time , &result ) ;
60 return result ;
61 }
62
63 G::DateTime::BrokenDownTime G::DateTime::local( EpochTime epoch_time )
64 {
65 static BrokenDownTime zero ;
66 BrokenDownTime bdt_local = zero ;
67 G::DateTime::localtime_r( &epoch_time , &bdt_local ) ;
68 return bdt_local ;
69 }
70
71 G::DateTime::Offset G::DateTime::offset( EpochTime utc )
72 {
73 BrokenDownTime bdt_local = local(utc) ;
74
75 EpochTime local = epochTime(bdt_local) ;
76 bool ahead = local >= utc ; // ie. east-of
77 EpochTime n = ahead ? (local-utc) : (utc-local) ;
78 return Offset( ahead , static_cast<unsigned int>(n) ) ;
79 }
80
81 std::string G::DateTime::offsetString( Offset offset )
82 {
83 unsigned int hh = offset.second / 3600U ;
84 unsigned int mm = (offset.second / 60U) % 60 ;
85
86 std::ostringstream ss ;
87 char sign = (offset.first || (hh==0&&mm==0)) ? '+' : '-' ;
88 ss << sign << (hh/10U) << (hh%10U) << (mm/10) << (mm%10) ;
89 return ss.str() ;
90 }
91
92 bool G::DateTime::equivalent( EpochTime t , const BrokenDownTime & bdt_in )
93 {
94 BrokenDownTime bdt_test = utc(t) ;
95 return same( bdt_test , bdt_in ) ;
96 }
97
98 bool G::DateTime::same( const BrokenDownTime & bdt1 , const BrokenDownTime & bdt2 )
99 {
100 return
101 bdt1.tm_mday == bdt2.tm_mday &&
102 bdt1.tm_hour == bdt2.tm_hour &&
103 bdt1.tm_min == bdt2.tm_min ;
104 }
105
G::DateTime::utc
static BrokenDownTime utc(EpochTime epoch_time)
Converts from epoch time to UTC broken-down-time.
Definition: gdatetime.cpp:55
G::DateTime::now
static EpochTime now()
Returns the current epoch time.
Definition: gdatetime.cpp:34
G::DateTime::EpochTime
std::time_t EpochTime
Definition: gdatetime.h:41
G::DateTime::offset
static Offset offset(EpochTime epoch_time)
Returns the offset between UTC and localtime as at 'epoch_time'.
Definition: gdatetime.cpp:71
G::DateTime::local
static BrokenDownTime local(EpochTime epoch_time)
Converts from epoch time to local broken-down-time.
Definition: gdatetime.cpp:63
gstr.h
gassert.h
G::DateTime::Offset
std::pair< bool, unsigned int > Offset
Definition: gdatetime.h:44
gdatetime.h
G::DateTime::offsetString
static std::string offsetString(Offset offset)
Converts the given utc/localtime offset into a five-character "+/-hhmm" string.
Definition: gdatetime.cpp:81
G::DateTime::BrokenDownTime
struct std::tm BrokenDownTime
Definition: gdatetime.h:43
G::DateTime::epochTime
static EpochTime epochTime(const BrokenDownTime &broken_down_time)
Converts from UTC broken-down-time to epoch time.
Definition: gdatetime.cpp:39