Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Item9750: Pam tells me i'm holding this file hostage - personally i t…
…hink it might well be useful to someone git-svn-id: http://svn.foswiki.org/trunk/MongoDBPlugin@10670 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
- Loading branch information
SvenDowideit
authored and
SvenDowideit
committed
Feb 8, 2011
1 parent
db231fd
commit 082b462
Showing
1 changed file
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
%TMPL:DEF{foswiki_d2n_js}%function(dateString) { | ||
var parseTime = function(date, defaultLocal) { | ||
|
||
// ala Time::Local::timegm() | ||
var perlOrderDateInit = function(sec, min, hour, day, month, year) { | ||
var milliseconds = 0; | ||
|
||
//console.log("---- year "+year); | ||
//console.log("---- month "+(month+1)); | ||
//console.log("---- day "+day); | ||
//console.log("---- hour "+hour); | ||
//console.log("---- min "+min); | ||
//console.log("---- sec "+sec); | ||
return new Date(year, month, day, hour, min, sec, milliseconds); | ||
} | ||
|
||
var defined = function(val) { | ||
return (val != undefined); | ||
} | ||
|
||
// Constants | ||
var ISOMONTH = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; | ||
|
||
var MONTHLENS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; | ||
|
||
var WEEKDAY = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']; | ||
|
||
var MON2NUM = { | ||
jan: 0, | ||
feb: 1, | ||
mar: 2, | ||
apr: 3, | ||
may: 4, | ||
jun: 5, | ||
jul: 6, | ||
aug: 7, | ||
sep: 8, | ||
oct: 9, | ||
nov: 10, | ||
dec: 11 | ||
}; | ||
var _daysInYear = function(year) { | ||
if (! (year % 400)) { | ||
return 366; | ||
} | ||
if (! (year % 100)) { | ||
return 365; | ||
} | ||
if (! (year % 4)) { | ||
return 366; | ||
} | ||
return 365; | ||
} | ||
|
||
date.replace(/^\s*/, ''); //remove leading spaces without de-tainting. | ||
date.replace(/\s*$/, ''); | ||
|
||
//require Time::Local; | ||
// NOTE: This routine *will break* if input is not one of below formats! | ||
var tzadj = 0; // Zulu | ||
if (defaultLocal) { | ||
// Local time at midnight on the epoch gives us minus the | ||
// local difference. e.g. CST is GMT + 1, so midnight Jan 1 1970 CST | ||
// is -01:00Z. But we don't want to give you that! Because it's | ||
// wrong on Winblows, where localtime() of a negative number gives | ||
// undef, resulting in a mad tzadj. So we simply offset the | ||
// base by 24 hours (86400 seconds). The params are simply the | ||
// result of gmtime(86400); | ||
/////tzadj = 86400 - Time::Local::timelocal(0, 0, 0, 2, 0, 70, 5, 1, 0); | ||
} | ||
|
||
// try "31 Dec 2001 - 23:59" (Foswiki date) | ||
// or "31 Dec 2001" | ||
//TODO: allow /.: too | ||
var foswikiDate = new RegExp(/(\d+)[-\s]+([a-z]{3})[-\s]+(\d+)(?:[-\s]+(\d+):(\d+))?/i); | ||
var mymatch = foswikiDate.exec(date); | ||
if (mymatch != undefined) { | ||
// if ( date =~ /(\d+)[-\s]+([a-z]{3})[-\s]+(\d+)(?:[-\s]+(\d+):(\d+))?/i ) { | ||
var year = parseInt(mymatch[3]); | ||
|
||
//TODO: %MON2NUM needs to be updated to use i8n | ||
//TODO: and should really work for long form of the month name too. | ||
return perlOrderDateInit(0, parseInt(mymatch[5]) || 0, parseInt(mymatch[4]) || 0, parseInt(mymatch[1]), MON2NUM[mymatch[2].toLowerCase()], year); // - tzadj; | ||
} | ||
|
||
// ISO date 2001-12-31T23:59:59+01:00 | ||
// Sven is going to presume that _all_ ISO dated must have a 'T' in them. | ||
var isoDate = new RegExp(/(\d\d\d\d)(?:-(\d\d)(?:-(\d\d))?)?(?:T(\d\d)(?::(\d\d)(?::(\d\d(?:\.\d+)?))?)?)?(Z|[-+]\d\d(?::\d\d)?)?/); | ||
mymatch = isoDate.exec(date); | ||
if ((date.match(/T/)) && (mymatch != undefined) | ||
// && ( date =~ | ||
// /(\d\d\d\d)(?:-(\d\d)(?:-(\d\d))?)?(?:T(\d\d)(?::(\d\d)(?::(\d\d(?:\.\d+)?))?)?)?(Z|[-+]\d\d(?::\d\d)?)?/ | ||
// ) | ||
) { | ||
//var ( Y, M, D, h, M, s, tz ) = | ||
// ( mymatch[1], mymatch[2] || 1, mymatch[3] || 1, mymatch[4] || 0, mymatch[5] || 0, mymatch[6] || 0, mymatch[7] || '' ); | ||
var Y = parseInt(mymatch[1]); | ||
var M = parseInt(mymatch[2]) || 1; | ||
var D = parseInt(mymatch[3]) || 1; | ||
var h = parseInt(mymatch[4]) || 0; | ||
var m = parseInt(mymatch[5]) || 0; | ||
var s = parseInt(mymatch[6]) || 0; | ||
var tz = parseInt(mymatch[7]) || ''; | ||
|
||
M--; | ||
if (tz == 'Z') { | ||
tzadj = 0; // Zulu | ||
} | ||
else { | ||
var tzRegExp = new RegExp(/([-+])(\d\d)(?::(\d\d))?/); | ||
mymatch = tzRegExp.exec(tz); | ||
if (mymatch != undefined) { | ||
//if ( tz =~ /([-+])(\d\d)(?::(\d\d))?/ ) { | ||
tzadj = (mymatch[1] || '') + (((parseInt(mymatch[2]) * 60) + (parseInt(mymatch[3]) || 0)) * 60); | ||
tzadj -= 0; | ||
} | ||
} | ||
return perlOrderDateInit(s, m, h, D, M, Y); // - tzadj; | ||
} | ||
|
||
//any date that leads with a year (2 digit years too) | ||
// date =~ m|^(\d\d+)(?:\s*[/\s.-]\s*(\d\d?)(?:\s*[/\s.-]\s*(\d\d?)(?:\s*[/\s.-]\s*(\d\d?)(?:\s*[:.]\s*(\d\d?)(?:\s*[:.]\s*(\d\d?))?)?)?)?)?$|x | ||
var YYYYMMdddRegExp = new RegExp(/^(\d\d+)(?:\s*[/\s.-]\s*(\d\d?)(?:\s*[/\s.-]\s*(\d\d?)(?:\s*[/\s.-]\s*(\d\d?)(?:\s*[:.]\s*(\d\d?)(?:\s*[:.]\s*(\d\d?))?)?)?)?)?$/); | ||
mymatch = YYYYMMdddRegExp.exec(date) | ||
if (mymatch != undefined) { | ||
|
||
//no defaulting yet so we can detect the 2009--12 error | ||
//var ( year, M, D, h, M, s ) = ( mymatch[1], mymatch[2], mymatch[3], mymatch[4], mymatch[5], mymatch[6] ); | ||
var year = parseInt(mymatch[1]); | ||
var M = parseInt(mymatch[2]); | ||
var D = parseInt(mymatch[3]); | ||
var h = parseInt(mymatch[4]); | ||
var m = parseInt(mymatch[5]); | ||
var s = parseInt(mymatch[6]); | ||
var tz = parseInt(mymatch[7]); | ||
|
||
//console.log('*****('+mymatch[1]+' , '+mymatch[2]); | ||
//console.log('=====('+parseInt(mymatch[1])+' , '+parseInt(mymatch[2])); | ||
//console.log('=====('+year+' , '+M); | ||
//without range checking on the 12 Jan 2009 case above, there is abmiguity - what is 14 Jan 12 ? | ||
//similarly, how would you decide what Jan 02 and 02 Jan are? | ||
//month_p = MON2NUM{ lc(month_p) } if (defined(MON2NUM{ lc(month_p) })); | ||
//range checks | ||
if (defined(M) && (M < 1 || M > 12)) { | ||
return null; | ||
} | ||
var month = (M || 1) - 1; | ||
var monthlength = MONTHLENS[month]; | ||
|
||
// If leap year, note February is month number 1 starting from 0 | ||
if (month == 1 && _daysInYear(year) == 366) { | ||
monthlength = 29; | ||
} | ||
if (defined(D) && (D < 0 || D > monthlength)) { | ||
return null; | ||
} | ||
if (defined(h) && (h < 0 || h > 24)) { | ||
return null; | ||
} | ||
if (defined(M) && (M < 0 || M > 60)) { | ||
return null; | ||
} | ||
if (defined(s) && (s < 0 || s > 60)) { | ||
return null; | ||
} | ||
|
||
var day = D || 1; | ||
var hour = h || 0; | ||
var min = m || 0; | ||
var sec = s || 0; | ||
|
||
return perlOrderDateInit(sec, min, hour, day, month, year); //- tzadj; | ||
} | ||
|
||
// try the js built in parser | ||
return new Date(date); | ||
} | ||
var parsedDate = parseTime(dateString); | ||
if (parsedDate == null) { | ||
return 0; | ||
} | ||
return parsedDate.getTime(); //return seconds since 1970 | ||
}%TMPL:END% |