-
-
Save eht16/757cc4e814da41a9e8d7a8fba443232f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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