/* Build:
 * cc -o test_september `pkg-config --cflags --libs glib-2.0` test_september.c
 */

#include <locale.h>
#include <stdio.h>
#include <string.h>

#include <glib.h>

static void do_test()
{
    GDate *date;
    static const char *format = NULL;
    char buf[100];

    printf ("\n================ Running the test ================\n");
    printf ("Current locale for time is: %s\n", setlocale (LC_TIME, NULL));
    date = g_date_new_dmy (1, 9, 2018);
    printf ("Testing date (DD-MM-YYYY): %02d-%02d-%04d\n",
            date->day, date->month, date->year);

    if (!format) {
        /* Is "%OB" supported?  */
        buf[0] = '\0';
        g_date_strftime (buf, 99, "%OB", date);
        if (!strcmp (buf, "%OB") || !strcmp (buf, "OB") || !strcmp (buf, ""))
            format = "%B %Y";
        else
            format = "%OB %Y";
    }

    /* In Polish the result is: "wrzesień 2018".  */
    g_date_strftime (buf, 99, format, date);
    printf ("strftime (\"%s\") output: %s\n", format, buf);

    printf ("Parsing it back...\n");
    /* In Polish it will be parsed as 01-08-2018.  */
    g_date_set_parse (date, buf);
    if (!g_date_valid (date))
        printf ("Failure, the parsed date is invalid\n");
    else {
        printf ("Result (DD-MM-YYYY): %02d-%02d-%04d ",
                date->day, date->month, date->year);
        if (date->day == 1 && date->month == 9 && date->year == 2018)
            printf ("- passed\n");
        else
            printf ("- Failure!\n");
    }
    g_date_free (date);
}

int main (int argc, char *argv[])
{
    setlocale (LC_ALL, "");
    do_test();

    setlocale (LC_ALL, "pl_PL.UTF-8");
    do_test();
}
