2 * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
24 #include "glib-compat-private.h"
25 #include "gst_private.h"
26 #include "gstdatetime.h"
33 * @short_description: A date, time and timezone structure
35 * Struct to store date, time and timezone information altogether.
36 * #GstDateTime is refcounted and immutable.
38 * Date information is handled using the proleptic Gregorian calendar.
40 * Provides basic creation functions and accessor functions to its fields.
45 #define GST_DATE_TIME_SEC_PER_DAY (G_GINT64_CONSTANT (86400))
46 #define GST_DATE_TIME_USEC_PER_DAY (G_GINT64_CONSTANT (86400000000))
47 #define GST_DATE_TIME_USEC_PER_HOUR (G_GINT64_CONSTANT (3600000000))
48 #define GST_DATE_TIME_USEC_PER_MINUTE (G_GINT64_CONSTANT (60000000))
49 #define GST_DATE_TIME_USEC_PER_SECOND (G_GINT64_CONSTANT (1000000))
50 #define GST_DATE_TIME_USEC_PER_MILLISECOND (G_GINT64_CONSTANT (1000))
56 volatile gint ref_count;
60 gst_date_time_new_from_gdatetime (GDateTime * dt)
67 gst_dt = g_slice_new (GstDateTime);
68 gst_dt->datetime = dt;
69 gst_dt->ref_count = 1;
74 * gst_date_time_get_year:
75 * @datetime: a #GstDateTime
77 * Returns the year of this #GstDateTime
79 * Return value: The year of this #GstDateTime
83 gst_date_time_get_year (const GstDateTime * datetime)
85 return g_date_time_get_year (datetime->datetime);
89 * gst_date_time_get_month:
90 * @datetime: a #GstDateTime
92 * Returns the month of this #GstDateTime. January is 1, February is 2, etc..
94 * Return value: The month of this #GstDateTime
98 gst_date_time_get_month (const GstDateTime * datetime)
100 return g_date_time_get_month (datetime->datetime);
104 * gst_date_time_get_day:
105 * @datetime: a #GstDateTime
107 * Returns the day of this #GstDateTime.
109 * Return value: The day of this #GstDateTime
113 gst_date_time_get_day (const GstDateTime * datetime)
115 return g_date_time_get_day_of_month (datetime->datetime);
119 * gst_date_time_get_hour:
120 * @datetime: a #GstDateTime
122 * Retrieves the hour of the day represented by @datetime in the gregorian
123 * calendar. The return is in the range of 0 to 23.
125 * Return value: the hour of the day
130 gst_date_time_get_hour (const GstDateTime * datetime)
132 return g_date_time_get_hour (datetime->datetime);
136 * gst_date_time_get_minute:
137 * @datetime: a #GstDateTime
139 * Retrieves the minute of the hour represented by @datetime in the gregorian
142 * Return value: the minute of the hour
147 gst_date_time_get_minute (const GstDateTime * datetime)
149 return g_date_time_get_minute (datetime->datetime);
153 * gst_date_time_get_second:
154 * @datetime: a #GstDateTime
156 * Retrieves the second of the minute represented by @datetime in the gregorian
159 * Return value: the second represented by @datetime
164 gst_date_time_get_second (const GstDateTime * datetime)
166 return g_date_time_get_second (datetime->datetime);
170 * gst_date_time_get_microsecond:
171 * @datetime: a #GstDateTime
173 * Retrieves the fractional part of the seconds in microseconds represented by
174 * @datetime in the gregorian calendar.
176 * Return value: the microsecond of the second
181 gst_date_time_get_microsecond (const GstDateTime * datetime)
183 return g_date_time_get_microsecond (datetime->datetime);
187 * gst_date_time_get_time_zone_offset:
188 * @datetime: a #GstDateTime
190 * Retrieves the offset from UTC in hours that the timezone specified
191 * by @datetime represents. Timezones ahead (to the east) of UTC have positive
192 * values, timezones before (to the west) of UTC have negative values.
193 * If @datetime represents UTC time, then the offset is zero.
195 * Return value: the offset from UTC in hours
199 gst_date_time_get_time_zone_offset (const GstDateTime * datetime)
201 return (g_date_time_get_utc_offset (datetime->datetime) /
202 G_USEC_PER_SEC) / 3600.0;
206 * gst_date_time_new_from_unix_epoch_local_time:
207 * @secs: seconds from the Unix epoch
209 * Creates a new #GstDateTime using the time since Jan 1, 1970 specified by
210 * @secs. The #GstDateTime is in the local timezone.
212 * Free-function: gst_date_time_unref
214 * Return value: (transfer full): the newly created #GstDateTime
219 gst_date_time_new_from_unix_epoch_local_time (gint64 secs)
222 gst_date_time_new_from_gdatetime (g_date_time_new_from_unix_local (secs));
226 * gst_date_time_new_from_unix_epoch_utc:
227 * @secs: seconds from the Unix epoch
229 * Creates a new #GstDateTime using the time since Jan 1, 1970 specified by
230 * @secs. The #GstDateTime is in the UTC timezone.
232 * Free-function: gst_date_time_unref
234 * Return value: (transfer full): the newly created #GstDateTime
239 gst_date_time_new_from_unix_epoch_utc (gint64 secs)
242 gst_date_time_new_from_gdatetime (g_date_time_new_from_unix_utc (secs));
246 * gst_date_time_new_local_time:
247 * @year: the gregorian year
248 * @month: the gregorian month
249 * @day: the day of the gregorian month
250 * @hour: the hour of the day
251 * @minute: the minute of the hour
252 * @seconds: the second of the minute
254 * Creates a new #GstDateTime using the date and times in the gregorian calendar
255 * in the local timezone.
257 * @year should be from 1 to 9999, @month should be from 1 to 12, @day from
258 * 1 to 31, @hour from 0 to 23, @minutes and @seconds from 0 to 59.
260 * Free-function: gst_date_time_unref
262 * Return value: (transfer full): the newly created #GstDateTime
267 gst_date_time_new_local_time (gint year, gint month, gint day, gint hour,
268 gint minute, gdouble seconds)
270 return gst_date_time_new_from_gdatetime (g_date_time_new_local (year, month,
271 day, hour, minute, seconds));
275 * gst_date_time_new_now_local_time:
277 * Creates a new #GstDateTime representing the current date and time.
279 * Free-function: gst_date_time_unref
281 * Return value: (transfer full): the newly created #GstDateTime which should
282 * be freed with gst_date_time_unref().
287 gst_date_time_new_now_local_time (void)
289 return gst_date_time_new_from_gdatetime (g_date_time_new_now_local ());
293 * gst_date_time_new_now_utc:
295 * Creates a new #GstDateTime that represents the current instant at Universal
298 * Free-function: gst_date_time_unref
300 * Return value: (transfer full): the newly created #GstDateTime which should
301 * be freed with gst_date_time_unref().
306 gst_date_time_new_now_utc (void)
308 return gst_date_time_new_from_gdatetime (g_date_time_new_now_utc ());
312 priv_gst_date_time_compare (gconstpointer dt1, gconstpointer dt2)
314 const GstDateTime *datetime1 = dt1;
315 const GstDateTime *datetime2 = dt2;
316 return g_date_time_compare (datetime1->datetime, datetime2->datetime);
321 * @tzoffset: Offset from UTC in hours.
322 * @year: the gregorian year
323 * @month: the gregorian month
324 * @day: the day of the gregorian month
325 * @hour: the hour of the day
326 * @minute: the minute of the hour
327 * @seconds: the second of the minute
329 * Creates a new #GstDateTime using the date and times in the gregorian calendar
330 * in the supplied timezone.
332 * @year should be from 1 to 9999, @month should be from 1 to 12, @day from
333 * 1 to 31, @hour from 0 to 23, @minutes and @seconds from 0 to 59.
335 * Note that @tzoffset is a float and was chosen so for being able to handle
336 * some fractional timezones, while it still keeps the readability of
337 * represeting it in hours for most timezones.
339 * Free-function: gst_date_time_unref
341 * Return value: (transfer full): the newly created #GstDateTime
346 gst_date_time_new (gfloat tzoffset, gint year, gint month, gint day, gint hour,
347 gint minute, gdouble seconds)
352 gint tzhour, tzminute;
354 tzhour = (gint) ABS (tzoffset);
355 tzminute = (gint) ((ABS (tzoffset) - tzhour) * 60);
357 g_snprintf (buf, 6, "%c%02d%02d", tzoffset >= 0 ? '+' : '-', tzhour,
360 tz = g_time_zone_new (buf);
362 dt = g_date_time_new (tz, year, month, day, hour, minute, seconds);
363 g_time_zone_unref (tz);
364 return gst_date_time_new_from_gdatetime (dt);
368 gst_date_time_free (GstDateTime * datetime)
370 g_date_time_unref (datetime->datetime);
371 g_slice_free (GstDateTime, datetime);
376 * @datetime: a #GstDateTime
378 * Atomically increments the reference count of @datetime by one.
380 * Return value: (transfer full): the reference @datetime
385 gst_date_time_ref (GstDateTime * datetime)
387 g_return_val_if_fail (datetime != NULL, NULL);
388 g_return_val_if_fail (datetime->ref_count > 0, NULL);
389 g_atomic_int_inc (&datetime->ref_count);
394 * gst_date_time_unref:
395 * @datetime: (transfer full): a #GstDateTime
397 * Atomically decrements the reference count of @datetime by one. When the
398 * reference count reaches zero, the structure is freed.
403 gst_date_time_unref (GstDateTime * datetime)
405 g_return_if_fail (datetime != NULL);
406 g_return_if_fail (datetime->ref_count > 0);
408 if (g_atomic_int_dec_and_test (&datetime->ref_count))
409 gst_date_time_free (datetime);