From d10b366a27dee30577afa349f594fc9c5eb75407 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Thu, 3 Oct 2013 11:11:25 +0200 Subject: [PATCH] Add constructor for Date and DateTime It is needed to be able to differentiate Date and DateTime because Javascript as only a datetime object named Date. review1115003 --- src/common.js | 3 ++- src/model.js | 4 ++-- src/pyson.js | 17 ++++++++--------- src/rpc.js | 22 ++++++++++------------ src/sao.js | 31 +++++++++++++++++++++++++++++++ tests/sao.js | 4 ++-- 6 files changed, 55 insertions(+), 26 deletions(-) diff --git a/src/common.js b/src/common.js index a99fb74ddc..838ad76a82 100644 --- a/src/common.js +++ b/src/common.js @@ -105,7 +105,8 @@ }; Sao.common.parse_datetime = function(date_format, time_format, value) { - var date = jQuery.datepicker.parseDate(date_format, value); + var date = Sao.DateTime( + jQuery.datepicker.parseDate(date_format, value)); var time_value = value.replace(jQuery.datepicker.formatDate( date_format + ' ', date), ''); var time = Sao.common.parse_time(time_format, time_value); diff --git a/src/model.js b/src/model.js index a7e5489729..f6b394fb1a 100644 --- a/src/model.js +++ b/src/model.js @@ -1153,8 +1153,8 @@ set_client: function(record, value, force_change) { if (!(value instanceof Date)) { try { - value = jQuery.datepicker.parseDate( - Sao.common.date_format(), value); + value = Sao.Date(jQuery.datepicker.parseDate( + Sao.common.date_format(), value)); } catch (e) { value = this._default; } diff --git a/src/pyson.js b/src/pyson.js index 2d4caa93b0..6f5bcfa0c7 100644 --- a/src/pyson.js +++ b/src/pyson.js @@ -26,8 +26,12 @@ if (v instanceof Sao.PYSON.PYSON) { return v.pyson(); } else if (v instanceof Date) { - if (v.getHours() || v.getMinutes() || v.getSeconds() || - v.getMilliseconds()) { + if (v.isDate) { + return Sao.PYSON.Date( + v.getFullYear(), + v.getMonth(), + v.getDate()).pyson(); + } else { return Sao.PYSON.DateTime( v.getFullYear(), v.getMonth(), @@ -36,11 +40,6 @@ v.getMinutes(), v.getSeconds(), v.getMilliseconds()).pyson(); - } else { - return Sao.PYSON.DateTime( - v.getFullYear(), - v.getMonth(), - v.getDate()).pyson(); } } return v; @@ -544,7 +543,7 @@ }); Sao.PYSON.Date.eval_ = function(value, context) { - var date = new Date(); + var date = Sao.Date(); date.setHours(0); date.setMinutes(0); date.setSeconds(0); @@ -610,7 +609,7 @@ }); Sao.PYSON.DateTime.eval_ = function(value, context) { - var date = new Date(); + var date = Sao.DateTime(); if (value.y) date.setFullYear(value.y); if (value.M) date.setMonth(value.M - 1); if (value.d) date.setDate(value.d); diff --git a/src/rpc.js b/src/rpc.js index 3e283e62f9..d903fcb72d 100644 --- a/src/rpc.js +++ b/src/rpc.js @@ -68,12 +68,12 @@ if (value && value.__class__) { switch (value.__class__) { case 'datetime': - value = new Date(Date.UTC(value.year, + value = Sao.DateTime(Date.UTC(value.year, value.month - 1, value.day, value.hour, value.minute, value.second)); break; case 'date': - value = new Date(value.year, + value = Sao.Date(value.year, value.month - 1, value.day); break; case 'time': @@ -117,9 +117,14 @@ } else if ((typeof(value) != 'string') && (typeof(value) != 'number') && (value !== null)) { if (value instanceof Date) { - if (value.getHours() || value.getMinutes() || - value.getSeconds()) - { + if (value.isDate){ + value = { + '__class__': 'date', + 'year': value.getFullYear(), + 'month': value.getMonth() + 1, + 'day': value.getDate() + }; + } else { value = { '__class__': 'datetime', 'year': value.getUTCFullYear(), @@ -129,13 +134,6 @@ 'minute': value.getUTCMinutes(), 'second': value.getUTCSeconds() }; - } else { - value = { - '__class__': 'date', - 'year': value.getFullYear(), - 'month': value.getMonth() + 1, - 'day': value.getDate() - }; } if (parent) { parent[index] = value; diff --git a/src/sao.js b/src/sao.js index 6f7099ce4a..8740fc17ed 100644 --- a/src/sao.js +++ b/src/sao.js @@ -35,6 +35,37 @@ var Sao = {}; Sao.Decimal = Number; + Sao.Date = function(year, month, day) { + var date; + if (year === undefined) { + date = new Date(); + } else if (date === undefined) { + date = new Date(year); + } else { + date = new Date(year, month, day); + } + date.isDate = true; + date.setHours(0); + date.setMinutes(0); + date.setSeconds(0); + date.setMilliseconds(0); + return date; + }; + + Sao.DateTime = function(year, month, day, hour, minute, second) { + var datetime; + if (year === undefined) { + datetime = new Date(); + } else if (month === undefined) { + datetime = new Date(year); + } else { + datetime = new Date(year, month, day, hour, minute, second); + } + datetime.isDateTime = true; + datetime.setMilliseconds(0); + return datetime; + }; + Sao.Time = Sao.class_(Object, { init: function(hour, minute, second) { this.date = new Date(0, 0, 0, hour, minute, second); diff --git a/tests/sao.js b/tests/sao.js index 59691ec3a1..ed10b70ce2 100644 --- a/tests/sao.js +++ b/tests/sao.js @@ -18,8 +18,8 @@ 'foo': true, 'bar': false }, - 'datetime': new Date(2012, 11, 29, 19, 59, 10), - 'date': new Date(2012, 11, 29), + 'datetime': Sao.DateTime(2012, 11, 29, 19, 59, 10), + 'date': Sao.Date(2012, 11, 29), 'decimal': new Sao.Decimal(1.1), 'null': null };