JavaScript gettimeofday
Returns the current time as array
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 | function gettimeofday ( return_float ) { // Returns the current time as array // // version: 1008.1718 // discuss at: http://phpjs.org/functions/gettimeofday // + original by: Brett Zamir (http://brett-zamir.me) // + derived from: Josh Fraser (http://onlineaspect.com/2007/06/08/auto-detect-a-time-zone-with-javascript/) // + parts by: Breaking Par Consulting Inc (http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256CFB006C45F7) // + revised by: Theriault // * example 1: gettimeofday(); // * returns 1: {sec: 12, usec: 153000, minuteswest: -480, dsttime: 0} // * example 1: gettimeofday(true); // * returns 1: 1238748978.49 var t = new Date(), y = 0; if (return_float) { return t.getTime() / 1000; } y = t.getFullYear(); // Store current year. return { sec : t.getUTCSeconds(), usec : t.getUTCMilliseconds() * 1000, minuteswest : t.getTimezoneOffset(), // Compare Jan 1 minus Jan 1 UTC to Jul 1 minus Jul 1 UTC to see if DST is observed. dsttime : 0 + (((new Date(y, 0)) - Date.UTC(y, 0)) !== ((new Date(y, 6)) - Date.UTC(y, 6))) }; } |
Examples
Running
1 2 | gettimeofday(); gettimeofday(true); |
Should return
1 | 1238748978.49 |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have gettimeofday goodness in JavaScript.
No comments yet. Be the first!
spread the word:
Use any PHP function in JavaScript
These kind folks have already donated: @HalfWinter, Paulo Freitas, Andros Peña Romo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>