utcdatetime.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. * @fileoverview Locale independent date/time class.
  16. *
  17. */
  18. goog.provide('goog.date.UtcDateTime');
  19. goog.require('goog.date');
  20. goog.require('goog.date.Date');
  21. goog.require('goog.date.DateTime');
  22. goog.require('goog.date.Interval');
  23. /**
  24. * Class representing a date/time in GMT+0 time zone, without daylight saving.
  25. * Defaults to current date and time if none is specified. The get... and the
  26. * getUTC... methods are equivalent.
  27. *
  28. * @param {number|goog.date.DateLike=} opt_year Four digit UTC year or a
  29. * date-like object. If not set, the created object will contain the
  30. * date determined by goog.now().
  31. * @param {number=} opt_month UTC month, 0 = Jan, 11 = Dec.
  32. * @param {number=} opt_date UTC date of month, 1 - 31.
  33. * @param {number=} opt_hours UTC hours, 0 - 23.
  34. * @param {number=} opt_minutes UTC minutes, 0 - 59.
  35. * @param {number=} opt_seconds UTC seconds, 0 - 59.
  36. * @param {number=} opt_milliseconds UTC milliseconds, 0 - 999.
  37. * @constructor
  38. * @struct
  39. * @extends {goog.date.DateTime}
  40. */
  41. goog.date.UtcDateTime = function(
  42. opt_year, opt_month, opt_date, opt_hours, opt_minutes, opt_seconds,
  43. opt_milliseconds) {
  44. var timestamp;
  45. if (goog.isNumber(opt_year)) {
  46. timestamp = Date.UTC(
  47. opt_year, opt_month || 0, opt_date || 1, opt_hours || 0,
  48. opt_minutes || 0, opt_seconds || 0, opt_milliseconds || 0);
  49. } else {
  50. timestamp = opt_year ? opt_year.getTime() : goog.now();
  51. }
  52. this.date = new Date(timestamp);
  53. };
  54. goog.inherits(goog.date.UtcDateTime, goog.date.DateTime);
  55. /**
  56. * @param {number} timestamp Number of milliseconds since Epoch.
  57. * @return {!goog.date.UtcDateTime}
  58. */
  59. goog.date.UtcDateTime.fromTimestamp = function(timestamp) {
  60. var date = new goog.date.UtcDateTime();
  61. date.setTime(timestamp);
  62. return date;
  63. };
  64. /**
  65. * Creates a DateTime from a UTC datetime string expressed in ISO 8601 format.
  66. *
  67. * @param {string} formatted A date or datetime expressed in ISO 8601 format.
  68. * @return {goog.date.UtcDateTime} Parsed date or null if parse fails.
  69. */
  70. goog.date.UtcDateTime.fromIsoString = function(formatted) {
  71. var ret = new goog.date.UtcDateTime(2000);
  72. return goog.date.setIso8601DateTime(ret, formatted) ? ret : null;
  73. };
  74. /**
  75. * Clones the UtcDateTime object.
  76. *
  77. * @return {!goog.date.UtcDateTime} A clone of the datetime object.
  78. * @override
  79. */
  80. goog.date.UtcDateTime.prototype.clone = function() {
  81. var date = new goog.date.UtcDateTime(this.date);
  82. date.setFirstDayOfWeek(this.getFirstDayOfWeek());
  83. date.setFirstWeekCutOffDay(this.getFirstWeekCutOffDay());
  84. return date;
  85. };
  86. /** @override */
  87. goog.date.UtcDateTime.prototype.add = function(interval) {
  88. if (interval.years || interval.months) {
  89. var yearsMonths = new goog.date.Interval(interval.years, interval.months);
  90. goog.date.Date.prototype.add.call(this, yearsMonths);
  91. }
  92. var daysAndTimeMillis = 1000 *
  93. (interval.seconds +
  94. 60 * (interval.minutes + 60 * (interval.hours + 24 * interval.days)));
  95. this.date = new Date(this.date.getTime() + daysAndTimeMillis);
  96. };
  97. /** @override */
  98. goog.date.UtcDateTime.prototype.getTimezoneOffset = function() {
  99. return 0;
  100. };
  101. /** @override */
  102. goog.date.UtcDateTime.prototype.getFullYear =
  103. goog.date.DateTime.prototype.getUTCFullYear;
  104. /** @override */
  105. goog.date.UtcDateTime.prototype.getMonth =
  106. goog.date.DateTime.prototype.getUTCMonth;
  107. /** @override */
  108. goog.date.UtcDateTime.prototype.getDate =
  109. goog.date.DateTime.prototype.getUTCDate;
  110. /** @override */
  111. goog.date.UtcDateTime.prototype.getHours =
  112. goog.date.DateTime.prototype.getUTCHours;
  113. /** @override */
  114. goog.date.UtcDateTime.prototype.getMinutes =
  115. goog.date.DateTime.prototype.getUTCMinutes;
  116. /** @override */
  117. goog.date.UtcDateTime.prototype.getSeconds =
  118. goog.date.DateTime.prototype.getUTCSeconds;
  119. /** @override */
  120. goog.date.UtcDateTime.prototype.getMilliseconds =
  121. goog.date.DateTime.prototype.getUTCMilliseconds;
  122. /** @override */
  123. goog.date.UtcDateTime.prototype.getDay = goog.date.DateTime.prototype.getUTCDay;
  124. /** @override */
  125. goog.date.UtcDateTime.prototype.setFullYear =
  126. goog.date.DateTime.prototype.setUTCFullYear;
  127. /** @override */
  128. goog.date.UtcDateTime.prototype.setMonth =
  129. goog.date.DateTime.prototype.setUTCMonth;
  130. /** @override */
  131. goog.date.UtcDateTime.prototype.setDate =
  132. goog.date.DateTime.prototype.setUTCDate;
  133. /** @override */
  134. goog.date.UtcDateTime.prototype.setHours =
  135. goog.date.DateTime.prototype.setUTCHours;
  136. /** @override */
  137. goog.date.UtcDateTime.prototype.setMinutes =
  138. goog.date.DateTime.prototype.setUTCMinutes;
  139. /** @override */
  140. goog.date.UtcDateTime.prototype.setSeconds =
  141. goog.date.DateTime.prototype.setUTCSeconds;
  142. /** @override */
  143. goog.date.UtcDateTime.prototype.setMilliseconds =
  144. goog.date.DateTime.prototype.setUTCMilliseconds;