]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/opac/common/js/Date.W3CDTF.js
5e1c24eda092bbb445c4dd2e4a785ce27bad6ed2
[working/Evergreen.git] / Open-ILS / web / opac / common / js / Date.W3CDTF.js
1 // Date/W3CDTF.js -- W3C Date and Time Formats
2
3 Date.W3CDTF = function ( dtf ) {
4     var dd = new Date();
5     dd.setW3CDTF = Date.W3CDTF.prototype.setW3CDTF;
6     dd.getW3CDTF = Date.W3CDTF.prototype.getW3CDTF;
7     if ( dtf ) this.setW3CDTF( dtf );
8     return dd;
9 };
10
11 Date.W3CDTF.VERSION = "0.03";
12
13 Date.W3CDTF.prototype.setW3CDTF = function( dtf ) {
14     var sp = dtf.split( /[^0-9]/ );
15
16     // invalid format
17     if ( sp.length < 6 || sp.length > 8 ) return;
18
19     // invalid time zone
20     if ( sp.length == 7 ) {
21         if ( dtf.charAt( dtf.length-1 ) != "Z" ) return;
22     }
23
24     // to numeric
25     for( var i=0; i<sp.length; i++ ) sp[i] = sp[i]-0;
26
27     // invalid range
28     if ( sp[0] < 1970 ||                // year
29          sp[1] < 1 || sp[1] > 12 ||     // month
30          sp[2] < 1 || sp[2] > 31 ||     // day
31          sp[3] < 0 || sp[3] > 23 ||     // hour
32          sp[4] < 0 || sp[4] > 59 ||     // min
33          sp[5] < 0 || sp[5] > 60 ) {    // sec
34         return;                         // invalid date 
35     }
36
37     // get UTC milli-second
38
39     var msec = Date.UTC( sp[0], sp[1]-1, sp[2], sp[3], sp[4], sp[5] );
40
41     // time zene offset
42     if ( sp.length == 8 ) {
43         if ( dtf.indexOf("+") < 0 ) sp[6] *= -1;
44         if ( sp[6] < -12 || sp[6] > 13 ) return;    // time zone offset hour
45         if ( sp[7] < 0 || sp[7] > 59 ) return;      // time zone offset min
46         msec -= (sp[6]*60+sp[7]) * 60000;
47     }
48
49     // set by milli-second;
50     return this.setTime( msec );
51 };
52
53 Date.W3CDTF.prototype.getW3CDTF = function() {
54     var year = this.getFullYear();
55     var mon  = this.getMonth()+1;
56     var day  = this.getDate();
57     var hour = this.getHours();
58     var min  = this.getMinutes();
59     var sec  = this.getSeconds();
60
61     // time zone
62     var tzos = this.getTimezoneOffset();
63     var tzpm = ( tzos > 0 ) ? "-" : "+";
64     if ( tzos < 0 ) tzos *= -1;
65     var tzhour = tzos / 60;
66     var tzmin  = tzos % 60;
67
68     // sprintf( "%02d", ... )
69     if ( mon  < 10 ) mon  = "0"+mon;
70     if ( day  < 10 ) day  = "0"+day;
71     if ( hour < 10 ) hour = "0"+hour;
72     if ( min  < 10 ) min  = "0"+min;
73     if ( sec  < 10 ) sec  = "0"+sec;
74     if ( tzhour < 10 ) tzhour = "0"+tzhour;
75     if ( tzmin  < 10 ) tzmin  = "0"+tzmin;
76     var dtf = year+"-"+mon+"-"+day+"T"+hour+":"+min+":"+sec+tzpm+tzhour+":"+tzmin;
77     return dtf;
78 };
79
80 /*
81
82 =head1 NAME
83
84 Date.W3CDTF - W3C Date and Time Formats
85
86 =head1 SYNOPSIS
87
88     var dd = new Date.W3CDTF();         // now
89     document.write( "getW3CDTF: "+ dd.getW3CDTF() +"\n" );
90
91     dd.setW3CDTF( "2005-04-23T17:20:00+09:00" );
92     document.write( "toLocaleString: "+ dd.toLocaleString() +"\n" );
93
94 =head1 DESCRIPTION
95
96 This module understands the W3CDTF date/time format, an ISO 8601 profile, 
97 defined by W3C. This format as the native date format of RSS 1.0.
98 It can be used to parse these formats in order to create the appropriate objects.
99
100 =head1 METHODS
101
102 =head2 new()
103
104 This constructor method creates a new Date object which has 
105 following methods in addition to Date's all native methods.
106
107 =head2 setW3CDTF( "2006-02-15T19:40:00Z" )
108
109 This method parse a W3CDTF datetime string and sets it.
110
111 =head2 getW3CDTF()
112
113 This method returns a W3CDTF datetime string.
114 Its timezone is always local timezone configured on OS.
115
116 =head1 SEE ALSO
117
118 http://www.w3.org/TR/NOTE-datetime
119
120 =head1 AUTHOR
121
122 Yusuke Kawasaki http://www.kawa.net/
123
124 =head1 COPYRIGHT AND LICENSE
125
126 Copyright (c) 2005-2006 Yusuke Kawasaki. All rights reserved.
127 This program is free software; you can redistribute it and/or 
128 modify it under the Artistic license. Or whatever license I choose,
129 which I will do instead of keeping this documentation like it is.
130
131 =cut
132 */