~ubuntu-branches/debian/lenny/libgsf/lenny

« back to all changes in this revision

Viewing changes to gsf/gsf-timestamp.c

  • Committer: Bazaar Package Importer
  • Author(s): J.H.M. Dassen (Ray)
  • Date: 2006-11-06 22:45:03 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 dapper)
  • Revision ID: james.westby@ubuntu.com-20061106224503-g6pmv1m82zy8jya9
Tags: 1.14.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 *
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
19
19
 * USA
20
20
 */
21
21
 
23
23
#include <gsf/gsf-timestamp.h>
24
24
#include <string.h>
25
25
#include <time.h>
 
26
#include <stdio.h>
 
27
#ifdef G_OS_WIN32
 
28
#include <windows.h>
 
29
#endif
26
30
 
27
31
static void
28
32
timestamp_to_string (GValue const *src_value, GValue *dest_value)
47
51
        return our_type;
48
52
}
49
53
 
 
54
/**
 
55
 * gsf_timestamp_copy:
 
56
 * @stamp: timestamp to be copied
 
57
 *
 
58
 * Copies a timestamp.
 
59
 *
 
60
 * Returns: a separate copy of @stamp.
 
61
 */
50
62
GsfTimestamp *
51
63
gsf_timestamp_copy (GsfTimestamp const *stamp)
52
64
{
55
67
        return res;
56
68
}
57
69
 
 
70
/**
 
71
 * gsf_timestamp_free :
 
72
 * @stamp : timestamp to be freed
 
73
 *
 
74
 * Releases the memory in use for @stamp (if any).
 
75
 **/
58
76
void
59
77
gsf_timestamp_free (GsfTimestamp *stamp)
60
78
{
61
79
        g_free (stamp);
62
80
}
63
81
 
 
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())
 
88
#else
 
89
/* FIXME: work out the offset anyway. */
 
90
#define GMTOFF(t) (0)
 
91
#endif
 
92
 
 
93
#ifdef G_OS_WIN32
 
94
static time_t gmt_to_local_win32(void)
 
95
{
 
96
    TIME_ZONE_INFORMATION tzinfo;
 
97
    DWORD dwStandardDaylight;
 
98
    long bias;
 
99
 
 
100
    dwStandardDaylight = GetTimeZoneInformation(&tzinfo);
 
101
    bias = tzinfo.Bias;
 
102
 
 
103
    if (dwStandardDaylight == TIME_ZONE_ID_STANDARD)
 
104
        bias += tzinfo.StandardBias;
 
105
    
 
106
    if (dwStandardDaylight == TIME_ZONE_ID_DAYLIGHT)
 
107
        bias += tzinfo.DaylightBias;
 
108
    
 
109
    return (- bias * 60);
 
110
}
 
111
#endif
 
112
 
 
113
/**
 
114
 * gsf_timestamp_parse :
 
115
 * @spec : The string to parse
 
116
 * @stamp : #GsfTimestamp
 
117
 *
 
118
 * Very simple parser for time stamps.  Currently requires a format of
 
119
 *      'YYYY-MM-DDThh:mm:ss'
 
120
 * and does no bounds checking.
 
121
 *
 
122
 * ICK ICK ICK
 
123
 * This routine should be called _from_sting.
 
124
 *
 
125
 * Returns TRUE on success
 
126
 **/
64
127
int
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)
67
129
{
68
 
        return 0;
 
130
        struct tm       tm;
 
131
 
 
132
        memset (&tm, 0, sizeof (struct tm));
 
133
 
 
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 */
 
139
 
 
140
                /* err on the side of avoiding negatives */
 
141
                if (tm.tm_year >= 1900)
 
142
                        tm.tm_year -= 1900;
 
143
 
 
144
                stamp->timet = mktime (&tm) + GMTOFF(tm);
 
145
                return TRUE;
 
146
        }
 
147
        return FALSE;
69
148
}
70
149
 
 
150
/**
 
151
 * gsf_timestamp_as_string :
 
152
 * @stamp: timestamp to be converted.
 
153
 *
 
154
 * Produce a string representation (ISO 8601 format) of @stamp.
 
155
 *
 
156
 * Returns: a string representation of @stamp. When @stamp is NULL, the
 
157
 * representation is "&lt;invalid&gt;".
 
158
 */
71
159
char *
72
160
gsf_timestamp_as_string (GsfTimestamp const *stamp)
73
161
{
74
 
        time_t t;
 
162
        time_t    t;
 
163
        struct tm tm;
 
164
 
75
165
        g_return_val_if_fail (stamp != NULL, g_strdup ("<invalid>"));
76
166
 
77
 
        /* Use an honest time_t for ctime.  */
78
 
        t = stamp->timet;
79
 
        return g_strdup (ctime (&t));
 
167
        t = stamp->timet;       /* Use an honest time_t for gmtime_r.  */
 
168
#ifdef HAVE_GMTIME_R
 
169
        gmtime_r (&t, &tm);
 
170
#else
 
171
        /* -NOT- thread-safe */
 
172
        tm = *gmtime (&t);
 
173
#endif
 
174
 
 
175
 
 
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);
80
180
}
81
181
 
82
182
guint
85
185
        return stamp->timet;
86
186
}
87
187
 
 
188
/**
 
189
 * gsf_timestamp_equal :
 
190
 * @a: a timestamp
 
191
 * @b: another timestamp
 
192
 *
 
193
 * Compare timestamps @a and @b.
 
194
 *
 
195
 * Returns: true if @a and @b represent the same point in time; false otherwise.
 
196
 *
 
197
 **/
88
198
gboolean
89
199
gsf_timestamp_equal (GsfTimestamp const *a, GsfTimestamp const *b)
90
200
{
92
202
}
93
203
 
94
204
void
95
 
g_value_set_timestamp (GValue *value, GsfTimestamp const *stamp)
 
205
gsf_value_set_timestamp (GValue *value, GsfTimestamp const *stamp)
96
206
{
97
207
        g_value_set_boxed (value, stamp);
98
208
}