Giter Site home page Giter Site logo

Date.now() lacks milliseconds on Mac about mujs HOT 7 CLOSED

 avatar commented on August 19, 2024
Date.now() lacks milliseconds on Mac

from mujs.

Comments (7)

ccxvii avatar ccxvii commented on August 19, 2024

I would have expected MacOSX to set the __ unix __ macro, so that we can use gettimeofday() but I guess not...

from mujs.

 avatar commented on August 19, 2024

Yeah, it should probably set that macro since macOS is based on FreeBSD... I am surprised about that. Hmm, but the results indicate that it's not doing that. Since the output is using the "* 1000" fallback...

I did see that macOS has gettimeofday() so hopefully it's just a matter of using another macro to detect Macs.

from mujs.

avih avatar avih commented on August 19, 2024

It's also possible, if less likely, that gettimeofday returns in seconds resolution.

from mujs.

 avatar commented on August 19, 2024

That's probably not it...

https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man2/gettimeofday.2.html

The time is expressed in seconds and
microseconds since midnight (0 hour), January 1, 1970.

from mujs.

 avatar commented on August 19, 2024

@ccxvii Not sure if true, but wm4: __unix__ sounds like something non-standard (he's the main dev of mpv and programs alldayerryday so if he says that he doesn't know what it is then it is probably something non-standard) ;-)

from mujs.

ccxvii avatar ccxvii commented on August 19, 2024

The __unix__ macro catches all the main Linux and BSD variants. Adding a check for __APPLE__ should do the trick. Let me know if it doesn't work.

from mujs.

 avatar commented on August 19, 2024

@ccxvii Thanks, I wrote a small test:

#include <stdio.h>
#include <math.h>
#include <sys/time.h>

int main()
{
    printf("Hello World\n");
#if defined(__unix__)
    printf("unix\n");
#endif
#if defined(__APPLE__)
    printf("apple\n");
#endif

    struct timeval tv;
	gettimeofday(&tv, NULL);
	double tx = floor(tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0);
    printf("%.f\n", tx);
}

Result on a Mac:

+ $ ./a.out 
Hello World
apple
1512999085793

+ $ ./a.out 
Hello World
apple
1512999086631

+ $ ./a.out 
Hello World
apple
1512999087257

Thank you. That concludes that the new version will work.

Could you please consider tagging v1.0.2 so that distros pick it up with all these array sort and Mac fixes and recent memory leak fixes etc? Would be awesome! <3

from mujs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.