How to get current time in milliseconds in Haxe?
Asked Answered
M

3

5

I need a function that returns the local time in milliseconds on the CPP target.

I tried Haxe's Date class, but Date.now() gives me the time in seconds.

Macrophage answered 25/1, 2016 at 20:32 Comment(0)
I
9

Sys.time() * 1000.0 - http://api.haxe.org/Sys.html#time

Gives the most precise timestamp value (in seconds)

To be clear, I tried this and got millisecond resolution on the cpp target. Sys is available on cpp, cs, java, macro, neko, php and python.

Impassion answered 25/1, 2016 at 21:49 Comment(4)
Though I must say I find it kinda disturbing that this just has float precision. At least from C++, one is used to much better precision.Ablaze
I suppose it's easier to implement a single method across platforms if you pass a float back. Here's how it's implemented for cpp: github.com/HaxeFoundation/hxcpp/blob/…Impassion
they should really change the documentation and return milliseconds - else if useless unless you see this answer.Sputum
@Ablaze Remember that haxe floats are 64-bit, so you actually get 56 bits of integer precision.Karelian
F
4

You could try Date.now().getTime(), however:

Returns the timestamp of the date. It might only have a per-second precision depending on the platforms.

Flavoprotein answered 25/1, 2016 at 20:50 Comment(2)
Doesn't work for cpp target. Maybe I should edit my question.Macrophage
Yes, this only gives me per-second precision on the cpp target i.e: 1453822269000, 1453822270000...Impassion
M
0

A fast way of getting a timestamp would be to use the haxe.Timer.stamp() method.

Example:

import haxe.Timer;

var timestamp:Float = Timer.stamp(); // return a timestamp in seconds with fractions

Note that the value itself might differ depending on platforms, only differences between two values make sense.

Moonshine answered 15/6, 2020 at 15:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.