
org.hisrc.jsonix.Jsonix.Schema.XSD.DateAsDate.js Maven / Gradle / Ivy
The newest version!
Jsonix.Schema.XSD.DateAsDate = Jsonix.Class(Jsonix.Schema.XSD.Calendar, {
name : 'DateAsDate',
typeName : Jsonix.Schema.XSD.qname('date'),
parse : function(value, context, input, scope) {
var calendar = this.parseDate(value);
var date = new Date();
date.setFullYear(calendar.year);
date.setMonth(calendar.month - 1);
date.setDate(calendar.day);
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
date.setMilliseconds(0);
if (Jsonix.Util.Type.isNumber(calendar.fractionalSecond)) {
date.setMilliseconds(Math.floor(1000 * calendar.fractionalSecond));
}
var timezone;
var unknownTimezone;
var localTimezone = - date.getTimezoneOffset();
if (Jsonix.Util.NumberUtils.isInteger(calendar.timezone))
{
timezone = calendar.timezone;
unknownTimezone = false;
}
else
{
// Unknown timezone
timezone = localTimezone;
unknownTimezone = true;
}
//
var result = new Date(date.getTime() + (60000 * ( - timezone + localTimezone)));
if (unknownTimezone)
{
// null denotes "unknown timezone"
result.originalTimezone = null;
}
else
{
result.originalTimezone = timezone;
}
return result;
},
print : function(value, context, output, scope) {
Jsonix.Util.Ensure.ensureDate(value);
var localDate = new Date(value.getTime());
localDate.setHours(0);
localDate.setMinutes(0);
localDate.setSeconds(0);
localDate.setMilliseconds(0);
// Original timezone is unknown
if (value.originalTimezone === null)
{
return this.printDate(new Jsonix.XML.Calendar({
year : value.getFullYear(),
month : value.getMonth() + 1,
day : value.getDate()
}));
}
else
{
// If original timezone was known, correct and print the value with the timezone
if (Jsonix.Util.NumberUtils.isInteger(value.originalTimezone))
{
var correctedValue = new Date(value.getTime() - (60000 * (- value.originalTimezone - value.getTimezoneOffset())));
return this.printDate(new Jsonix.XML.Calendar({
year : correctedValue.getFullYear(),
month : correctedValue.getMonth() + 1,
day : correctedValue.getDate(),
timezone : value.originalTimezone
}));
}
// If original timezone was not specified, do not correct and use the local time zone
else
{
// We assume that the difference between the date value and local midnight
// should be interpreted as a timezone offset.
// In case there's no difference, we assume default/unknown timezone
var localTimezone = - value.getTime() + localDate.getTime();
if (localTimezone === 0) {
return this.printDate(new Jsonix.XML.Calendar({
year : value.getFullYear(),
month : value.getMonth() + 1,
day : value.getDate()
}));
} else {
var timezone = localTimezone - (60000 * value.getTimezoneOffset());
if (timezone >= -43200000) {
return this.printDate(new Jsonix.XML.Calendar({
year : value.getFullYear(),
month : value.getMonth() + 1,
day : value.getDate(),
timezone : Math.floor(timezone / 60000)
}));
} else {
var nextDay = new Date(value.getTime() + 86400000);
return this.printDate(new Jsonix.XML.Calendar({
year : nextDay.getFullYear(),
month : nextDay.getMonth() + 1,
day : nextDay.getDate(),
timezone : (Math.floor(timezone / 60000) + 1440)
}));
}
}
}
}
},
isInstance : function(value, context, scope) {
return Jsonix.Util.Type.isDate(value);
},
CLASS_NAME : 'Jsonix.Schema.XSD.DateAsDate'
});
Jsonix.Schema.XSD.DateAsDate.INSTANCE = new Jsonix.Schema.XSD.DateAsDate();
Jsonix.Schema.XSD.DateAsDate.INSTANCE.LIST = new Jsonix.Schema.XSD.List(Jsonix.Schema.XSD.DateAsDate.INSTANCE);
© 2015 - 2025 Weber Informatics LLC | Privacy Policy