Vala: is this the correct way to get the current time in milliseconds?

Using this library with Vala:

http://valadoc.org/#!api=glib-2.0/GLib.DateTime

    GLib.DateTime now = new GLib.DateTime.now_local();

    var sec = now.to_unix()
    var msec = (sec * 1000) + now.get_microsecond();

Is this the correct way to get the current time in milliseconds?

is there a better way?

+4
source share
1 answer

GLib.DateTime is the right way to do this, and it's a little strange that you are requesting local time and then converting it to unix time (which is implicitly converted to UTC). However, the real problem is that you combine milliseconds (1 / 1000th of a second) and microseconds (1/1000000 seconds per second). So change the last line to

var msec = (sec * 1000) + (now.get_microsecond () / 1000);

Alternatively, it is easier to use GLib.get_real_time :

int64 msec = GLib.get_real_time () / 1000;

(. GLib.get_monotonic_time

+9

All Articles