mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2024-12-26 15:36:14 +01:00
Add GDateTime to GLib
GDateTime is an opaque data type containing a date and time representation. It's immutable once created and reference counted. https://bugzilla.gnome.org/show_bug.cgi?id=50076 Based on the code by: Christian Hergert <chris@dronelabs.com> Signed-off-by: Emmanuele Bassi <ebassi@linux.intel.com>
This commit is contained in:
parent
3cee971db5
commit
e1f13ee9ed
@ -134,7 +134,8 @@ libglib_2_0_la_SOURCES = \
|
||||
gconvert.c \
|
||||
gdataset.c \
|
||||
gdatasetprivate.h \
|
||||
gdate.c \
|
||||
gdate.c \
|
||||
gdatetime.c \
|
||||
gdir.c \
|
||||
gerror.c \
|
||||
gfileutils.c \
|
||||
@ -142,11 +143,11 @@ libglib_2_0_la_SOURCES = \
|
||||
ghook.c \
|
||||
ghostutils.c \
|
||||
giochannel.c \
|
||||
gkeyfile.c \
|
||||
gkeyfile.c \
|
||||
glibintl.h \
|
||||
glib_trace.h \
|
||||
glist.c \
|
||||
gmain.c \
|
||||
gmain.c \
|
||||
gmappedfile.c \
|
||||
gmarkup.c \
|
||||
gmem.c \
|
||||
@ -233,6 +234,7 @@ glibsubinclude_HEADERS = \
|
||||
gconvert.h \
|
||||
gdataset.h \
|
||||
gdate.h \
|
||||
gdatetime.h \
|
||||
gdir.h \
|
||||
gerror.h \
|
||||
gfileutils.h \
|
||||
@ -277,7 +279,7 @@ glibsubinclude_HEADERS = \
|
||||
gtree.h \
|
||||
gtypes.h \
|
||||
gunicode.h \
|
||||
gurifuncs.h \
|
||||
gurifuncs.h \
|
||||
gutils.h \
|
||||
gvarianttype.h \
|
||||
gvariant.h \
|
||||
@ -289,7 +291,7 @@ install-data-local: install-ms-lib install-def-file
|
||||
echo "*** Old headers found in $(glibincludedir). You should remove the" ; \
|
||||
echo "*** contents of this directory and type 'make install' again." ; \
|
||||
false ; \
|
||||
fi
|
||||
fi
|
||||
|
||||
uninstall-local: uninstall-ms-lib uninstall-def-file
|
||||
|
||||
@ -400,8 +402,8 @@ bin_SCRIPTS = ${auto_config_binscripts}
|
||||
EXTRA_DIST += ${auto_config_binscripts}
|
||||
|
||||
CONFIGVARS = \
|
||||
"bindir" : "${bindir}", \
|
||||
"glib-version" : "${GLIB_VERSION}"
|
||||
"bindir" : "${bindir}", \
|
||||
"glib-version" : "${GLIB_VERSION}"
|
||||
|
||||
install-exec-hook:
|
||||
for sf in ${auto_config_binscripts} ; do \
|
||||
|
2227
glib/gdatetime.c
Normal file
2227
glib/gdatetime.c
Normal file
File diff suppressed because it is too large
Load Diff
190
glib/gdatetime.h
Normal file
190
glib/gdatetime.h
Normal file
@ -0,0 +1,190 @@
|
||||
/* gdatetime.h
|
||||
*
|
||||
* Copyright (C) 2009-2010 Christian Hergert <chris@dronelabs.com>
|
||||
*
|
||||
* This is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
|
||||
#error "Only <glib.h> can be included directly."
|
||||
#endif
|
||||
|
||||
#ifndef __G_DATE_TIME_H__
|
||||
#define __G_DATE_TIME_H__
|
||||
|
||||
#include <time.h>
|
||||
#include <glib.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
/**
|
||||
* G_TIME_SPAN_DAY:
|
||||
*
|
||||
* Evaluates to a time span of one day.
|
||||
*
|
||||
* Since: 2.26
|
||||
*/
|
||||
#define G_TIME_SPAN_DAY (G_GINT64_CONSTANT (86400000000))
|
||||
|
||||
/**
|
||||
* G_TIME_SPAN_HOUR:
|
||||
*
|
||||
* Evaluates to a time span of one hour.
|
||||
*
|
||||
* Since: 2.26
|
||||
*/
|
||||
#define G_TIME_SPAN_HOUR (G_GINT64_CONSTANT (3600000000))
|
||||
|
||||
/**
|
||||
* G_TIME_SPAN_MINUTE:
|
||||
*
|
||||
* Evaluates to a time span of one minute.
|
||||
*
|
||||
* Since: 2.26
|
||||
*/
|
||||
#define G_TIME_SPAN_MINUTE (G_GINT64_CONSTANT (60000000))
|
||||
|
||||
/**
|
||||
* G_TIME_SPAN_SECOND:
|
||||
*
|
||||
* Evaluates to a time span of one second.
|
||||
*
|
||||
* Since: 2.26
|
||||
*/
|
||||
#define G_TIME_SPAN_SECOND (G_GINT64_CONSTANT (1000000))
|
||||
|
||||
/**
|
||||
* G_TIME_SPAN_MILLISECOND:
|
||||
*
|
||||
* Evaluates to a time span of one millisecond.
|
||||
*
|
||||
* Since: 2.26
|
||||
*/
|
||||
#define G_TIME_SPAN_MILLISECOND (G_GINT64_CONSTANT (1000))
|
||||
|
||||
/**
|
||||
* GDateTime:
|
||||
*
|
||||
* <structname>GDateTime</structname> is an opaque structure whose members
|
||||
* cannot be accessed directly.
|
||||
*
|
||||
* Since: 2.26
|
||||
*/
|
||||
typedef struct _GDateTime GDateTime;
|
||||
|
||||
/**
|
||||
* GTimeSpan:
|
||||
*
|
||||
* A value representing an interval of time, in microseconds.
|
||||
*
|
||||
* Since: 2.26
|
||||
*/
|
||||
typedef gint64 GTimeSpan;
|
||||
|
||||
GDateTime * g_date_time_new_now (void);
|
||||
GDateTime * g_date_time_new_today (void);
|
||||
GDateTime * g_date_time_new_utc_now (void);
|
||||
GDateTime * g_date_time_new_from_date (gint year,
|
||||
gint month,
|
||||
gint day);
|
||||
GDateTime * g_date_time_new_from_epoch (gint64 secs);
|
||||
GDateTime * g_date_time_new_from_timeval (GTimeVal *tv);
|
||||
GDateTime * g_date_time_new_full (gint year,
|
||||
gint month,
|
||||
gint day,
|
||||
gint hour,
|
||||
gint minute,
|
||||
gint second,
|
||||
const gchar *timezone);
|
||||
|
||||
GDateTime * g_date_time_copy (const GDateTime *datetime);
|
||||
GDateTime * g_date_time_ref (GDateTime *datetime);
|
||||
void g_date_time_unref (GDateTime *datetime);
|
||||
|
||||
GDateTime * g_date_time_add (const GDateTime *datetime,
|
||||
GTimeSpan timespan);
|
||||
GDateTime * g_date_time_add_days (const GDateTime *datetime,
|
||||
gint days);
|
||||
GDateTime * g_date_time_add_hours (const GDateTime *datetime,
|
||||
gint hours);
|
||||
GDateTime * g_date_time_add_milliseconds (const GDateTime *datetime,
|
||||
gint milliseconds);
|
||||
GDateTime * g_date_time_add_minutes (const GDateTime *datetime,
|
||||
gint minutes);
|
||||
GDateTime * g_date_time_add_months (const GDateTime *datetime,
|
||||
gint months);
|
||||
GDateTime * g_date_time_add_seconds (const GDateTime *datetime,
|
||||
gint seconds);
|
||||
GDateTime * g_date_time_add_weeks (const GDateTime *datetime,
|
||||
gint weeks);
|
||||
GDateTime * g_date_time_add_years (const GDateTime *datetime,
|
||||
gint years);
|
||||
GDateTime * g_date_time_add_full (const GDateTime *datetime,
|
||||
gint years,
|
||||
gint months,
|
||||
gint days,
|
||||
gint hours,
|
||||
gint minutes,
|
||||
gint seconds);
|
||||
|
||||
GDateTime * g_date_time_day (const GDateTime *datetime);
|
||||
|
||||
gint g_date_time_compare (gconstpointer dt1,
|
||||
gconstpointer dt2);
|
||||
gboolean g_date_time_equal (gconstpointer dt1,
|
||||
gconstpointer dt2);
|
||||
guint g_date_time_hash (gconstpointer datetime);
|
||||
|
||||
GTimeSpan g_date_time_difference (const GDateTime *begin,
|
||||
const GDateTime *end);
|
||||
|
||||
void g_date_time_get_julian (const GDateTime *datetime,
|
||||
gint *period,
|
||||
gint *julian,
|
||||
gint *hour,
|
||||
gint *minute,
|
||||
gint *second);
|
||||
gint g_date_time_get_hour (const GDateTime *datetime);
|
||||
gint g_date_time_get_minute (const GDateTime *datetime);
|
||||
gint g_date_time_get_second (const GDateTime *datetime);
|
||||
gint g_date_time_get_millisecond (const GDateTime *datetime);
|
||||
gint g_date_time_get_microsecond (const GDateTime *datetime);
|
||||
gint g_date_time_get_day_of_week (const GDateTime *datetime);
|
||||
gint g_date_time_get_day_of_month (const GDateTime *datetime);
|
||||
gint g_date_time_get_day_of_year (const GDateTime *datetime);
|
||||
gint g_date_time_get_month (const GDateTime *datetime);
|
||||
gint g_date_time_get_year (const GDateTime *datetime);
|
||||
void g_date_time_get_dmy (const GDateTime *datetime,
|
||||
gint *day,
|
||||
gint *month,
|
||||
gint *year);
|
||||
|
||||
GTimeSpan g_date_time_get_utc_offset (const GDateTime *datetime);
|
||||
G_CONST_RETURN gchar *g_date_time_get_timezone_name (const GDateTime *datetime);
|
||||
|
||||
gboolean g_date_time_is_leap_year (const GDateTime *datetime);
|
||||
gboolean g_date_time_is_daylight_savings (const GDateTime *datetime);
|
||||
|
||||
GDateTime * g_date_time_to_local (const GDateTime *datetime);
|
||||
gint64 g_date_time_to_epoch (const GDateTime *datetime);
|
||||
void g_date_time_to_timeval (const GDateTime *datetime,
|
||||
GTimeVal *tv);
|
||||
GDateTime * g_date_time_to_utc (const GDateTime *datetime);
|
||||
gchar * g_date_time_printf (const GDateTime *datetime,
|
||||
const gchar *format) G_GNUC_MALLOC;
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* __G_DATE_TIME_H__ */
|
@ -43,6 +43,7 @@
|
||||
#include <glib/gconvert.h>
|
||||
#include <glib/gdataset.h>
|
||||
#include <glib/gdate.h>
|
||||
#include <glib/gdatetime.h>
|
||||
#include <glib/gdir.h>
|
||||
#include <glib/gerror.h>
|
||||
#include <glib/gfileutils.h>
|
||||
|
@ -323,6 +323,57 @@ g_date_valid_year G_GNUC_CONST
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if IN_HEADER(__G_DATE_TIME_H__)
|
||||
#if IN_FILE(__G_DATE_TIME_C__)
|
||||
g_date_time_add
|
||||
g_date_time_add_days
|
||||
g_date_time_add_full
|
||||
g_date_time_add_hours
|
||||
g_date_time_add_milliseconds
|
||||
g_date_time_add_minutes
|
||||
g_date_time_add_months
|
||||
g_date_time_add_seconds
|
||||
g_date_time_add_weeks
|
||||
g_date_time_add_years
|
||||
g_date_time_compare
|
||||
g_date_time_copy
|
||||
g_date_time_day
|
||||
g_date_time_difference
|
||||
g_date_time_equal
|
||||
g_date_time_get_day_of_month
|
||||
g_date_time_get_day_of_week
|
||||
g_date_time_get_day_of_year
|
||||
g_date_time_get_dmy
|
||||
g_date_time_get_hour
|
||||
g_date_time_get_julian
|
||||
g_date_time_get_microsecond
|
||||
g_date_time_get_millisecond
|
||||
g_date_time_get_minute
|
||||
g_date_time_get_month
|
||||
g_date_time_get_second
|
||||
g_date_time_get_timezone_name
|
||||
g_date_time_get_utc_offset
|
||||
g_date_time_get_year
|
||||
g_date_time_hash
|
||||
g_date_time_is_daylight_savings
|
||||
g_date_time_is_leap_year
|
||||
g_date_time_new_from_date
|
||||
g_date_time_new_from_epoch
|
||||
g_date_time_new_from_timeval
|
||||
g_date_time_new_full
|
||||
g_date_time_new_now
|
||||
g_date_time_new_today
|
||||
g_date_time_new_utc_now
|
||||
g_date_time_printf G_GNUC_MALLOC
|
||||
g_date_time_ref
|
||||
g_date_time_to_local
|
||||
g_date_time_to_epoch
|
||||
g_date_time_to_timeval
|
||||
g_date_time_to_utc
|
||||
g_date_time_unref
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if IN_HEADER(__G_DIR_H__)
|
||||
#if IN_FILE(__G_DIR_C__)
|
||||
g_dir_close
|
||||
|
1
glib/tests/.gitignore
vendored
1
glib/tests/.gitignore
vendored
@ -8,6 +8,7 @@ date
|
||||
dir
|
||||
error
|
||||
fileutils
|
||||
gdatetime
|
||||
gvariant
|
||||
hash
|
||||
hostutils
|
||||
|
@ -9,7 +9,6 @@ INCLUDES = \
|
||||
noinst_PROGRAMS = $(TEST_PROGS)
|
||||
progs_ldadd = $(top_builddir)/glib/libglib-2.0.la
|
||||
|
||||
|
||||
TEST_PROGS += testing
|
||||
testing_SOURCES = testing.c
|
||||
testing_LDADD = $(progs_ldadd)
|
||||
@ -145,6 +144,10 @@ error_LDADD = $(progs_ldadd)
|
||||
TEST_PROGS += bookmarkfile
|
||||
bookmarkfile_LDADD = $(progs_ldadd)
|
||||
|
||||
TEST_PROGS += gdatetime
|
||||
gdatetime_SOURCES = gdatetime.c
|
||||
gdatetime_LDADD = $(progs_ldadd)
|
||||
|
||||
if OS_UNIX
|
||||
|
||||
# some testing of gtester funcitonality
|
||||
|
962
glib/tests/gdatetime.c
Normal file
962
glib/tests/gdatetime.c
Normal file
@ -0,0 +1,962 @@
|
||||
/* gdatetime-tests.c
|
||||
*
|
||||
* Copyright (C) 2009-2010 Christian Hergert <chris@dronelabs.com>
|
||||
*
|
||||
* This is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <glib.h>
|
||||
#include <glib/gstdio.h>
|
||||
|
||||
#define ASSERT_DATE(dt,y,m,d) G_STMT_START { \
|
||||
g_assert_cmpint ((y), ==, g_date_time_get_year ((dt))); \
|
||||
g_assert_cmpint ((m), ==, g_date_time_get_month ((dt))); \
|
||||
g_assert_cmpint ((d), ==, g_date_time_get_day_of_month ((dt))); \
|
||||
} G_STMT_END
|
||||
#define ASSERT_TIME(dt,H,M,S) G_STMT_START { \
|
||||
g_assert_cmpint ((H), ==, g_date_time_get_hour ((dt))); \
|
||||
g_assert_cmpint ((M), ==, g_date_time_get_minute ((dt))); \
|
||||
g_assert_cmpint ((S), ==, g_date_time_get_second ((dt))); \
|
||||
} G_STMT_END
|
||||
|
||||
static void
|
||||
get_localtime_tm (time_t time_,
|
||||
struct tm *retval)
|
||||
{
|
||||
#ifdef HAVE_LOCALTIME_R
|
||||
localtime_r (&time_, retval);
|
||||
#else
|
||||
{
|
||||
struct tm *ptm = localtime (&time_);
|
||||
|
||||
if (ptm == NULL)
|
||||
{
|
||||
/* Happens at least in Microsoft's C library if you pass a
|
||||
* negative time_t. Use 2000-01-01 as default date.
|
||||
*/
|
||||
#ifndef G_DISABLE_CHECKS
|
||||
g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, "ptm != NULL");
|
||||
#endif
|
||||
|
||||
tm.tm_mon = 0;
|
||||
tm.tm_mday = 1;
|
||||
tm.tm_year = 100;
|
||||
}
|
||||
else
|
||||
memcpy ((void *) retval, (void *) ptm, sizeof (struct tm));
|
||||
}
|
||||
#endif /* HAVE_LOCALTIME_R */
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_now (void)
|
||||
{
|
||||
GDateTime *dt;
|
||||
struct tm tm;
|
||||
|
||||
memset (&tm, 0, sizeof (tm));
|
||||
get_localtime_tm (time (NULL), &tm);
|
||||
|
||||
dt = g_date_time_new_now ();
|
||||
g_assert_cmpint (g_date_time_get_year (dt), ==, 1900 + tm.tm_year);
|
||||
g_assert_cmpint (g_date_time_get_month (dt), ==, 1 + tm.tm_mon);
|
||||
g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, tm.tm_mday);
|
||||
g_assert_cmpint (g_date_time_get_hour (dt), ==, tm.tm_hour);
|
||||
g_assert_cmpint (g_date_time_get_minute (dt), ==, tm.tm_min);
|
||||
g_assert_cmpint (g_date_time_get_second (dt), ==, tm.tm_sec);
|
||||
g_date_time_unref (dt);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_today (void)
|
||||
{
|
||||
GDateTime *dt;
|
||||
struct tm tm;
|
||||
|
||||
memset (&tm, 0, sizeof (tm));
|
||||
get_localtime_tm (time (NULL), &tm);
|
||||
|
||||
dt = g_date_time_new_today ();
|
||||
g_assert_cmpint (g_date_time_get_year (dt), ==, 1900 + tm.tm_year);
|
||||
g_assert_cmpint (g_date_time_get_month (dt), ==, 1 + tm.tm_mon);
|
||||
g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, tm.tm_mday);
|
||||
g_assert_cmpint (g_date_time_get_hour (dt), ==, 0);
|
||||
g_assert_cmpint (g_date_time_get_minute (dt), ==, 0);
|
||||
g_assert_cmpint (g_date_time_get_second (dt), ==, 0);
|
||||
g_assert_cmpint (g_date_time_get_millisecond (dt), ==, 0);
|
||||
g_date_time_unref (dt);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_new_from_epoch (void)
|
||||
{
|
||||
GDateTime *dt;
|
||||
struct tm tm;
|
||||
time_t t;
|
||||
|
||||
memset (&tm, 0, sizeof (tm));
|
||||
t = time (NULL);
|
||||
get_localtime_tm (t, &tm);
|
||||
|
||||
dt = g_date_time_new_from_epoch (t);
|
||||
g_assert_cmpint (g_date_time_get_year (dt), ==, 1900 + tm.tm_year);
|
||||
g_assert_cmpint (g_date_time_get_month (dt), ==, 1 + tm.tm_mon);
|
||||
g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, tm.tm_mday);
|
||||
g_assert_cmpint (g_date_time_get_hour (dt), ==, tm.tm_hour);
|
||||
g_assert_cmpint (g_date_time_get_minute (dt), ==, tm.tm_min);
|
||||
g_assert_cmpint (g_date_time_get_second (dt), ==, tm.tm_sec);
|
||||
g_date_time_unref (dt);
|
||||
|
||||
memset (&tm, 0, sizeof (tm));
|
||||
tm.tm_year = 70;
|
||||
tm.tm_mday = 1;
|
||||
tm.tm_mon = 0;
|
||||
tm.tm_hour = 0;
|
||||
tm.tm_min = 0;
|
||||
tm.tm_sec = 0;
|
||||
t = mktime (&tm);
|
||||
|
||||
dt = g_date_time_new_from_epoch (t);
|
||||
g_assert_cmpint (g_date_time_get_year (dt), ==, 1970);
|
||||
g_assert_cmpint (g_date_time_get_month (dt), ==, 1);
|
||||
g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 1);
|
||||
g_assert_cmpint (g_date_time_get_hour (dt), ==, 0);
|
||||
g_assert_cmpint (g_date_time_get_minute (dt), ==, 0);
|
||||
g_assert_cmpint (g_date_time_get_second (dt), ==, 0);
|
||||
g_date_time_unref (dt);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_is_leap_year (void)
|
||||
{
|
||||
GDateTime *dt;
|
||||
gint i;
|
||||
|
||||
for (i = 1; i <= 3000; i++)
|
||||
{
|
||||
dt = g_date_time_new_from_date (i, 1, 1);
|
||||
if ((((i % 4) == 0) && ((i % 100) != 0)) || ((i % 400) == 0))
|
||||
g_assert (g_date_time_is_leap_year (dt));
|
||||
else
|
||||
g_assert (!g_date_time_is_leap_year (dt));
|
||||
g_date_time_unref (dt);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_compare (void)
|
||||
{
|
||||
GDateTime *dt1, *dt2;
|
||||
gint i;
|
||||
|
||||
dt1 = g_date_time_new_from_date (2000, 1, 1);
|
||||
|
||||
for (i = 1; i < 2000; i++)
|
||||
{
|
||||
dt2 = g_date_time_new_from_date (i, 12, 31);
|
||||
g_assert_cmpint (1, ==, g_date_time_compare (dt1, dt2));
|
||||
g_date_time_unref (dt2);
|
||||
}
|
||||
|
||||
dt2 = g_date_time_new_full (1999, 12, 31, 23, 59, 59, NULL);
|
||||
g_assert_cmpint (1, ==, g_date_time_compare (dt1, dt2));
|
||||
g_date_time_unref (dt2);
|
||||
|
||||
dt2 = g_date_time_new_full (2000, 1, 1, 0, 0, 1, NULL);
|
||||
g_assert_cmpint (-1, ==, g_date_time_compare (dt1, dt2));
|
||||
g_date_time_unref (dt2);
|
||||
|
||||
dt2 = g_date_time_new_full (2000, 1, 1, 0, 0, 0, NULL);
|
||||
g_assert_cmpint (0, ==, g_date_time_compare (dt1, dt2));
|
||||
g_date_time_unref (dt2);
|
||||
|
||||
g_date_time_unref (dt1);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_copy (void)
|
||||
{
|
||||
GDateTime *dt1, *dt2;
|
||||
|
||||
dt1 = g_date_time_new_now ();
|
||||
dt2 = g_date_time_copy (dt1);
|
||||
g_assert (dt1 != NULL);
|
||||
g_assert (dt2 != NULL);
|
||||
g_assert_cmpint (0, ==, g_date_time_compare (dt1, dt2));
|
||||
g_date_time_unref (dt1);
|
||||
g_date_time_unref (dt2);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_date (void)
|
||||
{
|
||||
GDateTime *dt1, *dt2;
|
||||
|
||||
dt1 = g_date_time_new_full (2009, 10, 19, 13, 0, 55, NULL);
|
||||
dt2 = g_date_time_day (dt1);
|
||||
g_assert (dt1 != NULL);
|
||||
g_assert (dt2 != NULL);
|
||||
g_assert_cmpint (2009, ==, g_date_time_get_year (dt2));
|
||||
g_assert_cmpint (10, ==, g_date_time_get_month (dt2));
|
||||
g_assert_cmpint (19, ==, g_date_time_get_day_of_month (dt2));
|
||||
g_assert_cmpint (0, ==, g_date_time_get_hour (dt2));
|
||||
g_assert_cmpint (0, ==, g_date_time_get_minute (dt2));
|
||||
g_assert_cmpint (0, ==, g_date_time_get_second (dt2));
|
||||
g_date_time_unref (dt1);
|
||||
g_date_time_unref (dt2);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_equal (void)
|
||||
{
|
||||
GDateTime *dt1, *dt2;
|
||||
|
||||
dt1 = g_date_time_new_from_date (2009, 10, 19);
|
||||
dt2 = g_date_time_new_from_date (2009, 10, 19);
|
||||
g_assert (g_date_time_equal (dt1, dt2));
|
||||
g_date_time_unref (dt1);
|
||||
g_date_time_unref (dt2);
|
||||
|
||||
dt1 = g_date_time_new_from_date (2009, 10, 18);
|
||||
dt2 = g_date_time_new_from_date (2009, 10, 19);
|
||||
g_assert (!g_date_time_equal (dt1, dt2));
|
||||
g_date_time_unref (dt1);
|
||||
g_date_time_unref (dt2);
|
||||
|
||||
/* America/Recife is GMT-3 and is not in DST for this time */
|
||||
dt1 = g_date_time_new_full (2010, 5, 24, 8, 0, 0, "America/Recife");
|
||||
dt2 = g_date_time_new_full (2010, 5, 24, 11, 0, 0, "UTC");
|
||||
g_assert (g_date_time_equal (dt1, dt2));
|
||||
g_date_time_unref (dt1);
|
||||
g_date_time_unref (dt2);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_get_day_of_week (void)
|
||||
{
|
||||
GDateTime *dt;
|
||||
|
||||
dt = g_date_time_new_from_date (2009, 10, 19);
|
||||
g_assert_cmpint (1, ==, g_date_time_get_day_of_week (dt));
|
||||
g_date_time_unref (dt);
|
||||
|
||||
dt = g_date_time_new_from_date (2000, 10, 1);
|
||||
g_assert_cmpint (7, ==, g_date_time_get_day_of_week (dt));
|
||||
g_date_time_unref (dt);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_get_day_of_month (void)
|
||||
{
|
||||
GDateTime *dt;
|
||||
|
||||
dt = g_date_time_new_from_date (2009, 10, 19);
|
||||
g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 19);
|
||||
g_date_time_unref (dt);
|
||||
|
||||
dt = g_date_time_new_from_date (1400, 3, 12);
|
||||
g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 12);
|
||||
g_date_time_unref (dt);
|
||||
|
||||
dt = g_date_time_new_from_date (1800, 12, 31);
|
||||
g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 31);
|
||||
g_date_time_unref (dt);
|
||||
|
||||
dt = g_date_time_new_from_date (1000, 1, 1);
|
||||
g_assert_cmpint (g_date_time_get_day_of_month (dt), ==, 1);
|
||||
g_date_time_unref (dt);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_get_dmy (void)
|
||||
{
|
||||
GDateTime *dt;
|
||||
struct tm tm;
|
||||
time_t t;
|
||||
gint d, m, y;
|
||||
|
||||
t = time (NULL);
|
||||
memset (&tm, 0, sizeof (struct tm));
|
||||
get_localtime_tm (t, &tm);
|
||||
|
||||
dt = g_date_time_new_from_epoch (t);
|
||||
g_date_time_get_dmy(dt, &d, &m, &y);
|
||||
g_assert_cmpint(y, ==, tm.tm_year + 1900);
|
||||
g_assert_cmpint(m, ==, tm.tm_mon + 1);
|
||||
g_assert_cmpint(d, ==, tm.tm_mday);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_get_hour (void)
|
||||
{
|
||||
GDateTime *dt;
|
||||
|
||||
dt = g_date_time_new_full (2009, 10, 19, 15, 13, 11, NULL);
|
||||
g_assert_cmpint (15, ==, g_date_time_get_hour (dt));
|
||||
g_date_time_unref (dt);
|
||||
|
||||
dt = g_date_time_new_full (100, 10, 19, 1, 0, 0, NULL);
|
||||
g_assert_cmpint (1, ==, g_date_time_get_hour (dt));
|
||||
g_date_time_unref (dt);
|
||||
|
||||
dt = g_date_time_new_full (100, 10, 19, 0, 0, 0, NULL);
|
||||
g_assert_cmpint (0, ==, g_date_time_get_hour (dt));
|
||||
g_date_time_unref (dt);
|
||||
|
||||
dt = g_date_time_new_full (100, 10, 1, 23, 59, 59, NULL);
|
||||
g_assert_cmpint (23, ==, g_date_time_get_hour (dt));
|
||||
g_date_time_unref (dt);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_get_julian (void)
|
||||
{
|
||||
GDateTime *dt;
|
||||
gint period, julian, hour = 1, min = 1, sec = 1;
|
||||
|
||||
dt = g_date_time_new_from_date (1984, 8, 16);
|
||||
g_date_time_get_julian (dt, &period, &julian, &hour, &min, &sec);
|
||||
g_assert_cmpint (period, ==, 0);
|
||||
g_assert_cmpint (julian, ==, 2445929);
|
||||
g_assert_cmpint (hour, ==, 0);
|
||||
g_assert_cmpint (min, ==, 0);
|
||||
g_assert_cmpint (sec, ==, 0);
|
||||
g_date_time_unref (dt);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_get_microsecond (void)
|
||||
{
|
||||
GTimeVal tv;
|
||||
GDateTime *dt;
|
||||
|
||||
g_get_current_time (&tv);
|
||||
dt = g_date_time_new_from_timeval (&tv);
|
||||
g_assert_cmpint (tv.tv_usec, ==, g_date_time_get_microsecond (dt));
|
||||
g_date_time_unref (dt);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_get_millisecond (void)
|
||||
{
|
||||
GTimeVal tv;
|
||||
GDateTime *dt;
|
||||
|
||||
g_get_current_time (&tv);
|
||||
dt = g_date_time_new_from_timeval (&tv);
|
||||
g_assert_cmpint ((tv.tv_usec/1000), ==, g_date_time_get_millisecond (dt));
|
||||
g_date_time_unref (dt);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_get_year (void)
|
||||
{
|
||||
GDateTime *dt;
|
||||
|
||||
dt = g_date_time_new_from_date (2009, 1, 1);
|
||||
g_assert_cmpint (2009, ==, g_date_time_get_year (dt));
|
||||
g_date_time_unref (dt);
|
||||
|
||||
dt = g_date_time_new_from_date (1, 1, 1);
|
||||
g_assert_cmpint (1, ==, g_date_time_get_year (dt));
|
||||
g_date_time_unref (dt);
|
||||
|
||||
dt = g_date_time_new_from_date (13, 1, 1);
|
||||
g_assert_cmpint (13, ==, g_date_time_get_year (dt));
|
||||
g_date_time_unref (dt);
|
||||
|
||||
dt = g_date_time_new_from_date (3000, 1, 1);
|
||||
g_assert_cmpint (3000, ==, g_date_time_get_year (dt));
|
||||
g_date_time_unref (dt);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_hash (void)
|
||||
{
|
||||
GHashTable *h;
|
||||
|
||||
h = g_hash_table_new_full (g_date_time_hash, g_date_time_equal,
|
||||
(GDestroyNotify)g_date_time_unref,
|
||||
NULL);
|
||||
g_hash_table_insert (h, g_date_time_new_now (), NULL);
|
||||
g_hash_table_remove_all (h);
|
||||
g_hash_table_destroy (h);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_new_from_timeval (void)
|
||||
{
|
||||
GDateTime *dt;
|
||||
GTimeVal tv, tv2;
|
||||
|
||||
g_get_current_time (&tv);
|
||||
dt = g_date_time_new_from_timeval (&tv);
|
||||
g_date_time_to_timeval (dt, &tv2);
|
||||
g_assert_cmpint (tv.tv_sec, ==, tv2.tv_sec);
|
||||
g_assert_cmpint (tv.tv_usec, ==, tv2.tv_usec);
|
||||
g_date_time_unref (dt);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_to_epoch (void)
|
||||
{
|
||||
GDateTime *dt;
|
||||
time_t t;
|
||||
|
||||
t = time (NULL);
|
||||
dt = g_date_time_new_from_epoch (t);
|
||||
g_assert_cmpint (g_date_time_to_epoch (dt), ==, t);
|
||||
g_date_time_unref (dt);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_add_years (void)
|
||||
{
|
||||
GDateTime *dt, *dt2;
|
||||
|
||||
dt = g_date_time_new_from_date (2009, 10, 19);
|
||||
dt2 = g_date_time_add_years (dt, 1);
|
||||
g_assert_cmpint (2010, ==, g_date_time_get_year (dt2));
|
||||
g_date_time_unref (dt);
|
||||
g_date_time_unref (dt2);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_add_months (void)
|
||||
{
|
||||
#define TEST_ADD_MONTHS(y,m,d,a,ny,nm,nd) G_STMT_START { \
|
||||
GDateTime *dt, *dt2; \
|
||||
dt = g_date_time_new_from_date (y, m, d); \
|
||||
dt2 = g_date_time_add_months (dt, a); \
|
||||
ASSERT_DATE (dt2, ny, nm, nd); \
|
||||
g_date_time_unref (dt); \
|
||||
g_date_time_unref (dt2); \
|
||||
} G_STMT_END
|
||||
|
||||
TEST_ADD_MONTHS (2009, 12, 31, 1, 2010, 1, 31);
|
||||
TEST_ADD_MONTHS (2009, 12, 31, 1, 2010, 1, 31);
|
||||
TEST_ADD_MONTHS (2009, 6, 15, 1, 2009, 7, 15);
|
||||
TEST_ADD_MONTHS (1400, 3, 1, 1, 1400, 4, 1);
|
||||
TEST_ADD_MONTHS (1400, 1, 31, 1, 1400, 2, 28);
|
||||
TEST_ADD_MONTHS (1400, 1, 31, 7200, 2000, 1, 31);
|
||||
TEST_ADD_MONTHS (2008, 2, 29, 12, 2009, 2, 28);
|
||||
TEST_ADD_MONTHS (2000, 8, 16, -5, 2000, 3, 16);
|
||||
TEST_ADD_MONTHS (2000, 8, 16, -12, 1999, 8, 16);
|
||||
TEST_ADD_MONTHS (2011, 2, 1, -13, 2010, 1, 1);
|
||||
TEST_ADD_MONTHS (1776, 7, 4, 1200, 1876, 7, 4);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_add_days (void)
|
||||
{
|
||||
#define TEST_ADD_DAYS(y,m,d,a,ny,nm,nd) G_STMT_START { \
|
||||
GDateTime *dt, *dt2; \
|
||||
dt = g_date_time_new_from_date (y, m, d); \
|
||||
dt2 = g_date_time_add_days (dt, a); \
|
||||
g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
|
||||
g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
|
||||
g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
|
||||
g_date_time_unref (dt); \
|
||||
g_date_time_unref (dt2); \
|
||||
} G_STMT_END
|
||||
|
||||
TEST_ADD_DAYS (2009, 1, 31, 1, 2009, 2, 1);
|
||||
TEST_ADD_DAYS (2009, 2, 1, -1, 2009, 1, 31);
|
||||
TEST_ADD_DAYS (2008, 2, 28, 1, 2008, 2, 29);
|
||||
TEST_ADD_DAYS (2008, 12, 31, 1, 2009, 1, 1);
|
||||
TEST_ADD_DAYS (1, 1, 1, 1, 1, 1, 2);
|
||||
TEST_ADD_DAYS (1955, 5, 24, 10, 1955, 6, 3);
|
||||
TEST_ADD_DAYS (1955, 5, 24, -10, 1955, 5, 14);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_add_weeks (void)
|
||||
{
|
||||
#define TEST_ADD_WEEKS(y,m,d,a,ny,nm,nd) G_STMT_START { \
|
||||
GDateTime *dt, *dt2; \
|
||||
dt = g_date_time_new_from_date (y, m, d); \
|
||||
dt2 = g_date_time_add_weeks (dt, a); \
|
||||
g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
|
||||
g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
|
||||
g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
|
||||
g_date_time_unref (dt); \
|
||||
g_date_time_unref (dt2); \
|
||||
} G_STMT_END
|
||||
|
||||
TEST_ADD_WEEKS (2009, 1, 1, 1, 2009, 1, 8);
|
||||
TEST_ADD_WEEKS (2009, 8, 30, 1, 2009, 9, 6);
|
||||
TEST_ADD_WEEKS (2009, 12, 31, 1, 2010, 1, 7);
|
||||
TEST_ADD_WEEKS (2009, 1, 1, -1, 2008, 12, 25);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_add_hours (void)
|
||||
{
|
||||
#define TEST_ADD_HOURS(y,m,d,h,mi,s,a,ny,nm,nd,nh,nmi,ns) G_STMT_START { \
|
||||
GDateTime *dt, *dt2; \
|
||||
dt = g_date_time_new_full (y, m, d, h, mi, s, NULL); \
|
||||
dt2 = g_date_time_add_hours (dt, a); \
|
||||
g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
|
||||
g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
|
||||
g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
|
||||
g_assert_cmpint (nh, ==, g_date_time_get_hour (dt2)); \
|
||||
g_assert_cmpint (nmi, ==, g_date_time_get_minute (dt2)); \
|
||||
g_assert_cmpint (ns, ==, g_date_time_get_second (dt2)); \
|
||||
g_date_time_unref (dt); \
|
||||
g_date_time_unref (dt2); \
|
||||
} G_STMT_END
|
||||
|
||||
TEST_ADD_HOURS (2009, 1, 1, 0, 0, 0, 1, 2009, 1, 1, 1, 0, 0);
|
||||
TEST_ADD_HOURS (2008, 12, 31, 23, 0, 0, 1, 2009, 1, 1, 0, 0, 0);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_add_full (void)
|
||||
{
|
||||
#define TEST_ADD_FULL(y,m,d,h,mi,s,ay,am,ad,ah,ami,as,ny,nm,nd,nh,nmi,ns) G_STMT_START { \
|
||||
GDateTime *dt, *dt2; \
|
||||
dt = g_date_time_new_full (y, m, d, h, mi, s, NULL); \
|
||||
dt2 = g_date_time_add_full (dt, ay, am, ad, ah, ami, as); \
|
||||
g_assert_cmpint (ny, ==, g_date_time_get_year (dt2)); \
|
||||
g_assert_cmpint (nm, ==, g_date_time_get_month (dt2)); \
|
||||
g_assert_cmpint (nd, ==, g_date_time_get_day_of_month (dt2)); \
|
||||
g_assert_cmpint (nh, ==, g_date_time_get_hour (dt2)); \
|
||||
g_assert_cmpint (nmi, ==, g_date_time_get_minute (dt2)); \
|
||||
g_assert_cmpint (ns, ==, g_date_time_get_second (dt2)); \
|
||||
g_date_time_unref (dt); \
|
||||
g_date_time_unref (dt2); \
|
||||
} G_STMT_END
|
||||
|
||||
TEST_ADD_FULL (2009, 10, 21, 0, 0, 0,
|
||||
1, 1, 1, 1, 1, 1,
|
||||
2010, 11, 22, 1, 1, 1);
|
||||
TEST_ADD_FULL (2000, 1, 1, 1, 1, 1,
|
||||
0, 1, 0, 0, 0, 0,
|
||||
2000, 2, 1, 1, 1, 1);
|
||||
TEST_ADD_FULL (2000, 1, 1, 0, 0, 0,
|
||||
-1, 1, 0, 0, 0, 0,
|
||||
1999, 2, 1, 0, 0, 0);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_add_milliseconds (void)
|
||||
{
|
||||
#define TEST_ADD_MILLISECOND(i,o) G_STMT_START { \
|
||||
GDateTime *dt, *dt2; \
|
||||
dt = g_date_time_new_from_date (2000, 1, 1); \
|
||||
dt2 = g_date_time_add_milliseconds (dt, i); \
|
||||
g_assert_cmpint (o, ==, g_date_time_get_millisecond (dt2)); \
|
||||
g_date_time_unref (dt); \
|
||||
g_date_time_unref (dt2); \
|
||||
} G_STMT_END
|
||||
|
||||
TEST_ADD_MILLISECOND (199, 199);
|
||||
TEST_ADD_MILLISECOND (10001, 1);
|
||||
TEST_ADD_MILLISECOND (22201, 201);
|
||||
TEST_ADD_MILLISECOND (-1000, 0);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_add_minutes (void)
|
||||
{
|
||||
#define TEST_ADD_MINUTES(i,o) G_STMT_START { \
|
||||
GDateTime *dt, *dt2; \
|
||||
dt = g_date_time_new_from_date (2000, 1, 1); \
|
||||
dt2 = g_date_time_add_minutes (dt, i); \
|
||||
g_assert_cmpint (o, ==, g_date_time_get_minute (dt2)); \
|
||||
g_date_time_unref (dt); \
|
||||
g_date_time_unref (dt2); \
|
||||
} G_STMT_END
|
||||
|
||||
TEST_ADD_MINUTES (60, 0);
|
||||
TEST_ADD_MINUTES (100, 40);
|
||||
TEST_ADD_MINUTES (5, 5);
|
||||
TEST_ADD_MINUTES (86401, 1);
|
||||
TEST_ADD_MINUTES (-86401, 59);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_add_seconds (void)
|
||||
{
|
||||
#define TEST_ADD_SECONDS(i,o) G_STMT_START { \
|
||||
GDateTime *dt, *dt2; \
|
||||
dt = g_date_time_new_from_date (2000, 1, 1); \
|
||||
dt2 = g_date_time_add_seconds (dt, i); \
|
||||
g_assert_cmpint (o, ==, g_date_time_get_second (dt2)); \
|
||||
g_date_time_unref (dt); \
|
||||
g_date_time_unref (dt2); \
|
||||
} G_STMT_END
|
||||
|
||||
TEST_ADD_SECONDS (1, 1);
|
||||
TEST_ADD_SECONDS (60, 0);
|
||||
TEST_ADD_SECONDS (61, 1);
|
||||
TEST_ADD_SECONDS (120, 0);
|
||||
TEST_ADD_SECONDS (-61, 59);
|
||||
TEST_ADD_SECONDS (86401, 1);
|
||||
TEST_ADD_SECONDS (-86401, 59);
|
||||
TEST_ADD_SECONDS (-31, 29);
|
||||
TEST_ADD_SECONDS (13, 13);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_diff (void)
|
||||
{
|
||||
#define TEST_DIFF(y,m,d,y2,m2,d2,u) G_STMT_START { \
|
||||
GDateTime *dt1, *dt2; \
|
||||
GTimeSpan ts = 0; \
|
||||
dt1 = g_date_time_new_from_date (y, m, d); \
|
||||
dt2 = g_date_time_new_from_date (y2, m2, d2); \
|
||||
ts = g_date_time_difference (dt1, dt2); \
|
||||
g_assert_cmpint (ts, ==, u); \
|
||||
g_date_time_unref (dt1); \
|
||||
g_date_time_unref (dt2); \
|
||||
} G_STMT_END
|
||||
|
||||
TEST_DIFF (2009, 1, 1, 2009, 2, 1, G_TIME_SPAN_DAY * 31);
|
||||
TEST_DIFF (2009, 1, 1, 2010, 1, 1, G_TIME_SPAN_DAY * 365);
|
||||
TEST_DIFF (2008, 2, 28, 2008, 2, 29, G_TIME_SPAN_DAY);
|
||||
TEST_DIFF (2008, 2, 29, 2008, 2, 28, -G_TIME_SPAN_DAY);
|
||||
|
||||
/* TODO: Add usec tests */
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_get_minute (void)
|
||||
{
|
||||
GDateTime *dt;
|
||||
|
||||
dt = g_date_time_new_full (2009, 12, 1, 1, 31, 0, NULL);
|
||||
g_assert_cmpint (31, ==, g_date_time_get_minute (dt));
|
||||
g_date_time_unref (dt);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_get_month (void)
|
||||
{
|
||||
GDateTime *dt;
|
||||
|
||||
dt = g_date_time_new_full (2009, 12, 1, 1, 31, 0, NULL);
|
||||
g_assert_cmpint (12, ==, g_date_time_get_month (dt));
|
||||
g_date_time_unref (dt);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_get_second (void)
|
||||
{
|
||||
GDateTime *dt;
|
||||
|
||||
dt = g_date_time_new_full (2009, 12, 1, 1, 31, 44, NULL);
|
||||
g_assert_cmpint (44, ==, g_date_time_get_second (dt));
|
||||
g_date_time_unref (dt);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_new_from_date (void)
|
||||
{
|
||||
GDateTime *dt;
|
||||
|
||||
dt = g_date_time_new_from_date (2009, 12, 11);
|
||||
g_assert_cmpint (2009, ==, g_date_time_get_year (dt));
|
||||
g_assert_cmpint (12, ==, g_date_time_get_month (dt));
|
||||
g_assert_cmpint (11, ==, g_date_time_get_day_of_month (dt));
|
||||
g_date_time_unref (dt);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_new_full (void)
|
||||
{
|
||||
GDateTime *dt;
|
||||
|
||||
dt = g_date_time_new_full (2009, 12, 11, 12, 11, 10, NULL);
|
||||
g_assert_cmpint (2009, ==, g_date_time_get_year (dt));
|
||||
g_assert_cmpint (12, ==, g_date_time_get_month (dt));
|
||||
g_assert_cmpint (11, ==, g_date_time_get_day_of_month (dt));
|
||||
g_assert_cmpint (12, ==, g_date_time_get_hour (dt));
|
||||
g_assert_cmpint (11, ==, g_date_time_get_minute (dt));
|
||||
g_assert_cmpint (10, ==, g_date_time_get_second (dt));
|
||||
g_date_time_unref (dt);
|
||||
|
||||
dt = g_date_time_new_full (2010, 5, 24, 8, 4, 0, "America/Recife");
|
||||
g_assert_cmpint (2010, ==, g_date_time_get_year (dt));
|
||||
g_assert_cmpint (5, ==, g_date_time_get_month (dt));
|
||||
g_assert_cmpint (24, ==, g_date_time_get_day_of_month (dt));
|
||||
g_assert_cmpint (8, ==, g_date_time_get_hour (dt));
|
||||
g_assert_cmpint (4, ==, g_date_time_get_minute (dt));
|
||||
g_assert_cmpint (0, ==, g_date_time_get_second (dt));
|
||||
g_assert_cmpstr ("BRT", ==, g_date_time_get_timezone_name (dt));
|
||||
g_assert (!g_date_time_is_daylight_savings (dt));
|
||||
g_date_time_unref (dt);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_utc_now (void)
|
||||
{
|
||||
GDateTime *dt;
|
||||
time_t t;
|
||||
struct tm tm;
|
||||
|
||||
t = time (NULL);
|
||||
gmtime_r (&t, &tm);
|
||||
dt = g_date_time_new_utc_now ();
|
||||
g_assert_cmpint (tm.tm_year + 1900, ==, g_date_time_get_year (dt));
|
||||
g_assert_cmpint (tm.tm_mon + 1, ==, g_date_time_get_month (dt));
|
||||
g_assert_cmpint (tm.tm_mday, ==, g_date_time_get_day_of_month (dt));
|
||||
g_assert_cmpint (tm.tm_hour, ==, g_date_time_get_hour (dt));
|
||||
g_assert_cmpint (tm.tm_min, ==, g_date_time_get_minute (dt));
|
||||
g_assert_cmpint (tm.tm_sec, ==, g_date_time_get_second (dt));
|
||||
g_date_time_unref (dt);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_get_utc_offset (void)
|
||||
{
|
||||
GDateTime *dt;
|
||||
GTimeSpan ts;
|
||||
struct tm tm;
|
||||
|
||||
memset (&tm, 0, sizeof (tm));
|
||||
get_localtime_tm (time (NULL), &tm);
|
||||
|
||||
dt = g_date_time_new_now ();
|
||||
ts = g_date_time_get_utc_offset (dt);
|
||||
g_assert_cmpint (ts, ==, (tm.tm_gmtoff * G_TIME_SPAN_SECOND));
|
||||
g_date_time_unref (dt);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_to_timeval (void)
|
||||
{
|
||||
GTimeVal tv1, tv2;
|
||||
GDateTime *dt;
|
||||
|
||||
memset (&tv1, 0, sizeof (tv1));
|
||||
memset (&tv2, 0, sizeof (tv2));
|
||||
|
||||
g_get_current_time (&tv1);
|
||||
dt = g_date_time_new_from_timeval (&tv1);
|
||||
g_date_time_to_timeval (dt, &tv2);
|
||||
g_assert_cmpint (tv1.tv_sec, ==, tv2.tv_sec);
|
||||
g_assert_cmpint (tv1.tv_usec, ==, tv2.tv_usec);
|
||||
g_date_time_unref (dt);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_to_local (void)
|
||||
{
|
||||
GDateTime *utc, *now, *dt;
|
||||
|
||||
utc = g_date_time_new_utc_now ();
|
||||
now = g_date_time_new_now ();
|
||||
dt = g_date_time_to_local (utc);
|
||||
|
||||
g_assert_cmpint (g_date_time_get_year (now), ==, g_date_time_get_year (dt));
|
||||
g_assert_cmpint (g_date_time_get_month (now), ==, g_date_time_get_month (dt));
|
||||
g_assert_cmpint (g_date_time_get_day_of_month (now), ==, g_date_time_get_day_of_month (dt));
|
||||
g_assert_cmpint (g_date_time_get_hour (now), ==, g_date_time_get_hour (dt));
|
||||
g_assert_cmpint (g_date_time_get_minute (now), ==, g_date_time_get_minute (dt));
|
||||
g_assert_cmpint (g_date_time_get_second (now), ==, g_date_time_get_second (dt));
|
||||
|
||||
g_date_time_unref (now);
|
||||
g_date_time_unref (utc);
|
||||
g_date_time_unref (dt);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_to_utc (void)
|
||||
{
|
||||
GDateTime *dt, *dt2;
|
||||
time_t t;
|
||||
struct tm tm;
|
||||
|
||||
t = time (NULL);
|
||||
gmtime_r (&t, &tm);
|
||||
dt2 = g_date_time_new_now ();
|
||||
dt = g_date_time_to_utc (dt2);
|
||||
g_assert_cmpint (tm.tm_year + 1900, ==, g_date_time_get_year (dt));
|
||||
g_assert_cmpint (tm.tm_mon + 1, ==, g_date_time_get_month (dt));
|
||||
g_assert_cmpint (tm.tm_mday, ==, g_date_time_get_day_of_month (dt));
|
||||
g_assert_cmpint (tm.tm_hour, ==, g_date_time_get_hour (dt));
|
||||
g_assert_cmpint (tm.tm_min, ==, g_date_time_get_minute (dt));
|
||||
g_assert_cmpint (tm.tm_sec, ==, g_date_time_get_second (dt));
|
||||
g_date_time_unref (dt);
|
||||
g_date_time_unref (dt2);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_get_day_of_year (void)
|
||||
{
|
||||
#define TEST_DAY_OF_YEAR(y,m,d,o) G_STMT_START { \
|
||||
GDateTime *__dt = g_date_time_new_from_date ((y), (m), (d)); \
|
||||
g_assert_cmpint ((o), ==, g_date_time_get_day_of_year (__dt)); \
|
||||
g_date_time_unref (__dt); } G_STMT_END
|
||||
|
||||
TEST_DAY_OF_YEAR (2009, 1, 1, 1);
|
||||
TEST_DAY_OF_YEAR (2009, 2, 1, 32);
|
||||
TEST_DAY_OF_YEAR (2009, 8, 16, 228);
|
||||
TEST_DAY_OF_YEAR (2008, 8, 16, 229);
|
||||
}
|
||||
|
||||
static void
|
||||
test_GDateTime_printf (void)
|
||||
{
|
||||
gchar dst[16];
|
||||
struct tm tt;
|
||||
time_t t;
|
||||
gchar t_str[16];
|
||||
|
||||
#define TEST_PRINTF(f,o) G_STMT_START { \
|
||||
GDateTime *__dt = g_date_time_new_from_date (2009, 10, 24); \
|
||||
gchar *__p = g_date_time_printf (__dt, (f)); \
|
||||
g_assert_cmpstr (__p, ==, (o)); \
|
||||
g_date_time_unref (__dt); \
|
||||
g_free (__p); } G_STMT_END
|
||||
|
||||
#define TEST_PRINTF_DATE(y,m,d,f,o) G_STMT_START { \
|
||||
GDateTime *dt = g_date_time_new_from_date ((y), (m), (d)); \
|
||||
gchar *p = g_date_time_printf (dt, (f)); \
|
||||
g_assert_cmpstr (p, ==, (o)); \
|
||||
g_date_time_unref (dt); \
|
||||
g_free (p); } G_STMT_END
|
||||
|
||||
#define TEST_PRINTF_TIME(h,m,s,f,o) G_STMT_START { \
|
||||
GDateTime *dt = g_date_time_new_full (2009, 10, 24, (h), (m), (s), NULL); \
|
||||
gchar *p = g_date_time_printf (dt, (f)); \
|
||||
g_assert_cmpstr (p, ==, (o)); \
|
||||
g_date_time_unref (dt); \
|
||||
g_free (p); } G_STMT_END
|
||||
|
||||
/*
|
||||
* This is a little helper to make sure we can compare timezones to
|
||||
* that of the generated timezone.
|
||||
*/
|
||||
t = time (NULL);
|
||||
memset (&tt, 0, sizeof(tt));
|
||||
get_localtime_tm (t, &tt);
|
||||
tt.tm_year = 2009 - 1900;
|
||||
tt.tm_mon = 9; /* 0 indexed */
|
||||
tt.tm_mday = 24;
|
||||
t = mktime (&tt);
|
||||
memset (&tt, 0, sizeof(tt));
|
||||
get_localtime_tm (t, &tt);
|
||||
strftime (dst, sizeof(dst), "%Z", &tt);
|
||||
|
||||
/* get current time_t for 20090924 in the local timezone */
|
||||
tt.tm_sec = 0;
|
||||
tt.tm_min = 0;
|
||||
tt.tm_hour = 0;
|
||||
t = mktime (&tt);
|
||||
g_sprintf (t_str, "%ld", t);
|
||||
|
||||
TEST_PRINTF ("%a", "Sat");
|
||||
TEST_PRINTF ("%A", "Saturday");
|
||||
TEST_PRINTF ("%b", "Oct");
|
||||
TEST_PRINTF ("%B", "October");
|
||||
TEST_PRINTF ("%d", "24");
|
||||
TEST_PRINTF_DATE (2009, 1, 1, "%d", "01");
|
||||
TEST_PRINTF ("%e", "24"); // fixme
|
||||
TEST_PRINTF ("%h", "Oct");
|
||||
TEST_PRINTF ("%H", "00");
|
||||
TEST_PRINTF_TIME (15, 0, 0, "%H", "15");
|
||||
TEST_PRINTF ("%I", "12");
|
||||
TEST_PRINTF_TIME (15, 0, 0, "%I", "03");
|
||||
TEST_PRINTF ("%j", "297");
|
||||
TEST_PRINTF ("%k", " 0");
|
||||
TEST_PRINTF_TIME (13, 13, 13, "%k", "13");
|
||||
TEST_PRINTF ("%l", "12");
|
||||
TEST_PRINTF_TIME (13, 13, 13, "%l", " 1");
|
||||
TEST_PRINTF_TIME (10, 13, 13, "%l", "10");
|
||||
TEST_PRINTF ("%m", "10");
|
||||
TEST_PRINTF ("%M", "00");
|
||||
TEST_PRINTF ("%N", "0");
|
||||
TEST_PRINTF ("%p", "AM");
|
||||
TEST_PRINTF_TIME (13, 13, 13, "%p", "PM");
|
||||
TEST_PRINTF ("%P", "am");
|
||||
TEST_PRINTF_TIME (13, 13, 13, "%P", "pm");
|
||||
TEST_PRINTF ("%r", "12:00:00 AM");
|
||||
TEST_PRINTF_TIME (13, 13, 13, "%r", "01:13:13 PM");
|
||||
TEST_PRINTF ("%R", "00:00");
|
||||
TEST_PRINTF_TIME (13, 13, 31, "%R", "13:13");
|
||||
TEST_PRINTF ("%s", t_str);
|
||||
TEST_PRINTF ("%S", "00");
|
||||
TEST_PRINTF ("%t", " ");
|
||||
TEST_PRINTF ("%W", "42");
|
||||
TEST_PRINTF ("%u", "6");
|
||||
TEST_PRINTF ("%x", "10/24/09");
|
||||
TEST_PRINTF ("%X", "00:00:00");
|
||||
TEST_PRINTF_TIME (13, 14, 15, "%X", "13:14:15");
|
||||
TEST_PRINTF ("%y", "09");
|
||||
TEST_PRINTF ("%Y", "2009");
|
||||
TEST_PRINTF ("%%", "%");
|
||||
TEST_PRINTF ("%", "");
|
||||
TEST_PRINTF ("%9", NULL);
|
||||
TEST_PRINTF ("%z", dst);
|
||||
}
|
||||
|
||||
gint
|
||||
main (gint argc,
|
||||
gchar *argv[])
|
||||
{
|
||||
g_test_init (&argc, &argv, NULL);
|
||||
|
||||
/* GDateTime Tests */
|
||||
|
||||
g_test_add_func ("/GDateTime/add_days", test_GDateTime_add_days);
|
||||
g_test_add_func ("/GDateTime/add_full", test_GDateTime_add_full);
|
||||
g_test_add_func ("/GDateTime/add_hours", test_GDateTime_add_hours);
|
||||
g_test_add_func ("/GDateTime/add_milliseconds", test_GDateTime_add_milliseconds);
|
||||
g_test_add_func ("/GDateTime/add_minutes", test_GDateTime_add_minutes);
|
||||
g_test_add_func ("/GDateTime/add_months", test_GDateTime_add_months);
|
||||
g_test_add_func ("/GDateTime/add_seconds", test_GDateTime_add_seconds);
|
||||
g_test_add_func ("/GDateTime/add_weeks", test_GDateTime_add_weeks);
|
||||
g_test_add_func ("/GDateTime/add_years", test_GDateTime_add_years);
|
||||
g_test_add_func ("/GDateTime/compare", test_GDateTime_compare);
|
||||
g_test_add_func ("/GDateTime/copy", test_GDateTime_copy);
|
||||
g_test_add_func ("/GDateTime/date", test_GDateTime_date);
|
||||
g_test_add_func ("/GDateTime/diff", test_GDateTime_diff);
|
||||
g_test_add_func ("/GDateTime/equal", test_GDateTime_equal);
|
||||
g_test_add_func ("/GDateTime/get_day_of_week", test_GDateTime_get_day_of_week);
|
||||
g_test_add_func ("/GDateTime/get_day_of_month", test_GDateTime_get_day_of_month);
|
||||
g_test_add_func ("/GDateTime/get_day_of_year", test_GDateTime_get_day_of_year);
|
||||
g_test_add_func ("/GDateTime/get_dmy", test_GDateTime_get_dmy);
|
||||
g_test_add_func ("/GDateTime/get_hour", test_GDateTime_get_hour);
|
||||
g_test_add_func ("/GDateTime/get_julian", test_GDateTime_get_julian);
|
||||
g_test_add_func ("/GDateTime/get_microsecond", test_GDateTime_get_microsecond);
|
||||
g_test_add_func ("/GDateTime/get_millisecond", test_GDateTime_get_millisecond);
|
||||
g_test_add_func ("/GDateTime/get_minute", test_GDateTime_get_minute);
|
||||
g_test_add_func ("/GDateTime/get_month", test_GDateTime_get_month);
|
||||
g_test_add_func ("/GDateTime/get_second", test_GDateTime_get_second);
|
||||
g_test_add_func ("/GDateTime/get_utc_offset", test_GDateTime_get_utc_offset);
|
||||
g_test_add_func ("/GDateTime/get_year", test_GDateTime_get_year);
|
||||
g_test_add_func ("/GDateTime/hash", test_GDateTime_hash);
|
||||
g_test_add_func ("/GDateTime/is_leap_year", test_GDateTime_is_leap_year);
|
||||
g_test_add_func ("/GDateTime/new_from_date", test_GDateTime_new_from_date);
|
||||
g_test_add_func ("/GDateTime/new_from_epoch", test_GDateTime_new_from_epoch);
|
||||
g_test_add_func ("/GDateTime/new_from_timeval", test_GDateTime_new_from_timeval);
|
||||
g_test_add_func ("/GDateTime/new_full", test_GDateTime_new_full);
|
||||
g_test_add_func ("/GDateTime/now", test_GDateTime_now);
|
||||
g_test_add_func ("/GDateTime/printf", test_GDateTime_printf);
|
||||
g_test_add_func ("/GDateTime/to_local", test_GDateTime_to_local);
|
||||
g_test_add_func ("/GDateTime/to_epoch", test_GDateTime_to_epoch);
|
||||
g_test_add_func ("/GDateTime/to_timeval", test_GDateTime_to_timeval);
|
||||
g_test_add_func ("/GDateTime/to_utc", test_GDateTime_to_utc);
|
||||
g_test_add_func ("/GDateTime/today", test_GDateTime_today);
|
||||
g_test_add_func ("/GDateTime/utc_now", test_GDateTime_utc_now);
|
||||
|
||||
return g_test_run ();
|
||||
}
|
Loading…
Reference in New Issue
Block a user