Skip to content

Instantly share code, notes, and snippets.

@eht16
Last active November 7, 2021 23:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eht16/757cc4e814da41a9e8d7a8fba443232f to your computer and use it in GitHub Desktop.
Save eht16/757cc4e814da41a9e8d7a8fba443232f to your computer and use it in GitHub Desktop.
#include <errno.h>
#include <stdio.h>
#include <time.h>
#include <glib.h>
/*
gcc -o strftime_test strftime_test.c `pkg-config --cflags --libs glib-2.0` && strftime_test
*/
void format_time(time_t time_to_use, const gchar *format)
{
const struct tm *now_tm = localtime(&time_to_use);
GDateTime *dt = g_date_time_new_from_unix_local(time_to_use);
gchar *g_formatted;
gchar formatted[8192]; // ridiculous huge buffer to ensure that is not the problem
gsize len;
len = strftime(formatted, 8192, format, now_tm);
g_formatted = g_date_time_format(dt, format);
printf("Format: %s:\n", format);
if (len == 0)
printf("CRT : Error: %s\n", g_strerror(errno));
else
printf("CRT : %s\n", formatted);
printf("GLib: %s\n\n", g_formatted);
g_free(g_formatted);
}
int main(int argc, char **argv)
{
time_t now = time(NULL);
format_time(now, "%a, %d %b %Y %T %z");
format_time(now, "%a, %d %b %Y %T %Z");
format_time(now, "%a, %d %b %Y %H:%M:%S %z");
format_time(now, "%a, %d %b %Y %H:%M:%S %Z");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment