Function: localtime version 121

Function:
Description: Returns the results of the C system call localtime as an associative array if the associative_array argument is set to 1 other wise it is a regular array.
Version:
Status: latest
Date: 2011-12-20 05:32
Summary: Import from phpjs.
Programmer: jj5
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function localtime (timestamp, is_assoc) {
    // http://jsphp.co/jsphp/fn/view/localtime
    // +   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)
    // +   improved by: Ryan W Tenney (http://ryan.10e.us)
    // *     example 1: localtime();
    // *     returns 1: [50,28,0,14,2,109,6,73,0]
    var t, yday, x, o = {};

    if (timestamp === undefined) {
        t = new Date();
    } else if (timestamp instanceof Date) {
        t = timestamp;
    } else {
        t = new Date(timestamp * 1000);
    }

    x = function (t, m) {
        var a = (new Date(t.getFullYear(), 0, m, 0, 0, 0, 0)).toUTCString();
        return t - new Date(a.slice(0, a.lastIndexOf(' ') - 1));
    };

    yday = Math.floor((t - new Date(t.getFullYear(), 0, 1)) / 86400000);

    o = {
        'tm_sec': t.getSeconds(),
        // seconds
        'tm_min': t.getMinutes(),
        // minutes
        'tm_hour': t.getHours(),
        // hour
        'tm_mday': t.getDate(),
        // day of the month, 1 - 31
        'tm_mon': t.getMonth(),
        // month of the year, 0 (January) to 11 (December)
        'tm_year': t.getFullYear() - 1900,
        // years since 1900
        'tm_wday': t.getDay(),
        // day of the week, 0 (Sun) to 6 (Sat)
        'tm_yday': yday,
        // day of the year
        'tm_isdst': +(x(t, 1) != x(t, 6)) // is daylight savings time in effect
    };

    return is_assoc ? o : [
    o.tm_sec, o.tm_min, o.tm_hour, o.tm_mday, o.tm_mon, o.tm_year, o.tm_wday, o.tm_yday, o.tm_isdst];
}
[top]

Comments

There are no comments yet, be the first!

Please Login or Register to post comments.