Question

What is the advantage of using GDateTime instead of localtime_r in this code:

char time_str[100];
time_t t;
struct tm lt;

t = time(NULL);
localtime_r(&t, &lt);
strftime(time_str, 100, _("%Y%m%d-%H%M%S"), &lt);
Was it helpful?

Solution

GDateTime appears to be a component of the Gnome Library (GLib). As documented in GLib, GDateTime is a structure (and set of methods) for working with date/time data, capable of representing (with microsecond precision) any date/time from 0001-01-01 00:00:00 to 9999-12-31 23:59:59.999999.

Therefore, if (a) You're already using GLib (or are willing to pull in the entire GLib library just for this one thing), and (b) you need or have a use for the increased range and/or precision provided by GDateTime, then, the advantage of using GDateTime instead of localtime_r is that you have the increased range and/or precision provided by GDateTime.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top