return this.length + ' ' + this.unit;
};
+function getTimezoneOffset(_timezone, _timeInMs) {
+ var result = native_.callSync('Time_getTimeZoneOffset', {
+ timezone: _timezone,
+ value: _timeInMs
+ });
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
+
+ return native_.getResultObject(result);
+}
+
+function getMsUTC(date, timezone) {
+ var ms_utc = Date.UTC(date.getUTCFullYear(),
+ date.getUTCMonth(),
+ date.getUTCDate(),
+ date.getUTCHours(),
+ date.getUTCMinutes(),
+ date.getUTCSeconds(),
+ date.getUTCMilliseconds());
+ if (arguments.length === 2) {
+ var result = native_.callSync('Time_getMsUTC', {
+ timezone: timezone,
+ value: ms_utc
+ });
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
+
+ ms_utc = native_.getResultObject(result);
+ }
+
+ return ms_utc;
+}
+
tizen.TZDate = function(year, month, day, hours, minutes, seconds, milliseconds, timezone) {
validator_.isConstructorCall(this, tizen.TZDate);
- this.timezone_ = timezone || tizen.time.getLocalTimezone();
+ if (timezone) {
+ if (tizen.time.getAvailableTimezones().indexOf(timezone) < 0) {
+ throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR);
+ }
+
+ this.timezone_ = timezone;
+ } else {
+ this.timezone_ = tizen.time.getLocalTimezone();
+ }
var hours = hours || 0;
var minutes = minutes || 0;
var seconds = seconds || 0;
var milliseconds = milliseconds || 0;
- if (!arguments.length)
+ if (!arguments.length) {
this.date_ = new Date();
- else if (arguments.length === 1 || arguments.length === 2) {
- if (arguments[0] instanceof Date)
+ } else if (arguments.length === 1 || arguments.length === 2) {
+ if (arguments[0] instanceof Date) {
this.date_ = arguments[0];
- else
+ } else {
this.date_ = new Date();
- if (arguments[1])
+ }
+ if (arguments[1]) {
+ if (tizen.time.getAvailableTimezones().indexOf(arguments[1]) < 0) {
+ throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR);
+ }
this.timezone_ = arguments[1];
+ }
+ } else {
+ this.date_ = {};
+ if (timezone) {
+ var d = new Date();
+ d.setUTCFullYear(year);
+ d.setUTCMonth(month);
+ d.setUTCDate(day);
+ d.setUTCHours(hours);
+ d.setUTCMinutes(minutes);
+ d.setUTCSeconds(seconds);
+ d.setUTCMilliseconds(milliseconds);
+ this.date_ = new Date(getMsUTC(d, timezone));
+ } else {
+ this.date_ = new Date(year, month, day, hours, minutes, seconds, milliseconds);
+ }
}
- else
- this.date_ = new Date(year, month, day, hours, minutes, seconds, milliseconds);
-
- if (tizen.time.getAvailableTimezones().indexOf(this.timezone_) < 0)
- throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR);
};
-function getTimezoneOffset(_timezone, _timeInMs) {
- var result = native_.callSync('Time_getTimeZoneOffset', {
- timezone: _timezone,
- value: _timeInMs
- });
- if (native_.isFailure(result)) {
- throw native_.getErrorObject(result);
- }
-
- return native_.getResultObject(result);
-}
-
-function _getTimeWithOffset(date) {
- var OffsetInMilliseconds = date.getTimezoneOffset() * _minuteInMilliseconds * -1;
- return date.getTime() + OffsetInMilliseconds;
-}
-
tizen.TZDate.prototype.getDate = function() {
return this.date_.getDate();
};
};
tizen.TZDate.prototype.getHours = function() {
- return this.date_.getHours();
+ var d = this.addDuration(new tizen.TimeDuration(getTimezoneOffset(this.timezone_,
+ getMsUTC(this.date_))));
+ return d.date_.getUTCHours();
};
tizen.TZDate.prototype.setHours = function() {
tizen.TZDate.prototype.getUTCDate = function() {
var d = this.addDuration(new tizen.TimeDuration(getTimezoneOffset(this.timezone_,
- _getTimeWithOffset(this.date_)) * -1));
+ getMsUTC(this.date_)) * -1));
return d.getDate();
};
tizen.TZDate.prototype.getUTCDay = function() {
var d = this.addDuration(new tizen.TimeDuration(getTimezoneOffset(this.timezone_,
- _getTimeWithOffset(this.date_)) * -1));
+ getMsUTC(this.date_)) * -1));
return d.getDay();
};
tizen.TZDate.prototype.getUTCFullYear = function() {
var d = this.addDuration(new tizen.TimeDuration(getTimezoneOffset(this.timezone_,
- _getTimeWithOffset(this.date_)) * -1));
+ getMsUTC(this.date_)) * -1));
return d.getFullYear();
};
};
tizen.TZDate.prototype.getUTCHours = function() {
- var d = this.addDuration(new tizen.TimeDuration(getTimezoneOffset(this.timezone_,
- _getTimeWithOffset(this.date_)) * -1));
- return d.getHours();
+ return this.date_.getUTCHours();
};
tizen.TZDate.prototype.setUTCHours = function() {
type: types_.LONG
}]);
- var offset_hours = getTimezoneOffset(this.timezone_, _getTimeWithOffset(this.date_)) /
+ var offset_hours = getTimezoneOffset(this.timezone_, getMsUTC(this.date_)) /
_hourInMilliseconds;
this.date_.setHours(args.hours + offset_hours);
};
tizen.TZDate.prototype.getUTCMilliseconds = function() {
var d = this.addDuration(new tizen.TimeDuration(getTimezoneOffset(this.timezone_,
- _getTimeWithOffset(this.date_)) * -1));
+ getMsUTC(this.date_)) * -1));
return d.getMilliseconds();
};
tizen.TZDate.prototype.getUTCMinutes = function() {
var d = this.addDuration(new tizen.TimeDuration(getTimezoneOffset(this.timezone_,
- _getTimeWithOffset(this.date_)) * -1));
+ getMsUTC(this.date_)) * -1));
return d.getMinutes();
};
tizen.TZDate.prototype.getUTCMonth = function() {
var d = this.addDuration(new tizen.TimeDuration(getTimezoneOffset(this.timezone_,
- _getTimeWithOffset(this.date_)) * -1));
+ getMsUTC(this.date_)) * -1));
return d.getMonth();
};
tizen.TZDate.prototype.getUTCSeconds = function() {
var d = this.addDuration(new tizen.TimeDuration(getTimezoneOffset(this.timezone_,
- _getTimeWithOffset(this.date_)) * -1));
+ getMsUTC(this.date_)) * -1));
return d.getSeconds();
};
tizen.TZDate.prototype.isDST = function() {
var result = native_.callSync('Time_isDST', {
timezone: this.timezone_,
- value: _getTimeWithOffset(this.date_)
+ value: getMsUTC(this.date_)
});
if (native_.isFailure(result)) {
return false;
tizen.TZDate.prototype.getPreviousDSTTransition = function() {
var result = native_.callSync('Time_getDSTTransition', {
'timezone': this.timezone_,
- 'value': _getTimeWithOffset(this.date_),
+ 'value': getMsUTC(this.date_),
'trans': 'NEXT_TRANSITION'
});
if (native_.isFailure(result)) {
tizen.TZDate.prototype.getNextDSTTransition = function() {
var result = native_.callSync('Time_getDSTTransition', {
timezone: this.timezone_,
- value: _getTimeWithOffset(this.date_),
+ value: getMsUTC(this.date_),
trans: 'PREV_TRANSITION'
});
if (native_.isFailure(result)) {
TimeSetTimezoneChangeListener);
REGISTER_SYNC("Time_unsetTimezoneChangeListener",
TimeUnsetTimezoneChangeListener);
+ REGISTER_SYNC("Time_getMsUTC", TimeGetMsUTC);
#undef REGISTER_SYNC
#undef REGISTER_ASYNC
UErrorCode ec = U_ZERO_ERROR;
std::unique_ptr<TimeZone> timezone(TimeZone::createTimeZone(*id));
- std::unique_ptr<Calendar> cal(Calendar::createInstance(*timezone, ec));
- if (U_FAILURE(ec)) {
- LoggerE("Failed to create Calendar instance");
- ReportError(out);
- return;
- }
- cal->setTime(dateInMs, ec);
+ int32_t rawOffset = 0;
+ int32_t dstOffset = 0;
+ timezone->getOffset(dateInMs, false, rawOffset, dstOffset, ec);
if (U_FAILURE(ec)) {
- LoggerE("Failed to set time");
+ LoggerE("Failed to get timezone offset");
ReportError(out);
return;
}
-
- int32_t offset = timezone->getRawOffset();
-
- if (cal->inDaylightTime(ec)) offset += _hourInMilliseconds;
-
std::stringstream offsetStr;
- offsetStr << offset;
-
+ offsetStr << (rawOffset + dstOffset);
ReportSuccess(JsonValue(offsetStr.str()), out);
}
}
UErrorCode ec = U_ZERO_ERROR;
- std::unique_ptr<Calendar> cal(
- Calendar::createInstance(TimeZone::createTimeZone(*id), ec));
+ std::unique_ptr<TimeZone> timezone(TimeZone::createTimeZone(*id));
+ std::unique_ptr<Calendar> cal(Calendar::createInstance(*timezone, ec));
if (U_FAILURE(ec)) {
LoggerE("Failed to create Calendar instance");
ReportError(out);
std::unique_ptr<UnicodeString> id(
new UnicodeString(args.get("timezone").to_str().c_str()));
UDate dateInMs = strtod(args.get("value").to_str().c_str(), NULL);
- dateInMs -= _hourInMilliseconds;
if (errno == ERANGE) {
LoggerE("Value out of range");
}
UErrorCode ec = U_ZERO_ERROR;
- std::unique_ptr<Calendar> cal(
- Calendar::createInstance(TimeZone::createTimeZone(*id), ec));
- if (U_FAILURE(ec)) {
- LoggerE("Failed to create Calendar instance");
- ReportError(out);
- return;
- }
+ std::unique_ptr<TimeZone> timezone(TimeZone::createTimeZone(*id));
- cal->setTime(dateInMs, ec);
+ int32_t rawOffset = 0;
+ int32_t dstOffset = 0;
+ timezone->getOffset(dateInMs, false, rawOffset, dstOffset, ec);
if (U_FAILURE(ec)) {
- LoggerE("Failed to set time");
+ LoggerE("Failed to get timezone offset");
ReportError(out);
return;
}
-
- ReportSuccess(JsonValue{static_cast<bool>(cal->inDaylightTime(ec))}, out);
+ ReportSuccess(JsonValue{static_cast<bool>(dstOffset)}, out);
}
void TimeInstance::TimeGetDSTTransition(const JsonValue& args,
}
UErrorCode ec = U_ZERO_ERROR;
- std::unique_ptr<Calendar> cal(
- Calendar::createInstance(TimeZone::createTimeZone(*id), ec));
+ std::unique_ptr<TimeZone> timezone(TimeZone::createTimeZone(*id));
+ std::unique_ptr<Calendar> cal(Calendar::createInstance(*timezone, ec));
if (U_FAILURE(ec)) {
LoggerE("Failed to create Calendar instance");
return false;
ReportSuccess(out);
}
+void TimeInstance::TimeGetMsUTC(const JsonValue& args, JsonObject& out) {
+ LoggerD("Entered");
+
+ std::unique_ptr<UnicodeString> id(new UnicodeString(args.get("timezone").to_str().c_str()));
+ UDate dateInMs = strtod(args.get("value").to_str().c_str(), NULL);
+ if (errno == ERANGE) {
+ LoggerE("Value out of range");
+ ReportError(out);
+ return;
+ }
+
+ UErrorCode ec = U_ZERO_ERROR;
+ std::unique_ptr<TimeZone> timezone(TimeZone::createTimeZone(*id));
+
+ int32_t rawOffset = 0;
+ int32_t dstOffset = 0;
+ timezone->getOffset(dateInMs, true, rawOffset, dstOffset, ec);
+ if (U_FAILURE(ec)) {
+ LoggerE("Failed to get timezone offset");
+ ReportError(out);
+ return;
+ }
+
+ dateInMs -= (rawOffset + dstOffset);
+
+ ReportSuccess(JsonValue{static_cast<double>(dateInMs)}, out);
+}
+
} // namespace time
} // namespace extension