[FIX] OPW 575691: parse server datetime manually

datejs's parser completely fails over itself during CST/EST transition

bzr revid: xmo@openerp.com-20120803124956-e9zwqfm7l5wmvi8l
This commit is contained in:
Xavier Morel
2012-08-03 14:49:56 +02:00
parent c8f8e0d43f
commit 6f002dddd6
2 changed files with 77 additions and 18 deletions
+18 -18
View File
@@ -15,16 +15,19 @@ openerp.web.str_to_datetime = function(str) {
if(!str) {
return str;
}
var regex = /^(\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d)(?:\.\d+)?$/;
var regex = /^(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(?:\.\d+)?$/;
var res = regex.exec(str);
if ( !res ) {
throw new Error("'" + str + "' is not a valid datetime");
}
var obj = Date.parseExact(res[1] + " UTC", 'yyyy-MM-dd HH:mm:ss zzz');
if (! obj) {
throw new Error("'" + str + "' is not a valid datetime");
}
return obj;
return new Date(Date.UTC(
parseInt(res[1], 10),
parseInt(res[2], 10) - 1,
parseInt(res[3], 10),
parseInt(res[4], 10),
parseInt(res[5], 10),
parseInt(res[6], 10)
));
};
/**
@@ -42,16 +45,16 @@ openerp.web.str_to_date = function(str) {
if(!str) {
return str;
}
var regex = /^\d\d\d\d-\d\d-\d\d$/;
var regex = /^(\d\d\d\d)-(\d\d)-(\d\d)$/;
var res = regex.exec(str);
if ( !res ) {
throw new Error("'" + str + "' is not a valid date");
}
var obj = Date.parseExact(str, 'yyyy-MM-dd');
if (! obj) {
throw new Error("'" + str + "' is not a valid date");
}
return obj;
return new Date(
parseInt(res[1], 10),
parseInt(res[2], 10) - 1,
parseInt(res[3], 10)
);
};
/**
@@ -69,16 +72,13 @@ openerp.web.str_to_time = function(str) {
if(!str) {
return str;
}
var regex = /^(\d\d:\d\d:\d\d)(?:\.\d+)?$/;
var regex = /^(\d\d):(\d\d):(\d\d)(?:\.\d+)?$/;
var res = regex.exec(str);
if ( !res ) {
throw new Error("'" + str + "' is not a valid time");
}
var obj = Date.parseExact("1970-01-01 " + res[1], 'yyyy-MM-dd HH:mm:ss');
if (! obj) {
throw new Error("'" + str + "' is not a valid time");
}
return obj;
return new Date(
1970, 0, 1, parseInt(res[1], 10), parseInt(res[2], 10), parseInt(res[3], 10));
};
/*
+59
View File
@@ -25,6 +25,65 @@ $(document).ready(function () {
date2.getUTCHours(), date2.getUTCMinutes(), date2.getUTCSeconds()],
[2011, 12 - 1, 10, 0, 0, 0]);
});
test('Server datetime: CET -> EST', function () {
// NOTE: this test may only work in the EU (CET/EST), no idea how to handle *that*
// DST transition is at 01:00 UTC on a specific day
// http://en.wikipedia.org/wiki/European_Summer_Time
// right before transition
strictEqual(
openerp.web.str_to_datetime('2012-03-25 00:59:46').toString(),
new Date(Date.UTC(2012, 2, 25, 0, 59, 46)).toString());
// Right after transition
strictEqual(
openerp.web.str_to_datetime('2012-03-25 01:01:33').toString(),
new Date(Date.UTC(2012, 2, 25, 1, 1, 33)).toString());
strictEqual(
openerp.web.str_to_datetime('2012-03-25 01:24:33').toString(),
new Date(Date.UTC(2012, 2, 25, 1, 24, 33)).toString());
strictEqual(
openerp.web.str_to_datetime('2012-03-25 01:42:33').toString(),
new Date(Date.UTC(2012, 2, 25, 1, 42, 33)).toString());
strictEqual(
openerp.web.str_to_datetime('2012-03-25 02:05:14').toString(),
new Date(Date.UTC(2012, 2, 25, 2, 5, 14)).toString());
strictEqual(
openerp.web.str_to_datetime('2012-03-25 02:42:14').toString(),
new Date(Date.UTC(2012, 2, 25, 2, 42, 14)).toString());
// End of overlapping time
strictEqual(
openerp.web.str_to_datetime('2012-03-25 03:05:14').toString(),
new Date(Date.UTC(2012, 2, 25, 3, 5, 14)).toString());
});
test('Server datetime: EST -> CET', function () {
strictEqual(
openerp.web.str_to_datetime('2012-10-27 23:59:46').toString(),
new Date(Date.UTC(2012, 9, 27, 23, 59, 46)).toString());
strictEqual(
openerp.web.str_to_datetime('2012-10-28 00:59:46').toString(),
new Date(Date.UTC(2012, 9, 28, 0, 59, 46)).toString());
strictEqual(
openerp.web.str_to_datetime('2012-10-28 01:01:33').toString(),
new Date(Date.UTC(2012, 9, 28, 1, 1, 33)).toString());
strictEqual(
openerp.web.str_to_datetime('2012-10-28 01:24:33').toString(),
new Date(Date.UTC(2012, 9, 28, 1, 24, 33)).toString());
strictEqual(
openerp.web.str_to_datetime('2012-10-28 01:42:33').toString(),
new Date(Date.UTC(2012, 9, 28, 1, 42, 33)).toString());
strictEqual(
openerp.web.str_to_datetime('2012-10-28 02:05:14').toString(),
new Date(Date.UTC(2012, 9, 28, 2, 5, 14)).toString());
strictEqual(
openerp.web.str_to_datetime('2012-10-28 02:42:14').toString(),
new Date(Date.UTC(2012, 9, 28, 2, 42, 14)).toString());
strictEqual(
openerp.web.str_to_datetime('2012-10-28 03:05:14').toString(),
new Date(Date.UTC(2012, 9, 28, 3, 5, 14)).toString());
strictEqual(
openerp.web.str_to_datetime('2012-10-28 05:05:14').toString(),
new Date(Date.UTC(2012, 9, 28, 5, 5, 14)).toString());
});
test('Parse server date', function () {
var date = openerp.web.str_to_date("2009-05-04");
deepEqual(