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 result
= gmtime (&time
);
100 timestamp
= beagle_timestamp_new ();
102 timestamp
->year
= result
->tm_year
+ 1900;
103 timestamp
->month
= result
->tm_mon
+ 1;
104 timestamp
->day
= result
->tm_mday
;
106 timestamp
->hour
= result
->tm_hour
;
107 timestamp
->minute
= result
->tm_min
;
108 timestamp
->second
= result
->tm_sec
;
114 * beagle_timestamp_free:
115 * @timestamp: a #BeagleTimestamp
117 * Frees the memory allocated by the given #BeagleTimestamp.
120 beagle_timestamp_free (BeagleTimestamp
*timestamp
)
125 /* I love you, UNIX */
127 give_me_a_time_t_that_is_utc (struct tm
*tm
) {
144 * beagle_timestamp_to_unix_time:
145 * @timestamp: a #BeagleTimestamp
148 * Converts the given #BeagleTimestamp to a unix #time_t.
150 * Return value: %TRUE on success and otherwise %FALSE.
153 beagle_timestamp_to_unix_time (BeagleTimestamp
*timestamp
, time_t *time
)
158 /* We special-case the timestamp "epoch" and use the unix epoch */
159 if (timestamp
->year
== 0 && timestamp
->month
== 0 && timestamp
->day
== 0 &&
160 timestamp
->hour
== 0 && timestamp
->minute
== 0 && timestamp
->second
== 0 &&
161 timestamp
->ticks
== 0 && timestamp
->tz_hour
== 0 && timestamp
->tz_minute
== 0) {
166 if (timestamp
->year
< 1970 || timestamp
->year
> 2038) {
170 tm_time
.tm_year
= timestamp
->year
- 1900;
171 tm_time
.tm_mon
= timestamp
->month
- 1;
172 tm_time
.tm_mday
= timestamp
->day
;
173 tm_time
.tm_hour
= timestamp
->hour
;
174 tm_time
.tm_min
= timestamp
->minute
;
175 tm_time
.tm_sec
= timestamp
->second
;
176 tm_time
.tm_isdst
= -1;
178 result
= give_me_a_time_t_that_is_utc (&tm_time
);
184 if (timestamp
->tz_hour
> 0)
185 tz
= timestamp
->tz_hour
* 60 + timestamp
->tz_minute
;
187 tz
= timestamp
->tz_hour
*60 - timestamp
->tz_minute
;
193 /* Check for overflow */
203 _beagle_timestamp_to_string (BeagleTimestamp
*timestamp
)
205 return g_strdup_printf ("%04d-%02d-%02dT%02d:%02d:%02d.%07d%+03d:%02d",
206 timestamp
->year
, timestamp
->month
, timestamp
->day
,
207 timestamp
->hour
, timestamp
->minute
, timestamp
->second
,
208 timestamp
->ticks
, timestamp
->tz_hour
, timestamp
->tz_minute
);
213 _beagle_timestamp_get_start (void)