possible GDateTime issue with localtime on Illumos/Solaris
I'm not certain, but I think there may be an issue converting to localtime with some of the GDateTime functions on Illumos-based distros.
I'll attach this too, but I have what I think is a simple test program that illustrates the issue:
/* Compile with:
gcc -m32 `/usr/bin/pkg-config --cflags-only-I glib-2.0` \
-c GDateTime-test.c
gcc -m32 `/usr/bin/pkg-config --ldflags glib-2.0`
*/
/*
* Both a gstreamer test (tests/check/gst/gstdatetime.c) and pluma
* 1.26.0's "insert timestamp" feature get the local time wrong
* (they're off by the offset from UTC), is it because GDateTime gets
* it wrong?
*/
/*
* A stripped-down example to illustrate the problem, based upon
# gstreamer's tests/check/gst/gstdatetime.c
*/
#include <stdio.h>
#include <sys/types.h>
#include <time.h>
#include <glib.h>
int main(int argc, char *argv[])
{
GDateTime *dt;
struct tm *tm;
time_t now;
guchar * formatted;
now = time(NULL);
tm = localtime(&now);
printf("Using time(), localtime(), and asctime():\n");
printf("=========================================\n");
printf("struct tm's tm->hour = %d\n", tm->tm_hour);
printf("asctime(localtime(time)) = %s\n", asctime(tm));
dt = g_date_time_new_now_local();
formatted = g_date_time_format(dt, "%a %b %e %I:%M:%S %Y");
printf("Using GDateTime and g_date_time_new_now_local():\n");
printf("================================================\n");
printf("g_date_time_get_hour = %d\n", g_date_time_get_hour(dt));
printf("formatted GDateTime = %s\n", formatted);
}
If I compile and run this on Linux, it generates the output I expect:
$ uname -s -r
Linux 4.18.0-305.12.1.el8_4.x86_64
$ ./GDateTime-test
Using time(), localtime(), and asctime():
=========================================
struct tm's tm->hour = 4
asctime(localtime(time)) = Sun Dec 5 04:14:49 2021
Using GDateTime and g_date_time_new_now_local():
================================================
g_date_time_get_hour = 4
formatted GDateTime = Sun Dec 5 04:14:49 2021
If I compile and run this on OpenIndiana (one of the Illumos distros that came out of the OpenSolaris project), I instead get:
$ ./GDateTime-test
Using time(), localtime(), and asctime():
=========================================
struct tm's tm->hour = 4
asctime(localtime(time)) = Sun Dec 5 04:16:10 2021
Using GDateTime and g_date_time_new_now_local():
================================================
g_date_time_get_hour = 10
formatted GDateTime = Sun Dec 5 10:16:10 2021
Note that my timezone is 6 hours west of UTC, so the GDateTime is off by exactly the timezone offset.
OpenIndiana is currently shipping glib 2.66.8; we're looking to update to 2.70.2 but that's a big project because of all the software we have that links to glib.
It's certainly possible that I'm misunderstanding how e.g. g_date_time_new_now_local()
and other _local()
variants are supposed to work and that my example just doesn't make sense, but then I would expect the same code run on Linux to be wrong in the same way.
I started looking at this issue because I noticed that two different opensource projects, GStreamer and pluma, were both getting the time wrong on OpenIndiana, both by exactly the offset from UTC. I can point you to the code in each of those projects that's producing the incorrect local time on OpenIndiana (OI), if that would be helpful.
Considering that the time functions haven't diverged that much between commercial Solaris and the Illumos distros, I would expect this issue probably impacts commercial Solaris too, but I don't have any way to verify that.
I'm happy to run additional tests, if you have other code you would like me to try.