| 1 |
// Copyright 2009 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Locale independent date/time class. |
| 17 |
* |
| 18 |
*/ |
| 19 |
|
| 20 |
goog.provide('goog.date.UtcDateTime'); |
| 21 |
|
| 22 |
goog.require('goog.date'); |
| 23 |
goog.require('goog.date.Date'); |
| 24 |
goog.require('goog.date.DateTime'); |
| 25 |
goog.require('goog.date.Interval'); |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
/** |
| 30 |
* Class representing a date/time in GMT+0 time zone, without daylight saving. |
| 31 |
* Defaults to current date and time if none is specified. The get... and the |
| 32 |
* getUTC... methods are equivalent. |
| 33 |
* |
| 34 |
* @param {number|goog.date.DateLike=} opt_year Four digit UTC year or a |
| 35 |
* date-like object. If not set, the created object will contain the |
| 36 |
* date determined by goog.now(). |
| 37 |
* @param {number=} opt_month UTC month, 0 = Jan, 11 = Dec. |
| 38 |
* @param {number=} opt_date UTC date of month, 1 - 31. |
| 39 |
* @param {number=} opt_hours UTC hours, 0 - 23. |
| 40 |
* @param {number=} opt_minutes UTC minutes, 0 - 59. |
| 41 |
* @param {number=} opt_seconds UTC seconds, 0 - 59. |
| 42 |
* @param {number=} opt_milliseconds UTC milliseconds, 0 - 999. |
| 43 |
* @constructor |
| 44 |
* @struct |
| 45 |
* @extends {goog.date.DateTime} |
| 46 |
*/ |
| 47 |
goog.date.UtcDateTime = function( |
| 48 |
opt_year, opt_month, opt_date, opt_hours, opt_minutes, opt_seconds, |
| 49 |
opt_milliseconds) { |
| 50 |
var timestamp; |
| 51 |
if (goog.isNumber(opt_year)) { |
| 52 |
timestamp = Date.UTC( |
| 53 |
opt_year, opt_month || 0, opt_date || 1, opt_hours || 0, |
| 54 |
opt_minutes || 0, opt_seconds || 0, opt_milliseconds || 0); |
| 55 |
} else { |
| 56 |
timestamp = opt_year ? opt_year.getTime() : goog.now(); |
| 57 |
} |
| 58 |
/** @override */ |
| 59 |
this.date = new Date(timestamp); |
| 60 |
}; |
| 61 |
goog.inherits(goog.date.UtcDateTime, goog.date.DateTime); |
| 62 |
|
| 63 |
|
| 64 |
/** |
| 65 |
* @param {number} timestamp Number of milliseconds since Epoch. |
| 66 |
* @return {!goog.date.UtcDateTime} |
| 67 |
*/ |
| 68 |
goog.date.UtcDateTime.fromTimestamp = function(timestamp) { |
| 69 |
var date = new goog.date.UtcDateTime(); |
| 70 |
date.setTime(timestamp); |
| 71 |
return date; |
| 72 |
}; |
| 73 |
|
| 74 |
|
| 75 |
/** |
| 76 |
* Creates a DateTime from a UTC datetime string expressed in ISO 8601 format. |
| 77 |
* |
| 78 |
* @param {string} formatted A date or datetime expressed in ISO 8601 format. |
| 79 |
* @return {goog.date.UtcDateTime} Parsed date or null if parse fails. |
| 80 |
*/ |
| 81 |
goog.date.UtcDateTime.fromIsoString = function(formatted) { |
| 82 |
var ret = new goog.date.UtcDateTime(2000); |
| 83 |
return goog.date.setIso8601DateTime(ret, formatted) ? ret : null; |
| 84 |
}; |
| 85 |
|
| 86 |
|
| 87 |
/** |
| 88 |
* Clones the UtcDateTime object. |
| 89 |
* |
| 90 |
* @return {!goog.date.UtcDateTime} A clone of the datetime object. |
| 91 |
* @override |
| 92 |
*/ |
| 93 |
goog.date.UtcDateTime.prototype.clone = function() { |
| 94 |
var date = new goog.date.UtcDateTime(this.date); |
| 95 |
date.setFirstDayOfWeek(this.getFirstDayOfWeek()); |
| 96 |
date.setFirstWeekCutOffDay(this.getFirstWeekCutOffDay()); |
| 97 |
return date; |
| 98 |
}; |
| 99 |
|
| 100 |
|
| 101 |
/** @override */ |
| 102 |
goog.date.UtcDateTime.prototype.add = function(interval) { |
| 103 |
if (interval.years || interval.months) { |
| 104 |
var yearsMonths = new goog.date.Interval(interval.years, interval.months); |
| 105 |
goog.date.Date.prototype.add.call(this, yearsMonths); |
| 106 |
} |
| 107 |
var daysAndTimeMillis = 1000 * |
| 108 |
(interval.seconds + |
| 109 |
60 * (interval.minutes + 60 * (interval.hours + 24 * interval.days))); |
| 110 |
this.date = new Date(this.date.getTime() + daysAndTimeMillis); |
| 111 |
}; |
| 112 |
|
| 113 |
|
| 114 |
/** @override */ |
| 115 |
goog.date.UtcDateTime.prototype.getTimezoneOffset = function() { |
| 116 |
return 0; |
| 117 |
}; |
| 118 |
|
| 119 |
|
| 120 |
/** @override */ |
| 121 |
goog.date.UtcDateTime.prototype.getFullYear = |
| 122 |
goog.date.DateTime.prototype.getUTCFullYear; |
| 123 |
|
| 124 |
|
| 125 |
/** @override */ |
| 126 |
goog.date.UtcDateTime.prototype.getMonth = |
| 127 |
goog.date.DateTime.prototype.getUTCMonth; |
| 128 |
|
| 129 |
|
| 130 |
/** @override */ |
| 131 |
goog.date.UtcDateTime.prototype.getDate = |
| 132 |
goog.date.DateTime.prototype.getUTCDate; |
| 133 |
|
| 134 |
|
| 135 |
/** @override */ |
| 136 |
goog.date.UtcDateTime.prototype.getHours = |
| 137 |
goog.date.DateTime.prototype.getUTCHours; |
| 138 |
|
| 139 |
|
| 140 |
/** @override */ |
| 141 |
goog.date.UtcDateTime.prototype.getMinutes = |
| 142 |
goog.date.DateTime.prototype.getUTCMinutes; |
| 143 |
|
| 144 |
|
| 145 |
/** @override */ |
| 146 |
goog.date.UtcDateTime.prototype.getSeconds = |
| 147 |
goog.date.DateTime.prototype.getUTCSeconds; |
| 148 |
|
| 149 |
|
| 150 |
/** @override */ |
| 151 |
goog.date.UtcDateTime.prototype.getMilliseconds = |
| 152 |
goog.date.DateTime.prototype.getUTCMilliseconds; |
| 153 |
|
| 154 |
|
| 155 |
/** @override */ |
| 156 |
goog.date.UtcDateTime.prototype.getDay = goog.date.DateTime.prototype.getUTCDay; |
| 157 |
|
| 158 |
|
| 159 |
/** @override */ |
| 160 |
goog.date.UtcDateTime.prototype.setFullYear = |
| 161 |
goog.date.DateTime.prototype.setUTCFullYear; |
| 162 |
|
| 163 |
|
| 164 |
/** @override */ |
| 165 |
goog.date.UtcDateTime.prototype.setMonth = |
| 166 |
goog.date.DateTime.prototype.setUTCMonth; |
| 167 |
|
| 168 |
|
| 169 |
/** @override */ |
| 170 |
goog.date.UtcDateTime.prototype.setDate = |
| 171 |
goog.date.DateTime.prototype.setUTCDate; |
| 172 |
|
| 173 |
|
| 174 |
/** @override */ |
| 175 |
goog.date.UtcDateTime.prototype.setHours = |
| 176 |
goog.date.DateTime.prototype.setUTCHours; |
| 177 |
|
| 178 |
|
| 179 |
/** @override */ |
| 180 |
goog.date.UtcDateTime.prototype.setMinutes = |
| 181 |
goog.date.DateTime.prototype.setUTCMinutes; |
| 182 |
|
| 183 |
|
| 184 |
/** @override */ |
| 185 |
goog.date.UtcDateTime.prototype.setSeconds = |
| 186 |
goog.date.DateTime.prototype.setUTCSeconds; |
| 187 |
|
| 188 |
|
| 189 |
/** @override */ |
| 190 |
goog.date.UtcDateTime.prototype.setMilliseconds = |
| 191 |
goog.date.DateTime.prototype.setUTCMilliseconds; |