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.

Gama11
  • 31,714
  • 9
  • 78
  • 100
Enes F.
  • 406
  • 6
  • 17

3 Answers3

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.

Gama11
  • 31,714
  • 9
  • 78
  • 100
Sam Twidale
  • 1,123
  • 19
  • 16
  • 31
  • 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. – TheSHEEEP Jan 27 '16 at 11:22
  • 1
    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: https://github.com/HaxeFoundation/hxcpp/blob/735bf91da2059dd2c510596f90f13dbf7345bf56/project/libs/std/Sys.cpp#L507 – Sam Twidale Jan 28 '16 at 16:31
  • they should really change the documentation and return milliseconds - else if useless unless you see this answer. – cancerbero Jun 24 '19 at 03:50
  • @TheSHEEEP Remember that haxe floats are 64-bit, so you actually get 56 bits of integer precision. – David Given Sep 04 '20 at 21:22
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.

Gama11
  • 31,714
  • 9
  • 78
  • 100
Andrew
  • 1,282
  • 6
  • 11
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.

Creative Magic
  • 3,143
  • 3
  • 28
  • 47