1 dump('entering util/date.js\n');
3 if (typeof util == 'undefined') var util = {};
6 util.date.EXPORT_OK = [
7 'dollars_float_to_cents_integer', 'cents_as_dollars'
9 util.date.EXPORT_TAGS = { ':all' : util.date.EXPORT_OK };
11 util.date.cents_as_dollars = function( cents ) {
12 cents = cents.toString();
13 // FIXME: strip miscellaneous characters
15 switch( cents.length ) {
16 case 0: cents = '000'; break;
17 case 1: cents = '00' + cents; break;
20 dump("util.date: cents_as_dollars: cents.length? " + E + "\n");
22 return cents.substr(0,cents.length-2) + '.' + cents.substr(cents.length - 2);
25 util.date.timer_init = function (id) {
26 if (typeof util.date.timer_init.prototype.timer == 'undefined') {
27 util.date.timer_init.prototype.timer = {};
29 util.date.timer_init.prototype.timer[id] = (new Date).getTime();
32 util.date.timer_elapsed = function (id) {
33 if (! util.date.timer_init.prototype.timer[id]) { util.date.timer_init(id); }
34 var ms = (new Date).getTime() - util.date.timer_init.prototype.timer[id];
35 return( ms + 'ms (' + ms/1000 + 's)' );
38 util.date.db_date2Date = function (date) {
39 var y = date.substr(0,4);
40 var mo = date.substr(5,2);
41 var d = date.substr(8,2);
42 var h = date.substr(11,2);
43 var mi = date.substr(14,2);
44 var s = date.substr(17,2);
45 return new Date(y,mo,d,h,mi,s);
48 util.date.formatted_date = function (date,format) {
49 // pass in a Date object or epoch seconds or a postgres style date string (2005-07-19 10:38:25.211964-04)
50 if (typeof(date) == 'string') {
51 if (date.match(/:/) || date.match(/-/)) {
52 date = util.date.db_date2Date(date);
54 date = new Date( parseInt( date + '000' ) );
56 } else if (typeof(date) == 'undefined') {
57 date = new Date( parseInt( date + '000' ) );
59 var mm = date.getMonth() + 1; mm = mm.toString(); if (mm.length == 1) mm = '0' +mm;
60 var dd = date.getDate().toString(); if (dd.length == 1) dd = '0' +dd;
61 var yyyy = date.getFullYear().toString();
62 var yy = yyyy.substr(2);
63 var H = date.getHours(); H = H.toString(); if (H.length == 1) H = '0' + H;
64 var I = date.getHours(); if (I > 12) I -= 12; I = I.toString();
65 var M = date.getMinutes(); M = M.toString(); if (M.length == 1) M = '0' + M;
67 s = s.replace( /%m/g, mm );
68 s = s.replace( /%d/g, dd );
69 s = s.replace( /%Y/g, yyyy );
70 s = s.replace( /%D/g, mm + '/' + dd + '/' + yy );
71 s = s.replace( /%F/g, yyyy + '-' + mm + '-' + dd );
72 s = s.replace( /%H/g, H );
73 s = s.replace( /%I/g, I );
74 s = s.replace( /%M/g, M );
78 util.date.interval_to_seconds = function ( $interval ) {
80 $interval = $interval.replace( /and/, ',' );
81 $interval = $interval.replace( /,/, ' ' );
84 var results = $interval.match( /\s*\+?\s*(\d+)\s*(\w{1})\w*\s*/g);
85 for (var i in results) {
86 var result = results[i].match( /\s*\+?\s*(\d+)\s*(\w{1})\w*\s*/ );
87 if (result[2] == 's') $amount += result[1] ;
88 if (result[2] == 'm') $amount += 60 * result[1] ;
89 if (result[2] == 'h') $amount += 60 * 60 * result[1] ;
90 if (result[2] == 'd') $amount += 60 * 60 * 24 * result[1] ;
91 if (result[2] == 'w') $amount += 60 * 60 * 24 * 7 * result[1] ;
92 if (result[2] == 'M') $amount += ((60 * 60 * 24 * 365)/12) * result[1] ;
93 if (result[2] == 'y') $amount += 60 * 60 * 24 * 365 * result[1] ;
98 dump('exiting util/date.js\n');