4 * Copyright (C) 2005 Novell, Inc.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
28 #include "beagle-timestamp.h"
29 #include "beagle-private.h"
34 struct _BeagleTimestamp
{
37 int hour
, minute
, second
;
41 int tz_hour
, tz_minute
;
44 static BeagleTimestamp
*
45 beagle_timestamp_new (void)
47 BeagleTimestamp
*timestamp
;
49 timestamp
= g_new0 (BeagleTimestamp
, 1);
55 * beagle_timestamp_new_from_string:
58 * Creates a newly allocated #BeagleTimestamp from the given string. The string should be of the following format, "2005-06-23T10:05:11.0000000+01:00".
60 * Return value: the newly allocated #BeagleTimestamp.
63 beagle_timestamp_new_from_string (const char *str
)
65 BeagleTimestamp
*timestamp
;
68 timestamp
= beagle_timestamp_new ();
70 consumed
= sscanf (str
, "%04d-%02d-%02dT%02d:%02d:%02d.%07d%03d:%02d",
71 ×tamp
->year
, ×tamp
->month
, ×tamp
->day
,
72 ×tamp
->hour
, ×tamp
->minute
, ×tamp
->second
,
73 ×tamp
->ticks
, ×tamp
->tz_hour
, ×tamp
->tz_minute
);
76 beagle_timestamp_free (timestamp
);
85 * beagle_timestamp_new_from_unix_time:
88 * Creates a newly allocated #BeagleTimestamp from @time.
90 * Return value: the newly created #BeagleTimestamp.
93 beagle_timestamp_new_from_unix_time (time_t time
)
95 BeagleTimestamp
*timestamp
;
98 int offset_mins
, tz_hour
, tz_min
;
100 /* Send everything in localtime since XmlSerializer .Net-1.0 likes that.
101 * FIXED: To be changed back to gmtime() in .Net-2.0 era.
103 result
= localtime (&time
);
105 timestamp
= beagle_timestamp_new ();
107 timestamp
->year
= result
->tm_year
+ 1900;
108 timestamp
->month
= result
->tm_mon
+ 1;
109 timestamp
->day
= result
->tm_mday
;
111 timestamp
->hour
= result
->tm_hour
;
112 timestamp
->minute
= result
->tm_min
;
113 timestamp
->second
= result
->tm_sec
;
115 /* Compute timezone offset. */
117 result
= localtime (&time
);
118 before_utc
= (result
->tm_mday
== 1);
120 offset_mins
= 60 * result
->tm_hour
+ result
->tm_min
;
121 if (before_utc
== FALSE
)
122 offset_mins
= 1440 - offset_mins
; /* 1440 = mins in 1 day */
124 tz_hour
= offset_mins
/60;
125 tz_min
= offset_mins
% 60;
126 if (before_utc
== FALSE
)
129 timestamp
->tz_hour
= tz_hour
;
130 timestamp
->tz_minute
= tz_min
;
136 * beagle_timestamp_free:
137 * @timestamp: a #BeagleTimestamp
139 * Frees the memory allocated by the given #BeagleTimestamp.
142 beagle_timestamp_free (BeagleTimestamp
*timestamp
)
147 /* I love you, UNIX */
149 give_me_a_time_t_that_is_utc (struct tm
*tm
) {
166 * beagle_timestamp_to_unix_time:
167 * @timestamp: a #BeagleTimestamp
170 * Converts the given #BeagleTimestamp to a unix #time_t.
172 * Return value: %TRUE on success and otherwise %FALSE.
175 beagle_timestamp_to_unix_time (BeagleTimestamp
*timestamp
, time_t *time
)
180 /* We special-case the timestamp "epoch" and use the unix epoch */
181 if (timestamp
->year
== 0 && timestamp
->month
== 0 && timestamp
->day
== 0 &&
182 timestamp
->hour
== 0 && timestamp
->minute
== 0 && timestamp
->second
== 0 &&
183 timestamp
->ticks
== 0 && timestamp
->tz_hour
== 0 && timestamp
->tz_minute
== 0) {
188 if (timestamp
->year
< 1970 || timestamp
->year
> 2038) {
192 tm_time
.tm_year
= timestamp
->year
- 1900;
193 tm_time
.tm_mon
= timestamp
->month
- 1;
194 tm_time
.tm_mday
= timestamp
->day
;
195 tm_time
.tm_hour
= timestamp
->hour
;
196 tm_time
.tm_min
= timestamp
->minute
;
197 tm_time
.tm_sec
= timestamp
->second
;
198 tm_time
.tm_isdst
= -1;
200 result
= give_me_a_time_t_that_is_utc (&tm_time
);
206 if (timestamp
->tz_hour
> 0)
207 tz
= timestamp
->tz_hour
* 60 + timestamp
->tz_minute
;
209 tz
= timestamp
->tz_hour
*60 - timestamp
->tz_minute
;
215 /* Check for overflow */
225 _beagle_timestamp_to_string (BeagleTimestamp
*timestamp
)
227 return g_strdup_printf ("%04d-%02d-%02dT%02d:%02d:%02d.%07d%+03d:%02d",
228 timestamp
->year
, timestamp
->month
, timestamp
->day
,
229 timestamp
->hour
, timestamp
->minute
, timestamp
->second
,
230 timestamp
->ticks
, timestamp
->tz_hour
, timestamp
->tz_minute
);
235 _beagle_timestamp_get_start (void)