// The GTimeSpan saved in db is an offset to datetime_origin (0001:01:01 00:00:00)
// Datetime_taken is to be displayed and stored in XMP without time zone conversion
// The other timestamps consider the timezone (GTimeSpan converted from local to UTC)
// The text format of datetime follows the exif format except when local format
Darktable has code to do those conversions (datetime.h/.c, database.h/.c). So that might be a good place to start. But as darktable is C code, you’ll have to translate that to the language you want to use.
Note that darktable does not use the “linux epoch” at time zero!
where DT_DATETIME_ORIGIN corresponds to a GTimeSpan == 0,
at least Linux/UNIX timestamps start counting from DT_DATETIME_EPOCH.
You may want to check this for your system.