PageRenderTime 103ms CodeModel.GetById 40ms app.highlight 5ms RepoModel.GetById 55ms app.codeStats 0ms

/ajax/scripts/units.js

http://showslow.googlecode.com/
JavaScript | 73 lines | 54 code | 15 blank | 4 comment | 16 complexity | 12f4c9c9bf895401703c6ac74cc31cb4 MD5 | raw file
 1/*==================================================
 2 *  Default Unit
 3 *==================================================
 4 */
 5
 6SimileAjax.NativeDateUnit = new Object();
 7
 8SimileAjax.NativeDateUnit.makeDefaultValue = function() {
 9    return new Date();
10};
11
12SimileAjax.NativeDateUnit.cloneValue = function(v) {
13    return new Date(v.getTime());
14};
15
16SimileAjax.NativeDateUnit.getParser = function(format) {
17    if (typeof format == "string") {
18        format = format.toLowerCase();
19    }
20    
21    var parser = (format == "iso8601" || format == "iso 8601") ?
22                    SimileAjax.DateTime.parseIso8601DateTime : 
23                    SimileAjax.DateTime.parseGregorianDateTime;
24                    
25    return function(d) {
26        if (typeof d != 'undefined' && typeof d.toUTCString == "function") {
27            return d;
28        } else {
29            return parser(d);
30        }
31    };
32};
33
34SimileAjax.NativeDateUnit.parseFromObject = function(o) {
35    return SimileAjax.DateTime.parseGregorianDateTime(o);
36};
37
38SimileAjax.NativeDateUnit.toNumber = function(v) {
39    return v.getTime();
40};
41
42SimileAjax.NativeDateUnit.fromNumber = function(n) {
43    return new Date(n);
44};
45
46SimileAjax.NativeDateUnit.compare = function(v1, v2) {
47    var n1, n2;
48    if (typeof v1 == "object") {
49        n1 = v1.getTime();
50    } else {
51        n1 = Number(v1);
52    }
53    if (typeof v2 == "object") {
54        n2 = v2.getTime();
55    } else {
56        n2 = Number(v2);
57    }
58    
59    return n1 - n2;
60};
61
62SimileAjax.NativeDateUnit.earlier = function(v1, v2) {
63    return SimileAjax.NativeDateUnit.compare(v1, v2) < 0 ? v1 : v2;
64};
65
66SimileAjax.NativeDateUnit.later = function(v1, v2) {
67    return SimileAjax.NativeDateUnit.compare(v1, v2) > 0 ? v1 : v2;
68};
69
70SimileAjax.NativeDateUnit.change = function(v, n) {
71    return new Date(v.getTime() + n);
72};
73