Path: blob/trunk/third_party/closure/goog/date/utcdatetime.js
2868 views
// Copyright 2009 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Locale independent date/time class.16*17*/1819goog.provide('goog.date.UtcDateTime');2021goog.require('goog.date');22goog.require('goog.date.Date');23goog.require('goog.date.DateTime');24goog.require('goog.date.Interval');25262728/**29* Class representing a date/time in GMT+0 time zone, without daylight saving.30* Defaults to current date and time if none is specified. The get... and the31* getUTC... methods are equivalent.32*33* @param {number|goog.date.DateLike=} opt_year Four digit UTC year or a34* date-like object. If not set, the created object will contain the35* date determined by goog.now().36* @param {number=} opt_month UTC month, 0 = Jan, 11 = Dec.37* @param {number=} opt_date UTC date of month, 1 - 31.38* @param {number=} opt_hours UTC hours, 0 - 23.39* @param {number=} opt_minutes UTC minutes, 0 - 59.40* @param {number=} opt_seconds UTC seconds, 0 - 59.41* @param {number=} opt_milliseconds UTC milliseconds, 0 - 999.42* @constructor43* @struct44* @extends {goog.date.DateTime}45*/46goog.date.UtcDateTime = function(47opt_year, opt_month, opt_date, opt_hours, opt_minutes, opt_seconds,48opt_milliseconds) {49var timestamp;50if (goog.isNumber(opt_year)) {51timestamp = Date.UTC(52opt_year, opt_month || 0, opt_date || 1, opt_hours || 0,53opt_minutes || 0, opt_seconds || 0, opt_milliseconds || 0);54} else {55timestamp = opt_year ? opt_year.getTime() : goog.now();56}57this.date = new Date(timestamp);58};59goog.inherits(goog.date.UtcDateTime, goog.date.DateTime);606162/**63* @param {number} timestamp Number of milliseconds since Epoch.64* @return {!goog.date.UtcDateTime}65*/66goog.date.UtcDateTime.fromTimestamp = function(timestamp) {67var date = new goog.date.UtcDateTime();68date.setTime(timestamp);69return date;70};717273/**74* Creates a DateTime from a UTC datetime string expressed in ISO 8601 format.75*76* @param {string} formatted A date or datetime expressed in ISO 8601 format.77* @return {goog.date.UtcDateTime} Parsed date or null if parse fails.78*/79goog.date.UtcDateTime.fromIsoString = function(formatted) {80var ret = new goog.date.UtcDateTime(2000);81return goog.date.setIso8601DateTime(ret, formatted) ? ret : null;82};838485/**86* Clones the UtcDateTime object.87*88* @return {!goog.date.UtcDateTime} A clone of the datetime object.89* @override90*/91goog.date.UtcDateTime.prototype.clone = function() {92var date = new goog.date.UtcDateTime(this.date);93date.setFirstDayOfWeek(this.getFirstDayOfWeek());94date.setFirstWeekCutOffDay(this.getFirstWeekCutOffDay());95return date;96};979899/** @override */100goog.date.UtcDateTime.prototype.add = function(interval) {101if (interval.years || interval.months) {102var yearsMonths = new goog.date.Interval(interval.years, interval.months);103goog.date.Date.prototype.add.call(this, yearsMonths);104}105var daysAndTimeMillis = 1000 *106(interval.seconds +10760 * (interval.minutes + 60 * (interval.hours + 24 * interval.days)));108this.date = new Date(this.date.getTime() + daysAndTimeMillis);109};110111112/** @override */113goog.date.UtcDateTime.prototype.getTimezoneOffset = function() {114return 0;115};116117118/** @override */119goog.date.UtcDateTime.prototype.getFullYear =120goog.date.DateTime.prototype.getUTCFullYear;121122123/** @override */124goog.date.UtcDateTime.prototype.getMonth =125goog.date.DateTime.prototype.getUTCMonth;126127128/** @override */129goog.date.UtcDateTime.prototype.getDate =130goog.date.DateTime.prototype.getUTCDate;131132133/** @override */134goog.date.UtcDateTime.prototype.getHours =135goog.date.DateTime.prototype.getUTCHours;136137138/** @override */139goog.date.UtcDateTime.prototype.getMinutes =140goog.date.DateTime.prototype.getUTCMinutes;141142143/** @override */144goog.date.UtcDateTime.prototype.getSeconds =145goog.date.DateTime.prototype.getUTCSeconds;146147148/** @override */149goog.date.UtcDateTime.prototype.getMilliseconds =150goog.date.DateTime.prototype.getUTCMilliseconds;151152153/** @override */154goog.date.UtcDateTime.prototype.getDay = goog.date.DateTime.prototype.getUTCDay;155156157/** @override */158goog.date.UtcDateTime.prototype.setFullYear =159goog.date.DateTime.prototype.setUTCFullYear;160161162/** @override */163goog.date.UtcDateTime.prototype.setMonth =164goog.date.DateTime.prototype.setUTCMonth;165166167/** @override */168goog.date.UtcDateTime.prototype.setDate =169goog.date.DateTime.prototype.setUTCDate;170171172/** @override */173goog.date.UtcDateTime.prototype.setHours =174goog.date.DateTime.prototype.setUTCHours;175176177/** @override */178goog.date.UtcDateTime.prototype.setMinutes =179goog.date.DateTime.prototype.setUTCMinutes;180181182/** @override */183goog.date.UtcDateTime.prototype.setSeconds =184goog.date.DateTime.prototype.setUTCSeconds;185186187/** @override */188goog.date.UtcDateTime.prototype.setMilliseconds =189goog.date.DateTime.prototype.setUTCMilliseconds;190191192