1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html><head><meta http-equiv=
"Content-Type" content=
"text/html;charset=UTF-8">
3 <title>Barry: tzwrapper.cc Source File
</title>
4 <link href=
"doxygen.css" rel=
"stylesheet" type=
"text/css">
5 <link href=
"tabs.css" rel=
"stylesheet" type=
"text/css">
7 <!-- Generated by Doxygen 1.5.6 -->
8 <div class=
"navigation" id=
"top">
11 <li><a href=
"index.html"><span>Main
Page
</span></a></li>
12 <li><a href=
"modules.html"><span>Modules
</span></a></li>
13 <li><a href=
"namespaces.html"><span>Namespaces
</span></a></li>
14 <li><a href=
"classes.html"><span>Classes
</span></a></li>
15 <li class=
"current"><a href=
"files.html"><span>Files
</span></a></li>
16 <li><a href=
"dirs.html"><span>Directories
</span></a></li>
19 <div class=
"navpath"><a class=
"el" href=
"dir_edc0f428e3eefcfdce9c25befd901c57.html">src
</a>
22 <div class=
"contents">
23 <h1>tzwrapper.cc
</h1><a href=
"tzwrapper_8cc.html">Go to the documentation of this file.
</a><div class=
"fragment"><pre class=
"fragment"><a name=
"l00001"></a>00001 <span class=
"comment">///
</span>
24 <a name=
"l00002"></a>00002 <span class=
"comment"></span><span class=
"comment">/// \file tzwrapper.cc
</span>
25 <a name=
"l00003"></a>00003 <span class=
"comment"></span><span class=
"comment">/// Timezone adjustment class, wrapping the TZ environment
</span>
26 <a name=
"l00004"></a>00004 <span class=
"comment"></span><span class=
"comment">/// variable to make struct tm -
> time_t conversions easier.
</span>
27 <a name=
"l00005"></a>00005 <span class=
"comment"></span><span class=
"comment">///
</span>
28 <a name=
"l00006"></a>00006 <span class=
"comment"></span>
29 <a name=
"l00007"></a>00007 <span class=
"comment">/*
</span>
30 <a name=
"l00008"></a>00008 <span class=
"comment"> Copyright (C)
2010-
2011, Chris Frey
<cdfrey@foursquare.net
>, To God be the glory
</span>
31 <a name=
"l00009"></a>00009 <span class=
"comment"> Released to the public domain.
</span>
32 <a name=
"l00010"></a>00010 <span class=
"comment"> Included in Barry and Barrified the namespace July
2010</span>
33 <a name=
"l00011"></a>00011 <span class=
"comment">*/
</span>
34 <a name=
"l00012"></a>00012
35 <a name=
"l00013"></a>00013 <span class=
"preprocessor">#include
"<a class="code
" href="tzwrapper_8h.html
" title="Timezone adjustment class, wrapping the TZ environment variable to make struct tm...
">tzwrapper.h</a>"</span>
36 <a name=
"l00014"></a>00014 <span class=
"preprocessor">#include
<string.h
></span>
37 <a name=
"l00015"></a>00015 <span class=
"preprocessor">#include
<stdio.h
></span>
38 <a name=
"l00016"></a>00016 <span class=
"preprocessor">#include
<string
></span>
39 <a name=
"l00017"></a>00017
40 <a name=
"l00018"></a>00018 <span class=
"keyword">using namespace
</span>std;
41 <a name=
"l00019"></a>00019
42 <a name=
"l00020"></a>00020 <span class=
"keyword">namespace
</span>Barry {
<span class=
"keyword">namespace
</span>Sync {
43 <a name=
"l00021"></a>00021
44 <a name=
"l00022"></a>00022 time_t utc_mktime(
<span class=
"keyword">struct
</span> tm *utctime)
45 <a name=
"l00023"></a>00023 {
46 <a name=
"l00024"></a>00024 time_t result;
47 <a name=
"l00025"></a>00025 <span class=
"keyword">struct
</span>tm tmp, check;
48 <a name=
"l00026"></a>00026
49 <a name=
"l00027"></a>00027 <span class=
"comment">// loop, converting
"local time" to time_t and back to utc tm,
</span>
50 <a name=
"l00028"></a>00028 <span class=
"comment">// and adjusting until there are no differences... this
</span>
51 <a name=
"l00029"></a>00029 <span class=
"comment">// automatically takes care of DST issues.
</span>
52 <a name=
"l00030"></a>00030
53 <a name=
"l00031"></a>00031 <span class=
"comment">// do first conversion
</span>
54 <a name=
"l00032"></a>00032 tmp = *utctime;
55 <a name=
"l00033"></a>00033 tmp.tm_isdst = -
1;
56 <a name=
"l00034"></a>00034 result = mktime(
&tmp);
57 <a name=
"l00035"></a>00035 <span class=
"keywordflow">if
</span>( result == (time_t)-
1 )
58 <a name=
"l00036"></a>00036 <span class=
"keywordflow">return
</span> (time_t)-
1;
59 <a name=
"l00037"></a>00037 <span class=
"keywordflow">if
</span>( gmtime_r(
&result,
&check) == NULL )
60 <a name=
"l00038"></a>00038 <span class=
"keywordflow">return
</span> (time_t)-
1;
61 <a name=
"l00039"></a>00039
62 <a name=
"l00040"></a>00040 <span class=
"comment">// loop until match
</span>
63 <a name=
"l00041"></a>00041 <span class=
"keywordflow">while
</span>( check.tm_year != utctime-
>tm_year ||
64 <a name=
"l00042"></a>00042 check.tm_mon != utctime-
>tm_mon ||
65 <a name=
"l00043"></a>00043 check.tm_mday != utctime-
>tm_mday ||
66 <a name=
"l00044"></a>00044 check.tm_hour != utctime-
>tm_hour ||
67 <a name=
"l00045"></a>00045 check.tm_min != utctime-
>tm_min )
68 <a name=
"l00046"></a>00046 {
69 <a name=
"l00047"></a>00047 tmp.tm_min += utctime-
>tm_min - check.tm_min;
70 <a name=
"l00048"></a>00048 tmp.tm_hour += utctime-
>tm_hour - check.tm_hour;
71 <a name=
"l00049"></a>00049 tmp.tm_mday += utctime-
>tm_mday - check.tm_mday;
72 <a name=
"l00050"></a>00050 tmp.tm_year += utctime-
>tm_year - check.tm_year;
73 <a name=
"l00051"></a>00051 tmp.tm_isdst = -
1;
74 <a name=
"l00052"></a>00052
75 <a name=
"l00053"></a>00053 result = mktime(
&tmp);
76 <a name=
"l00054"></a>00054 <span class=
"keywordflow">if
</span>( result == (time_t)-
1 )
77 <a name=
"l00055"></a>00055 <span class=
"keywordflow">return
</span> (time_t)-
1;
78 <a name=
"l00056"></a>00056 gmtime_r(
&result,
&check);
79 <a name=
"l00057"></a>00057 <span class=
"keywordflow">if
</span>( gmtime_r(
&result,
&check) == NULL )
80 <a name=
"l00058"></a>00058 <span class=
"keywordflow">return
</span> (time_t)-
1;
81 <a name=
"l00059"></a>00059 }
82 <a name=
"l00060"></a>00060
83 <a name=
"l00061"></a>00061 <span class=
"keywordflow">return
</span> result;
84 <a name=
"l00062"></a>00062 }
85 <a name=
"l00063"></a>00063
86 <a name=
"l00064"></a>00064 <span class=
"keyword">struct
</span>tm* iso_to_tm(
<span class=
"keyword">const
</span> <span class=
"keywordtype">char
</span> *timestamp,
87 <a name=
"l00065"></a>00065 <span class=
"keyword">struct
</span> tm *result,
88 <a name=
"l00066"></a>00066 <span class=
"keywordtype">bool
</span> &utc,
89 <a name=
"l00067"></a>00067 <span class=
"keywordtype">bool
</span> *zone,
90 <a name=
"l00068"></a>00068 <span class=
"keywordtype">int
</span> *zoneminutes)
91 <a name=
"l00069"></a>00069 {
92 <a name=
"l00070"></a>00070 memset(result,
0,
<span class=
"keyword">sizeof
</span>(
<span class=
"keyword">struct
</span> tm));
93 <a name=
"l00071"></a>00071
94 <a name=
"l00072"></a>00072 <span class=
"comment">// handle YYYY-MM-DDTHH:MM:SS.uuu-HH:MM format
</span>
95 <a name=
"l00073"></a>00073 <span class=
"comment">// by stripping out the dashes and colons
</span>
96 <a name=
"l00074"></a>00074 <span class=
"keywordtype">string
</span> ts = timestamp;
97 <a name=
"l00075"></a>00075 string::iterator i = ts.begin();
98 <a name=
"l00076"></a>00076 <span class=
"keywordtype">bool
</span> date =
<span class=
"keyword">true
</span>;
99 <a name=
"l00077"></a>00077 <span class=
"keywordflow">while
</span>( i != ts.end() ) {
100 <a name=
"l00078"></a>00078 <span class=
"keywordflow">if
</span>( *i ==
<span class=
"charliteral">'T'
</span> )
101 <a name=
"l00079"></a>00079 date =
<span class=
"keyword">false
</span>;
102 <a name=
"l00080"></a>00080 <span class=
"keywordflow">if
</span>( (date
&& *i ==
<span class=
"charliteral">'-'
</span>) || *i ==
<span class=
"charliteral">':'
</span> )
103 <a name=
"l00081"></a>00081 ts.erase(i);
104 <a name=
"l00082"></a>00082 <span class=
"keywordflow">else
</span>
105 <a name=
"l00083"></a>00083 ++i;
106 <a name=
"l00084"></a>00084 }
107 <a name=
"l00085"></a>00085
108 <a name=
"l00086"></a>00086 <span class=
"keywordtype">int
</span> found = sscanf(ts.c_str(),
<span class=
"stringliteral">"%04d%02d%02dT%02d%02d%02d"</span>,
109 <a name=
"l00087"></a>00087 &(result-
>tm_year),
&(result-
>tm_mon),
&(result-
>tm_mday),
110 <a name=
"l00088"></a>00088 &(result-
>tm_hour),
&(result-
>tm_min),
&(result-
>tm_sec));
111 <a name=
"l00089"></a>00089
112 <a name=
"l00090"></a>00090 result-
>tm_year -=
1900;
113 <a name=
"l00091"></a>00091 result-
>tm_mon -=
1;
114 <a name=
"l00092"></a>00092 result-
>tm_isdst = -
1;
115 <a name=
"l00093"></a>00093 <span class=
"keywordflow">if
</span>( found ==
3 ) {
116 <a name=
"l00094"></a>00094 <span class=
"comment">// only a date available, so force time to
00:
00:
00</span>
117 <a name=
"l00095"></a>00095 result-
>tm_hour =
0;
118 <a name=
"l00096"></a>00096 result-
>tm_min =
0;
119 <a name=
"l00097"></a>00097 result-
>tm_sec =
0;
120 <a name=
"l00098"></a>00098 }
121 <a name=
"l00099"></a>00099 <span class=
"keywordflow">else
</span> <span class=
"keywordflow">if
</span>( found !=
6 ) {
122 <a name=
"l00100"></a>00100 <span class=
"keywordflow">return
</span> 0;
123 <a name=
"l00101"></a>00101 }
124 <a name=
"l00102"></a>00102
125 <a name=
"l00103"></a>00103 utc = ts.find(
<span class=
"charliteral">'Z'
</span>,
15) != string::npos;
126 <a name=
"l00104"></a>00104
127 <a name=
"l00105"></a>00105 <span class=
"keywordflow">if
</span>( zone
&& zoneminutes ) {
128 <a name=
"l00106"></a>00106 *zone =
<span class=
"keyword">false
</span>;
129 <a name=
"l00107"></a>00107
130 <a name=
"l00108"></a>00108 <span class=
"keywordtype">size_t
</span> neg = ts.find(
<span class=
"charliteral">'-'
</span>,
15);
131 <a name=
"l00109"></a>00109 <span class=
"keywordtype">size_t
</span> pos = ts.find(
<span class=
"charliteral">'+'
</span>,
15);
132 <a name=
"l00110"></a>00110
133 <a name=
"l00111"></a>00111 <span class=
"keywordflow">if
</span>( neg != string::npos || pos != string::npos ) {
134 <a name=
"l00112"></a>00112 <span class=
"comment">// capture timezone offset
</span>
135 <a name=
"l00113"></a>00113 <span class=
"keywordtype">size_t
</span> it = neg != string::npos ? neg : pos;
136 <a name=
"l00114"></a>00114 it++;
137 <a name=
"l00115"></a>00115 <span class=
"keywordtype">string
</span> offset = ts.substr(it);
138 <a name=
"l00116"></a>00116
139 <a name=
"l00117"></a>00117 <span class=
"keywordtype">int
</span> hour, min;
140 <a name=
"l00118"></a>00118 found = sscanf(offset.c_str(),
<span class=
"stringliteral">"%02d%02d"</span>,
141 <a name=
"l00119"></a>00119 &hour,
&min);
142 <a name=
"l00120"></a>00120 <span class=
"keywordflow">if
</span>( offset.size() ==
4 && found ==
2 ) {
143 <a name=
"l00121"></a>00121 *zone =
<span class=
"keyword">true
</span>;
144 <a name=
"l00122"></a>00122 *zoneminutes = hour *
60 + min;
145 <a name=
"l00123"></a>00123 <span class=
"keywordflow">if
</span>( neg != string::npos )
146 <a name=
"l00124"></a>00124 *zoneminutes *= -
1;
147 <a name=
"l00125"></a>00125 }
148 <a name=
"l00126"></a>00126 }
149 <a name=
"l00127"></a>00127 }
150 <a name=
"l00128"></a>00128
151 <a name=
"l00129"></a>00129 <span class=
"keywordflow">return
</span> result;
152 <a name=
"l00130"></a>00130 }
153 <a name=
"l00131"></a>00131
154 <a name=
"l00132"></a>00132 std::string tm_to_iso(
<span class=
"keyword">const
</span> <span class=
"keyword">struct
</span> tm *t,
<span class=
"keywordtype">bool
</span> utc)
155 <a name=
"l00133"></a>00133 {
156 <a name=
"l00134"></a>00134 <span class=
"keywordtype">char
</span> tmp[
128];
157 <a name=
"l00135"></a>00135
158 <a name=
"l00136"></a>00136 <span class=
"keywordtype">int
</span> cc = snprintf(tmp,
<span class=
"keyword">sizeof
</span>(tmp),
<span class=
"stringliteral">"%04d%02d%02dT%02d%02d%02d"</span>,
159 <a name=
"l00137"></a>00137 t-
>tm_year +
1900,
160 <a name=
"l00138"></a>00138 t-
>tm_mon +
1,
161 <a name=
"l00139"></a>00139 t-
>tm_mday,
162 <a name=
"l00140"></a>00140 t-
>tm_hour,
163 <a name=
"l00141"></a>00141 t-
>tm_min,
164 <a name=
"l00142"></a>00142 t-
>tm_sec
165 <a name=
"l00143"></a>00143 );
166 <a name=
"l00144"></a>00144 <span class=
"keywordflow">if
</span>( cc
< 0 || (
<span class=
"keywordtype">size_t
</span>)cc
>=
<span class=
"keyword">sizeof
</span>(tmp) )
167 <a name=
"l00145"></a>00145 <span class=
"keywordflow">return
</span> <span class=
"stringliteral">""</span>;
168 <a name=
"l00146"></a>00146
169 <a name=
"l00147"></a>00147 <span class=
"keywordflow">if
</span>( utc ) {
170 <a name=
"l00148"></a>00148 <span class=
"keywordflow">if
</span>( (
<span class=
"keywordtype">size_t
</span>)cc
>= (
<span class=
"keyword">sizeof
</span>(tmp) -
1) )
171 <a name=
"l00149"></a>00149 <span class=
"keywordflow">return
</span> <span class=
"stringliteral">""</span>;
<span class=
"comment">// not enough room for Z
</span>
172 <a name=
"l00150"></a>00150 tmp[cc++] =
<span class=
"charliteral">'Z'
</span>;
173 <a name=
"l00151"></a>00151 tmp[cc] =
0;
174 <a name=
"l00152"></a>00152 }
175 <a name=
"l00153"></a>00153
176 <a name=
"l00154"></a>00154 <span class=
"keywordflow">return
</span> tmp;
177 <a name=
"l00155"></a>00155 }
178 <a name=
"l00156"></a>00156
179 <a name=
"l00157"></a><a class=
"code" href=
"classBarry_1_1Sync_1_1TzWrapper.html#6770fc385305652d74b565b882dfab0c">00157</a> <a class=
"code" href=
"classBarry_1_1Sync_1_1TzWrapper.html" title=
"Wrapper class for the TZ environment variable.">TzWrapper
</a>& <a class=
"code" href=
"classBarry_1_1Sync_1_1TzWrapper.html#6770fc385305652d74b565b882dfab0c" title=
"Set timezone via offset in minutes Negative minutes goes west, positive goes east...">TzWrapper::SetOffset
</a>(
<span class=
"keywordtype">int
</span> zoneminutes)
180 <a name=
"l00158"></a>00158 {
181 <a name=
"l00159"></a>00159 <span class=
"comment">//
</span>
182 <a name=
"l00160"></a>00160 <span class=
"comment">// Set a custom TZ with the offset in hours/minutes.
</span>
183 <a name=
"l00161"></a>00161 <span class=
"comment">//
</span>
184 <a name=
"l00162"></a>00162 <span class=
"comment">// Note that TZ sees negative offsets as *ahead* of
</span>
185 <a name=
"l00163"></a>00163 <span class=
"comment">// UTC and positive offsets as behind UTC. Therefore,
</span>
186 <a name=
"l00164"></a>00164 <span class=
"comment">// Berlin, one hour ahead of UTC is -
01:
00 and
</span>
187 <a name=
"l00165"></a>00165 <span class=
"comment">// Canada/Eastern standard time is +
05:
00.
</span>
188 <a name=
"l00166"></a>00166 <span class=
"comment">//
</span>
189 <a name=
"l00167"></a>00167 <span class=
"comment">// This is exactly opposite to the ISO timestamp format
</span>
190 <a name=
"l00168"></a>00168 <span class=
"comment">// which would have +
01:
00 and -
05:
00 respectively,
</span>
191 <a name=
"l00169"></a>00169 <span class=
"comment">// and therefore exactly opposite to the sign of zoneminutes.
</span>
192 <a name=
"l00170"></a>00170 <span class=
"comment">//
</span>
193 <a name=
"l00171"></a>00171 <span class=
"comment">// We use a fake timezone name of XXX here, since it
</span>
194 <a name=
"l00172"></a>00172 <span class=
"comment">// doesn't matter, we are only interested in the offset.
</span>
195 <a name=
"l00173"></a>00173
196 <a name=
"l00174"></a>00174 <span class=
"keywordtype">char
</span> buf[
128];
197 <a name=
"l00175"></a>00175 sprintf(buf,
<span class=
"stringliteral">"XXX%c%02d:%02d"</span>,
198 <a name=
"l00176"></a>00176 (zoneminutes
< 0 ?
<span class=
"charliteral">'+'
</span> :
<span class=
"charliteral">'-'
</span>),
199 <a name=
"l00177"></a>00177 abs(zoneminutes) /
60,
200 <a name=
"l00178"></a>00178 abs(zoneminutes) %
60
201 <a name=
"l00179"></a>00179 );
202 <a name=
"l00180"></a>00180 <span class=
"keywordflow">return
</span> <a class=
"code" href=
"classBarry_1_1Sync_1_1TzWrapper.html#46e3e3ec5c35694810271476707a6186" title=
"Set TZ to a new value.">Set
</a>(buf);
203 <a name=
"l00181"></a>00181 }
204 <a name=
"l00182"></a>00182
205 <a name=
"l00183"></a><a class=
"code" href=
"classBarry_1_1Sync_1_1TzWrapper.html#c1524071e5a61fa789331c9784b6093d">00183</a> time_t TzWrapper::iso_mktime(
<span class=
"keyword">const
</span> <span class=
"keywordtype">char
</span> *timestamp)
206 <a name=
"l00184"></a>00184 {
207 <a name=
"l00185"></a>00185 <span class=
"keywordtype">bool
</span> utc, zone;
208 <a name=
"l00186"></a>00186 <span class=
"keyword">struct
</span>tm t;
209 <a name=
"l00187"></a>00187 <span class=
"keywordtype">int
</span> zoneminutes;
210 <a name=
"l00188"></a>00188 <span class=
"keywordflow">if
</span>( !iso_to_tm(timestamp,
&t, utc,
&zone,
&zoneminutes) )
211 <a name=
"l00189"></a>00189 <span class=
"keywordflow">return
</span> (time_t)-
1;
212 <a name=
"l00190"></a>00190
213 <a name=
"l00191"></a>00191 <a class=
"code" href=
"classBarry_1_1Sync_1_1TzWrapper.html" title=
"Wrapper class for the TZ environment variable.">TzWrapper
</a> tzw;
214 <a name=
"l00192"></a>00192 <span class=
"keywordflow">if
</span>( utc ) {
215 <a name=
"l00193"></a>00193 tzw.
<a class=
"code" href=
"classBarry_1_1Sync_1_1TzWrapper.html#ab3349c356c87e37d41841d78fd8f227" title=
"Set timezone to UTC.">SetUTC
</a>();
216 <a name=
"l00194"></a>00194 }
217 <a name=
"l00195"></a>00195 <span class=
"keywordflow">else
</span> <span class=
"keywordflow">if
</span>( zone ) {
218 <a name=
"l00196"></a>00196 tzw.
<a class=
"code" href=
"classBarry_1_1Sync_1_1TzWrapper.html#6770fc385305652d74b565b882dfab0c" title=
"Set timezone via offset in minutes Negative minutes goes west, positive goes east...">SetOffset
</a>(zoneminutes);
219 <a name=
"l00197"></a>00197 }
220 <a name=
"l00198"></a>00198 <span class=
"keywordflow">return
</span> tzw.
<a class=
"code" href=
"classBarry_1_1Sync_1_1TzWrapper.html#1f1529e9d5001b0b08de41d884c27d63">mktime
</a>(
&t);
221 <a name=
"l00199"></a>00199 }
222 <a name=
"l00200"></a>00200
223 <a name=
"l00201"></a>00201 }}
<span class=
"comment">// namespace Barry::Sync
</span>
224 <a name=
"l00202"></a>00202
225 <a name=
"l00203"></a>00203
226 <a name=
"l00204"></a>00204 <span class=
"preprocessor">#ifdef TZ_TEST_MODE
</span>
227 <a name=
"l00205"></a>00205 <span class=
"preprocessor"></span><span class=
"preprocessor">#include
<iostream
></span>
228 <a name=
"l00206"></a>00206 <span class=
"keyword">using namespace
</span>std;
229 <a name=
"l00207"></a>00207 <span class=
"keyword">using namespace
</span>Barry::Sync;
230 <a name=
"l00208"></a>00208 <span class=
"keywordtype">int
</span> main()
231 <a name=
"l00209"></a>00209 {
232 <a name=
"l00210"></a>00210 time_t now = time(NULL);
233 <a name=
"l00211"></a>00211
234 <a name=
"l00212"></a>00212 cout
<< <span class=
"stringliteral">"TZ: "</span> << TzWrapper().ctime(
&now);
235 <a name=
"l00213"></a>00213 cout
<< <span class=
"stringliteral">"UTC: "</span> << TzWrapper(
<span class=
"stringliteral">""</span>).ctime(
&now);
236 <a name=
"l00214"></a>00214 cout
<< <span class=
"stringliteral">"UTC: "</span> << TzWrapper().SetUTC().ctime(
&now);
237 <a name=
"l00215"></a>00215 cout
<< <span class=
"stringliteral">"SysLocaltime: "</span> << TzWrapper().SetUTC().SetSysLocal().ctime(
&now);
238 <a name=
"l00216"></a>00216 cout
<< <span class=
"stringliteral">"TZ: "</span> << TzWrapper().SetUTC().SetDefault().ctime(
&now);
239 <a name=
"l00217"></a>00217 cout
<< <span class=
"stringliteral">"Canada/Eastern: "</span> << TzWrapper(
<span class=
"stringliteral">"Canada/Eastern"</span>).ctime(
&now);
240 <a name=
"l00218"></a>00218 cout
<< <span class=
"stringliteral">"Canada/Pacific: "</span> << TzWrapper(
<span class=
"stringliteral">"Canada/Pacific"</span>).ctime(
&now);
241 <a name=
"l00219"></a>00219
242 <a name=
"l00220"></a>00220 {
243 <a name=
"l00221"></a>00221 TzWrapper tzw(
<span class=
"stringliteral">"UTC"</span>);
244 <a name=
"l00222"></a>00222 cout
<< <span class=
"stringliteral">"UTC: "</span> << ctime(
&now);
245 <a name=
"l00223"></a>00223 }
246 <a name=
"l00224"></a>00224
247 <a name=
"l00225"></a>00225 cout
<< <span class=
"stringliteral">"TZ: "</span> << ctime(
&now);
248 <a name=
"l00226"></a>00226
249 <a name=
"l00227"></a>00227 <span class=
"comment">// test iso_mktime()... the test values assume a Canada/Eastern TZ
</span>
250 <a name=
"l00228"></a>00228 cout
<< <span class=
"stringliteral">"Using Canada/Eastern:"</span> << endl;
251 <a name=
"l00229"></a>00229 TzWrapper tzw(
<span class=
"stringliteral">"Canada/Eastern"</span>);
252 <a name=
"l00230"></a>00230 <span class=
"keyword">const
</span> <span class=
"keywordtype">char
</span> *iso1 =
<span class=
"stringliteral">"20100430T231500"</span>;
253 <a name=
"l00231"></a>00231 <span class=
"keyword">const
</span> <span class=
"keywordtype">char
</span> *iso2 =
<span class=
"stringliteral">"20100501T031500Z"</span>;
254 <a name=
"l00232"></a>00232 time_t t1 = TzWrapper::iso_mktime(iso1);
255 <a name=
"l00233"></a>00233 time_t t2 = TzWrapper::iso_mktime(iso2);
256 <a name=
"l00234"></a>00234 cout
<< <span class=
"stringliteral">" "</span> << iso1
<< <span class=
"stringliteral">": ("</span> << t1
<< <span class=
"stringliteral">") "</span> << ctime(
&t1);
257 <a name=
"l00235"></a>00235 cout
<< iso2
<< <span class=
"stringliteral">": ("</span> << t2
<< <span class=
"stringliteral">") "</span> << ctime(
&t2);
258 <a name=
"l00236"></a>00236 <span class=
"keywordflow">if
</span>( t1 == t2 )
259 <a name=
"l00237"></a>00237 cout
<< <span class=
"stringliteral">"t1 == t2: passed"</span> << endl;
260 <a name=
"l00238"></a>00238 <span class=
"keywordflow">else
</span>
261 <a name=
"l00239"></a>00239 cout
<< <span class=
"stringliteral">"t1 != t2: ERROR"</span> << endl;
262 <a name=
"l00240"></a>00240
263 <a name=
"l00241"></a>00241 time_t t3 = TzWrapper::iso_mktime(
<span class=
"stringliteral">"2010-05-01T03:15:00.000Z"</span>);
264 <a name=
"l00242"></a>00242 cout
<< ctime(
&t3);
265 <a name=
"l00243"></a>00243 <span class=
"keywordflow">if
</span>( t2 == t3 )
266 <a name=
"l00244"></a>00244 cout
<< <span class=
"stringliteral">"t2 == t3: passed"</span> << endl;
267 <a name=
"l00245"></a>00245 <span class=
"keywordflow">else
</span>
268 <a name=
"l00246"></a>00246 cout
<< <span class=
"stringliteral">"t2 != t3: ERROR"</span> << endl;
269 <a name=
"l00247"></a>00247
270 <a name=
"l00248"></a>00248 time_t t4 = TzWrapper::iso_mktime(
<span class=
"stringliteral">"2010-05-01T04:15:00.000+01:00"</span>);
271 <a name=
"l00249"></a>00249 cout
<< ctime(
&t4);
272 <a name=
"l00250"></a>00250 <span class=
"keywordflow">if
</span>( t3 == t4 )
273 <a name=
"l00251"></a>00251 cout
<< <span class=
"stringliteral">"t3 == t4: passed"</span> << endl;
274 <a name=
"l00252"></a>00252 <span class=
"keywordflow">else
</span>
275 <a name=
"l00253"></a>00253 cout
<< <span class=
"stringliteral">"t3 != t4: ERROR"</span> << endl;
276 <a name=
"l00254"></a>00254
277 <a name=
"l00255"></a>00255 time_t t5 = TzWrapper::iso_mktime(
<span class=
"stringliteral">"2010-05-01T00:15:00.000-03:00"</span>);
278 <a name=
"l00256"></a>00256 cout
<< ctime(
&t5);
279 <a name=
"l00257"></a>00257 <span class=
"keywordflow">if
</span>( t4 == t5 )
280 <a name=
"l00258"></a>00258 cout
<< <span class=
"stringliteral">"t4 == t5: passed"</span> << endl;
281 <a name=
"l00259"></a>00259 <span class=
"keywordflow">else
</span>
282 <a name=
"l00260"></a>00260 cout
<< <span class=
"stringliteral">"t4 != t5: ERROR: t4: "</span> << t4
<< <span class=
"stringliteral">" t5: "</span> << t5
<< endl;
283 <a name=
"l00261"></a>00261
284 <a name=
"l00262"></a>00262 <span class=
"keywordflow">if
</span>( TzWrapper::iso_mktime(
<span class=
"stringliteral">"20100430"</span>) != (time_t)-
1 )
285 <a name=
"l00263"></a>00263 cout
<< <span class=
"stringliteral">"Date check: passed"</span> << endl;
286 <a name=
"l00264"></a>00264 <span class=
"keywordflow">else
</span>
287 <a name=
"l00265"></a>00265 cout
<< <span class=
"stringliteral">"Date check: ERROR"</span> << endl;
288 <a name=
"l00266"></a>00266
289 <a name=
"l00267"></a>00267 cout
<< <span class=
"stringliteral">"t1: "</span> << tm_to_iso(gmtime(
&t1),
<span class=
"keyword">true
</span>)
<< endl;
290 <a name=
"l00268"></a>00268
291 <a name=
"l00269"></a>00269 <span class=
"keywordtype">bool
</span> utc, zone;
292 <a name=
"l00270"></a>00270 <span class=
"keywordtype">int
</span> zoneminutes;
293 <a name=
"l00271"></a>00271 <span class=
"keyword">struct
</span>tm zonetest;
294 <a name=
"l00272"></a>00272 <span class=
"keywordflow">if
</span>( !iso_to_tm(
<span class=
"stringliteral">"2010-05-01T04:15:00.000-"</span>,
&zonetest, utc,
&zone,
&zoneminutes) )
295 <a name=
"l00273"></a>00273 cout
<< <span class=
"stringliteral">"iso_to_tm failed wrongly: ERROR"</span> << endl;
296 <a name=
"l00274"></a>00274 <span class=
"keywordflow">if
</span>( zone )
297 <a name=
"l00275"></a>00275 cout
<< <span class=
"stringliteral">"zone true?: ERROR"</span> << endl;
298 <a name=
"l00276"></a>00276 <span class=
"keywordflow">else
</span>
299 <a name=
"l00277"></a>00277 cout
<< <span class=
"stringliteral">"zone fail check: passed"</span> << endl;
300 <a name=
"l00278"></a>00278
301 <a name=
"l00279"></a>00279 <span class=
"keywordflow">if
</span>( !iso_to_tm(
<span class=
"stringliteral">"2010-05-01T04:15:00.000-010"</span>,
&zonetest, utc,
&zone,
&zoneminutes) )
302 <a name=
"l00280"></a>00280 cout
<< <span class=
"stringliteral">"iso_to_tm failed wrongly: ERROR"</span> << endl;
303 <a name=
"l00281"></a>00281 <span class=
"keywordflow">if
</span>( zone )
304 <a name=
"l00282"></a>00282 cout
<< <span class=
"stringliteral">"zone true?: ERROR"</span> << endl;
305 <a name=
"l00283"></a>00283 <span class=
"keywordflow">else
</span>
306 <a name=
"l00284"></a>00284 cout
<< <span class=
"stringliteral">"zone fail check2: passed"</span> << endl;
307 <a name=
"l00285"></a>00285 }
308 <a name=
"l00286"></a>00286 <span class=
"preprocessor">#endif
</span>
309 <a name=
"l00287"></a>00287 <span class=
"preprocessor"></span>
311 <hr size=
"1"><address style=
"text-align: right;"><small>Generated on Thu Feb
10 16:
13:
59 2011 for Barry by
312 <a href=
"http://www.doxygen.org/index.html">
313 <img src=
"doxygen.png" alt=
"doxygen" align=
"middle" border=
"0"></a> 1.5.6 </small></address>