./configure wasnt happy if gsf is present but no wv. Its happy now. pkg-check-modules...
[beagle.git] / libbeagle / beagle / beagle-timestamp.c
blob69c521e25312e0a5cb2d7ce1927720675ebbf5fd
1 /*
2 * beagle-timestamp.c
4 * Copyright (C) 2005 Novell, Inc.
6 */
8 /*
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"
30 #include <stdio.h>
31 #include <time.h>
32 #include <stdlib.h>
34 struct _BeagleTimestamp {
35 int year, month, day;
37 int hour, minute, second;
39 int ticks;
41 int tz_hour, tz_minute;
44 static BeagleTimestamp *
45 beagle_timestamp_new (void)
47 BeagleTimestamp *timestamp;
49 timestamp = g_new0 (BeagleTimestamp, 1);
51 return timestamp;
54 /**
55 * beagle_timestamp_new_from_string:
56 * @str: a 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.
61 **/
62 BeagleTimestamp *
63 beagle_timestamp_new_from_string (const char *str)
65 BeagleTimestamp *timestamp;
66 int consumed;
68 timestamp = beagle_timestamp_new ();
70 consumed = sscanf (str, "%04d-%02d-%02dT%02d:%02d:%02d.%07d%03d:%02d",
71 &timestamp->year, &timestamp->month, &timestamp->day,
72 &timestamp->hour, &timestamp->minute, &timestamp->second,
73 &timestamp->ticks, &timestamp->tz_hour, &timestamp->tz_minute);
75 if (consumed != 9) {
76 beagle_timestamp_free (timestamp);
77 return NULL;
81 return timestamp;
84 /**
85 * beagle_timestamp_new_from_unix_time:
86 * @time: a #time_t
88 * Creates a newly allocated #BeagleTimestamp from @time.
90 * Return value: the newly created #BeagleTimestamp.
91 **/
92 BeagleTimestamp *
93 beagle_timestamp_new_from_unix_time (time_t time)
95 BeagleTimestamp *timestamp;
96 struct tm *result;
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;
110 return timestamp;
114 * beagle_timestamp_free:
115 * @timestamp: a #BeagleTimestamp
117 * Frees the memory allocated by the given #BeagleTimestamp.
119 void
120 beagle_timestamp_free (BeagleTimestamp *timestamp)
122 g_free (timestamp);
125 /* I love you, UNIX */
126 static time_t
127 give_me_a_time_t_that_is_utc (struct tm *tm) {
128 time_t ret;
129 char *tz;
131 tz = getenv("TZ");
132 setenv("TZ", "", 1);
133 tzset();
134 ret = mktime(tm);
135 if (tz)
136 setenv("TZ", tz, 1);
137 else
138 unsetenv("TZ");
139 tzset();
140 return ret;
144 * beagle_timestamp_to_unix_time:
145 * @timestamp: a #BeagleTimestamp
146 * @time: a #time_t
148 * Converts the given #BeagleTimestamp to a unix #time_t.
150 * Return value: %TRUE on success and otherwise %FALSE.
152 gboolean
153 beagle_timestamp_to_unix_time (BeagleTimestamp *timestamp, time_t *time)
155 time_t result, tz;
156 struct tm tm_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) {
162 *time = 0;
163 return TRUE;
166 if (timestamp->year < 1970 || timestamp->year > 2038) {
167 return FALSE;
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);
180 if (result == -1)
181 return FALSE;
183 /* Add timezone */
184 if (timestamp->tz_hour > 0)
185 tz = timestamp->tz_hour * 60 + timestamp->tz_minute;
186 else
187 tz = timestamp->tz_hour *60 - timestamp->tz_minute;
189 tz *= 60;
191 result += tz;
193 /* Check for overflow */
194 if (result < 0)
195 return FALSE;
197 *time = result;
199 return TRUE;
202 char *
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);
212 char *
213 _beagle_timestamp_get_start (void)
215 return NULL;