16
16
* You should have received a copy of the GNU Lesser General Public License
17
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
71
* gsf_timestamp_free :
72
* @stamp : timestamp to be freed
74
* Releases the memory in use for @stamp (if any).
59
77
gsf_timestamp_free (GsfTimestamp *stamp)
82
#if defined(HAVE_STRUCT_TM_TM_GMTOFF)
83
#define GMTOFF(t) ((t).tm_gmtoff)
84
#elif defined(HAVE_STRUCT_TM___TM_GMTOFF)
85
#define GMTOFF(t) ((t).__tm_gmtoff)
86
#elif defined(G_OS_WIN32)
87
#define GMTOFF(t) (gmt_to_local_win32())
89
/* FIXME: work out the offset anyway. */
94
static time_t gmt_to_local_win32(void)
96
TIME_ZONE_INFORMATION tzinfo;
97
DWORD dwStandardDaylight;
100
dwStandardDaylight = GetTimeZoneInformation(&tzinfo);
103
if (dwStandardDaylight == TIME_ZONE_ID_STANDARD)
104
bias += tzinfo.StandardBias;
106
if (dwStandardDaylight == TIME_ZONE_ID_DAYLIGHT)
107
bias += tzinfo.DaylightBias;
109
return (- bias * 60);
114
* gsf_timestamp_parse :
115
* @spec : The string to parse
116
* @stamp : #GsfTimestamp
118
* Very simple parser for time stamps. Currently requires a format of
119
* 'YYYY-MM-DDThh:mm:ss'
120
* and does no bounds checking.
123
* This routine should be called _from_sting.
125
* Returns TRUE on success
65
gsf_timestamp_parse (G_GNUC_UNUSED char const *spec,
66
G_GNUC_UNUSED GsfTimestamp *stamp)
128
gsf_timestamp_parse (char const *spec, GsfTimestamp *stamp)
132
memset (&tm, 0, sizeof (struct tm));
134
/* 'YYYY-MM-DDThh:mm:ss' */
135
if (6 == sscanf (spec, "%d-%d-%dT%d:%d:%d",
136
&tm.tm_year, &tm.tm_mon, &tm.tm_mday,
137
&tm.tm_hour, &tm.tm_min, &tm.tm_sec)) {
138
tm.tm_mon--; /* 0..11 */
140
/* err on the side of avoiding negatives */
141
if (tm.tm_year >= 1900)
144
stamp->timet = mktime (&tm) + GMTOFF(tm);
151
* gsf_timestamp_as_string :
152
* @stamp: timestamp to be converted.
154
* Produce a string representation (ISO 8601 format) of @stamp.
156
* Returns: a string representation of @stamp. When @stamp is NULL, the
157
* representation is "<invalid>".
72
160
gsf_timestamp_as_string (GsfTimestamp const *stamp)
75
165
g_return_val_if_fail (stamp != NULL, g_strdup ("<invalid>"));
77
/* Use an honest time_t for ctime. */
79
return g_strdup (ctime (&t));
167
t = stamp->timet; /* Use an honest time_t for gmtime_r. */
171
/* -NOT- thread-safe */
176
/* using 'YYYY-MM-DDThh:mm:ss' */
177
return g_strdup_printf ("%4d-%02d-%02dT%02d:%02d:%02dZ",
178
tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
179
tm.tm_hour, tm.tm_min, tm.tm_sec);