#include <glib.h>

int main (int argc, char *argv[])
{
	GTimeVal tv = {0, 0};
	GDateTime *dt, *cdt;
	GTimeZone *tz;


	/* ISO 8601 date in local timezone */
	char *iso8601_date = "2012-09-09T01:30:00Z";
	char *parsed_date;


	g_printf ("*** Converting ISO-8601 Date to Local Time Zone and UTC ***\n\n");

	/* Parse ISO 8601 formatted date */
	g_printf ("Date in ISO8601 format: %s\n", iso8601_date);
	g_time_val_from_iso8601 (iso8601_date, &tv);
	g_printf ("Date parsed by g_time_val_from_iso8601: %u %u\n", tv.tv_sec, tv.tv_usec);

	/* Convert to local timezone */
	dt = g_date_time_new_from_timeval_local (&tv);
	parsed_date = g_date_time_format (dt, "%FT%T");
	g_printf ("Date converted by g_date_time_new_from_timeval_local: %s\n", parsed_date);
	g_free (parsed_date);
	g_date_time_unref (dt);

	/* Convert to UTC */
	dt = g_date_time_new_from_timeval_utc (&tv);
	parsed_date = g_date_time_format (dt, "%FT%TZ");
	g_printf ("Date converted by g_date_time_new_from_timeval_utc: %s\n", parsed_date);
	g_free (parsed_date);
	g_date_time_unref (dt);

    g_printf ("\n\n");


	g_printf ("*** Converting ISO-8601 Date to Different Time Zones ***\n\n");
	dt = g_date_time_new_from_timeval_utc (&tv);

	/* Convert to Singapore Standard Time */
#ifdef G_OS_WIN32
	tz = g_time_zone_new ("Singapore Standard Time");
#else
	tz = g_time_zone_new ("Asia/Singapore");
#endif
	cdt = g_date_time_to_timezone (dt, tz);
	parsed_date = g_date_time_format (cdt, "%FT%T");
	g_printf ("Date converted to Singapore Standard Time: %s\n", parsed_date);
	g_free (parsed_date);
	g_date_time_unref (cdt);
	g_time_zone_unref (tz);

#if defined (G_OS_WIN32) || defined (SHOW_INTERVALS)
    g_printf ("\n");

    g_printf ("Abbreviation: %s\n", g_time_zone_get_abbreviation (tz, 0));
    g_printf ("Is DST: %s\n", g_time_zone_is_dst (tz, 0)? "Yes":"No");
#endif

    g_printf ("\n\n");

	/* Convert to Tokyo Standard Time */
#ifdef G_OS_WIN32
	tz = g_time_zone_new ("Tokyo Standard Time");
#else
	tz = g_time_zone_new ("Asia/Tokyo");
#endif
	cdt = g_date_time_to_timezone (dt, tz);
	parsed_date = g_date_time_format (cdt, "%FT%T");
	g_printf ("Date converted to Tokyo Standard Time: %s\n", parsed_date);
	g_free (parsed_date);
	g_date_time_unref (cdt);
	g_time_zone_unref (tz);

#if defined (G_OS_WIN32) || defined (SHOW_INTERVALS)
    g_printf ("\n");

    g_printf ("Abbreviation: %s\n", g_time_zone_get_abbreviation (tz, 0));
    g_printf ("Is DST: %s\n", g_time_zone_is_dst (tz, 0)? "Yes":"No");
#endif

    g_printf ("\n\n");

	/* Convert to Pacific Standard Time */
#ifdef G_OS_WIN32
	tz = g_time_zone_new ("Pacific Standard Time");
#else
	tz = g_time_zone_new ("PST8PDT");
#endif
	cdt = g_date_time_to_timezone (dt, tz);
	parsed_date = g_date_time_format (cdt, "%FT%T");
	g_printf ("Date converted to Pacific Standard Time: %s\n", parsed_date);
	g_free (parsed_date);
	g_date_time_unref (cdt);
	g_time_zone_unref (tz);

#if defined (G_OS_WIN32) || defined (SHOW_INTERVALS)
    g_printf ("\n");

    int i;
	for (i = 0; i < 3; i++)
	{
		g_printf ("Interval: %d\n", i);
        g_printf ("Abbreviation: %s\n", g_time_zone_get_abbreviation (tz, i));
        g_printf ("Is DST: %s\n", g_time_zone_is_dst (tz, i)? "Yes":"No");
        g_printf ("\n");
    }

	g_date_time_unref (dt);
#endif


	return 0;
}
