How to get local time in microseconds in c

The contents of GDateTime are private.

You can get the various components of the date and time, and then compute the microseconds:

msec = year × msecs_in_a_year
     + day_of_year × msecs_in_a_day
     + hour × msecs_in_an_hour
     + minutes × msecs_in_a_minute
     + seconds × 1000
     + microseconds / 1000

Or you could get the Unix time in seconds relative to UTC, then add the timezone’s offset in seconds, then multiply by 1_000_000, add the microseconds, and divide by 1_000.

In general, though, I have doubts as to what you’re trying to achieve. Why do you think you need time in microseconds from the local time? That kind of precision is rarely warranted, and if you’re doing comparisons it’s better to do them from a standard point in time like UTC.

1 Like