relative.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 Functions for formatting relative dates. Such as "3 days ago"
  16. * "3 hours ago", "14 minutes ago", "12 days ago", "Today", "Yesterday".
  17. *
  18. * For better quality localization of plurals ("hours"/"minutes"/"days") and
  19. * to use local digits, goog.date.relativeWithPlurals can be loaded in addition
  20. * to this namespace.
  21. *
  22. */
  23. goog.provide('goog.date.relative');
  24. goog.provide('goog.date.relative.TimeDeltaFormatter');
  25. goog.provide('goog.date.relative.Unit');
  26. goog.require('goog.i18n.DateTimeFormat');
  27. goog.require('goog.i18n.DateTimePatterns');
  28. /**
  29. * Number of milliseconds in a minute.
  30. * @type {number}
  31. * @private
  32. */
  33. goog.date.relative.MINUTE_MS_ = 60000;
  34. /**
  35. * Number of milliseconds in a day.
  36. * @type {number}
  37. * @private
  38. */
  39. goog.date.relative.DAY_MS_ = 86400000;
  40. /**
  41. * Enumeration used to identify time units internally.
  42. * @enum {number}
  43. */
  44. goog.date.relative.Unit = {
  45. MINUTES: 0,
  46. HOURS: 1,
  47. DAYS: 2
  48. };
  49. /**
  50. * Full date formatter.
  51. * @type {goog.i18n.DateTimeFormat}
  52. * @private
  53. */
  54. goog.date.relative.fullDateFormatter_;
  55. /**
  56. * Short time formatter.
  57. * @type {goog.i18n.DateTimeFormat}
  58. * @private
  59. */
  60. goog.date.relative.shortTimeFormatter_;
  61. /**
  62. * Month-date formatter.
  63. * @type {goog.i18n.DateTimeFormat}
  64. * @private
  65. */
  66. goog.date.relative.monthDateFormatter_;
  67. /**
  68. * @typedef {function(number, boolean, goog.date.relative.Unit): string}
  69. */
  70. goog.date.relative.TimeDeltaFormatter;
  71. /**
  72. * Handles formatting of time deltas.
  73. * @private {goog.date.relative.TimeDeltaFormatter}
  74. */
  75. goog.date.relative.formatTimeDelta_;
  76. /**
  77. * Sets a different formatting function for time deltas ("3 days ago").
  78. * While its visibility is public, this function is Closure-internal and should
  79. * not be used in application code.
  80. * @param {goog.date.relative.TimeDeltaFormatter} formatter The function to use
  81. * for formatting time deltas (i.e. relative times).
  82. */
  83. goog.date.relative.setTimeDeltaFormatter = function(formatter) {
  84. goog.date.relative.formatTimeDelta_ = formatter;
  85. };
  86. /**
  87. * Returns a date in month format, e.g. Mar 15.
  88. * @param {Date} date The date object.
  89. * @return {string} The formatted string.
  90. * @private
  91. */
  92. goog.date.relative.formatMonth_ = function(date) {
  93. if (!goog.date.relative.monthDateFormatter_) {
  94. goog.date.relative.monthDateFormatter_ =
  95. new goog.i18n.DateTimeFormat(goog.i18n.DateTimePatterns.MONTH_DAY_ABBR);
  96. }
  97. return goog.date.relative.monthDateFormatter_.format(date);
  98. };
  99. /**
  100. * Returns a date in short-time format, e.g. 2:50 PM.
  101. * @param {Date|goog.date.DateTime} date The date object.
  102. * @return {string} The formatted string.
  103. * @private
  104. */
  105. goog.date.relative.formatShortTime_ = function(date) {
  106. if (!goog.date.relative.shortTimeFormatter_) {
  107. goog.date.relative.shortTimeFormatter_ = new goog.i18n.DateTimeFormat(
  108. goog.i18n.DateTimeFormat.Format.SHORT_TIME);
  109. }
  110. return goog.date.relative.shortTimeFormatter_.format(date);
  111. };
  112. /**
  113. * Returns a date in full date format, e.g. Tuesday, March 24, 2009.
  114. * @param {Date|goog.date.DateTime} date The date object.
  115. * @return {string} The formatted string.
  116. * @private
  117. */
  118. goog.date.relative.formatFullDate_ = function(date) {
  119. if (!goog.date.relative.fullDateFormatter_) {
  120. goog.date.relative.fullDateFormatter_ =
  121. new goog.i18n.DateTimeFormat(goog.i18n.DateTimeFormat.Format.FULL_DATE);
  122. }
  123. return goog.date.relative.fullDateFormatter_.format(date);
  124. };
  125. /**
  126. * Accepts a timestamp in milliseconds and outputs a relative time in the form
  127. * of "1 hour ago", "1 day ago", "in 1 hour", "in 2 days" etc. If the date
  128. * delta is over 2 weeks, then the output string will be empty.
  129. * @param {number} dateMs Date in milliseconds.
  130. * @return {string} The formatted date.
  131. */
  132. goog.date.relative.format = function(dateMs) {
  133. var now = goog.now();
  134. var delta = Math.floor((now - dateMs) / goog.date.relative.MINUTE_MS_);
  135. var future = false;
  136. if (delta < 0) {
  137. future = true;
  138. delta *= -1;
  139. }
  140. if (delta < 60) { // Minutes.
  141. return goog.date.relative.formatTimeDelta_(
  142. delta, future, goog.date.relative.Unit.MINUTES);
  143. } else {
  144. delta = Math.floor(delta / 60);
  145. if (delta < 24) { // Hours.
  146. return goog.date.relative.formatTimeDelta_(
  147. delta, future, goog.date.relative.Unit.HOURS);
  148. } else {
  149. // We can be more than 24 hours apart but still only 1 day apart, so we
  150. // compare the closest time from today against the target time to find
  151. // the number of days in the delta.
  152. var midnight = new Date(goog.now());
  153. midnight.setHours(0);
  154. midnight.setMinutes(0);
  155. midnight.setSeconds(0);
  156. midnight.setMilliseconds(0);
  157. // Convert to days ago.
  158. delta =
  159. Math.ceil((midnight.getTime() - dateMs) / goog.date.relative.DAY_MS_);
  160. if (future) {
  161. delta *= -1;
  162. }
  163. // Uses days for less than 2-weeks.
  164. if (delta < 14) {
  165. return goog.date.relative.formatTimeDelta_(
  166. delta, future, goog.date.relative.Unit.DAYS);
  167. } else {
  168. // For messages older than 2 weeks do not show anything. The client
  169. // should decide the date format to show.
  170. return '';
  171. }
  172. }
  173. }
  174. };
  175. /**
  176. * Accepts a timestamp in milliseconds and outputs a relative time in the form
  177. * of "1 hour ago", "1 day ago". All future times will be returned as 0 minutes
  178. * ago.
  179. *
  180. * This is provided for compatibility with users of the previous incarnation of
  181. * the above {@see #format} method who relied on it protecting against
  182. * future dates.
  183. *
  184. * @param {number} dateMs Date in milliseconds.
  185. * @return {string} The formatted date.
  186. */
  187. goog.date.relative.formatPast = function(dateMs) {
  188. var now = goog.now();
  189. if (now < dateMs) {
  190. dateMs = now;
  191. }
  192. return goog.date.relative.format(dateMs);
  193. };
  194. /**
  195. * Accepts a timestamp in milliseconds and outputs a relative day. i.e. "Today",
  196. * "Yesterday", "Tomorrow", or "Sept 15".
  197. *
  198. * @param {number} dateMs Date in milliseconds.
  199. * @param {function(!Date):string=} opt_formatter Formatter for the date.
  200. * Defaults to form 'MMM dd'.
  201. * @return {string} The formatted date.
  202. */
  203. goog.date.relative.formatDay = function(dateMs, opt_formatter) {
  204. var today = new Date(goog.now());
  205. today.setHours(0);
  206. today.setMinutes(0);
  207. today.setSeconds(0);
  208. today.setMilliseconds(0);
  209. var yesterday = new Date(today.getTime() - goog.date.relative.DAY_MS_);
  210. var tomorrow = new Date(today.getTime() + goog.date.relative.DAY_MS_);
  211. var dayAfterTomorrow =
  212. new Date(today.getTime() + 2 * goog.date.relative.DAY_MS_);
  213. var message;
  214. if (dateMs >= tomorrow.getTime() && dateMs < dayAfterTomorrow.getTime()) {
  215. /** @desc Tomorrow. */
  216. var MSG_TOMORROW = goog.getMsg('Tomorrow');
  217. message = MSG_TOMORROW;
  218. } else if (dateMs >= today.getTime() && dateMs < tomorrow.getTime()) {
  219. /** @desc Today. */
  220. var MSG_TODAY = goog.getMsg('Today');
  221. message = MSG_TODAY;
  222. } else if (dateMs >= yesterday.getTime() && dateMs < today.getTime()) {
  223. /** @desc Yesterday. */
  224. var MSG_YESTERDAY = goog.getMsg('Yesterday');
  225. message = MSG_YESTERDAY;
  226. } else {
  227. // If we don't have a special relative term for this date, then return the
  228. // short date format (or a custom-formatted date).
  229. var formatFunction = opt_formatter || goog.date.relative.formatMonth_;
  230. message = formatFunction(new Date(dateMs));
  231. }
  232. return message;
  233. };
  234. /**
  235. * Formats a date, adding the relative date in parenthesis. If the date is less
  236. * than 24 hours then the time will be printed, otherwise the full-date will be
  237. * used. Examples:
  238. * 2:20 PM (1 minute ago)
  239. * Monday, February 27, 2009 (4 days ago)
  240. * Tuesday, March 20, 2005 // Too long ago for a relative date.
  241. *
  242. * @param {Date|goog.date.DateTime} date A date object.
  243. * @param {string=} opt_shortTimeMsg An optional short time message can be
  244. * provided if available, so that it's not recalculated in this function.
  245. * @param {string=} opt_fullDateMsg An optional date message can be
  246. * provided if available, so that it's not recalculated in this function.
  247. * @return {string} The date string in the above form.
  248. */
  249. goog.date.relative.getDateString = function(
  250. date, opt_shortTimeMsg, opt_fullDateMsg) {
  251. return goog.date.relative.getDateString_(
  252. date, goog.date.relative.format, opt_shortTimeMsg, opt_fullDateMsg);
  253. };
  254. /**
  255. * Formats a date, adding the relative date in parenthesis. Functions the same
  256. * as #getDateString but ensures that the date is always seen to be in the past.
  257. * If the date is in the future, it will be shown as 0 minutes ago.
  258. *
  259. * This is provided for compatibility with users of the previous incarnation of
  260. * the above {@see #getDateString} method who relied on it protecting against
  261. * future dates.
  262. *
  263. * @param {Date|goog.date.DateTime} date A date object.
  264. * @param {string=} opt_shortTimeMsg An optional short time message can be
  265. * provided if available, so that it's not recalculated in this function.
  266. * @param {string=} opt_fullDateMsg An optional date message can be
  267. * provided if available, so that it's not recalculated in this function.
  268. * @return {string} The date string in the above form.
  269. */
  270. goog.date.relative.getPastDateString = function(
  271. date, opt_shortTimeMsg, opt_fullDateMsg) {
  272. return goog.date.relative.getDateString_(
  273. date, goog.date.relative.formatPast, opt_shortTimeMsg, opt_fullDateMsg);
  274. };
  275. /**
  276. * Formats a date, adding the relative date in parenthesis. If the date is less
  277. * than 24 hours then the time will be printed, otherwise the full-date will be
  278. * used. Examples:
  279. * 2:20 PM (1 minute ago)
  280. * Monday, February 27, 2009 (4 days ago)
  281. * Tuesday, March 20, 2005 // Too long ago for a relative date.
  282. *
  283. * @param {Date|goog.date.DateTime} date A date object.
  284. * @param {function(number) : string} relativeFormatter Function to use when
  285. * formatting the relative date.
  286. * @param {string=} opt_shortTimeMsg An optional short time message can be
  287. * provided if available, so that it's not recalculated in this function.
  288. * @param {string=} opt_fullDateMsg An optional date message can be
  289. * provided if available, so that it's not recalculated in this function.
  290. * @return {string} The date string in the above form.
  291. * @private
  292. */
  293. goog.date.relative.getDateString_ = function(
  294. date, relativeFormatter, opt_shortTimeMsg, opt_fullDateMsg) {
  295. var dateMs = date.getTime();
  296. var relativeDate = relativeFormatter(dateMs);
  297. if (relativeDate) {
  298. relativeDate = ' (' + relativeDate + ')';
  299. }
  300. var delta = Math.floor((goog.now() - dateMs) / goog.date.relative.MINUTE_MS_);
  301. if (delta < 60 * 24) {
  302. // TODO(user): this call raises an exception if date is a goog.date.Date.
  303. return (opt_shortTimeMsg || goog.date.relative.formatShortTime_(date)) +
  304. relativeDate;
  305. } else {
  306. return (opt_fullDateMsg || goog.date.relative.formatFullDate_(date)) +
  307. relativeDate;
  308. }
  309. };
  310. /*
  311. * TODO(user):
  312. *
  313. * I think that this whole relative formatting should move to DateTimeFormat.
  314. * But we would have to wait for the next version of CLDR, which is cleaning
  315. * the data for relative dates (even ICU has incomplete support for this).
  316. */
  317. /**
  318. * Gets a localized relative date string for a given delta and unit.
  319. * @param {number} delta Number of minutes/hours/days.
  320. * @param {boolean} future Whether the delta is in the future.
  321. * @param {goog.date.relative.Unit} unit The units the delta is in.
  322. * @return {string} The message.
  323. * @private
  324. */
  325. goog.date.relative.getMessage_ = function(delta, future, unit) {
  326. var deltaFormatted = goog.i18n.DateTimeFormat.localizeNumbers(delta);
  327. if (!future && unit == goog.date.relative.Unit.MINUTES) {
  328. /**
  329. * @desc Relative date indicating how many minutes ago something happened
  330. * (singular).
  331. */
  332. var MSG_MINUTES_AGO_SINGULAR =
  333. goog.getMsg('{$num} minute ago', {'num': deltaFormatted});
  334. /**
  335. * @desc Relative date indicating how many minutes ago something happened
  336. * (plural).
  337. */
  338. var MSG_MINUTES_AGO_PLURAL =
  339. goog.getMsg('{$num} minutes ago', {'num': deltaFormatted});
  340. return delta == 1 ? MSG_MINUTES_AGO_SINGULAR : MSG_MINUTES_AGO_PLURAL;
  341. } else if (future && unit == goog.date.relative.Unit.MINUTES) {
  342. /**
  343. * @desc Relative date indicating in how many minutes something happens
  344. * (singular).
  345. */
  346. var MSG_IN_MINUTES_SINGULAR =
  347. goog.getMsg('in {$num} minute', {'num': deltaFormatted});
  348. /**
  349. * @desc Relative date indicating in how many minutes something happens
  350. * (plural).
  351. */
  352. var MSG_IN_MINUTES_PLURAL =
  353. goog.getMsg('in {$num} minutes', {'num': deltaFormatted});
  354. return delta == 1 ? MSG_IN_MINUTES_SINGULAR : MSG_IN_MINUTES_PLURAL;
  355. } else if (!future && unit == goog.date.relative.Unit.HOURS) {
  356. /**
  357. * @desc Relative date indicating how many hours ago something happened
  358. * (singular).
  359. */
  360. var MSG_HOURS_AGO_SINGULAR =
  361. goog.getMsg('{$num} hour ago', {'num': deltaFormatted});
  362. /**
  363. * @desc Relative date indicating how many hours ago something happened
  364. * (plural).
  365. */
  366. var MSG_HOURS_AGO_PLURAL =
  367. goog.getMsg('{$num} hours ago', {'num': deltaFormatted});
  368. return delta == 1 ? MSG_HOURS_AGO_SINGULAR : MSG_HOURS_AGO_PLURAL;
  369. } else if (future && unit == goog.date.relative.Unit.HOURS) {
  370. /**
  371. * @desc Relative date indicating in how many hours something happens
  372. * (singular).
  373. */
  374. var MSG_IN_HOURS_SINGULAR =
  375. goog.getMsg('in {$num} hour', {'num': deltaFormatted});
  376. /**
  377. * @desc Relative date indicating in how many hours something happens
  378. * (plural).
  379. */
  380. var MSG_IN_HOURS_PLURAL =
  381. goog.getMsg('in {$num} hours', {'num': deltaFormatted});
  382. return delta == 1 ? MSG_IN_HOURS_SINGULAR : MSG_IN_HOURS_PLURAL;
  383. } else if (!future && unit == goog.date.relative.Unit.DAYS) {
  384. /**
  385. * @desc Relative date indicating how many days ago something happened
  386. * (singular).
  387. */
  388. var MSG_DAYS_AGO_SINGULAR =
  389. goog.getMsg('{$num} day ago', {'num': deltaFormatted});
  390. /**
  391. * @desc Relative date indicating how many days ago something happened
  392. * (plural).
  393. */
  394. var MSG_DAYS_AGO_PLURAL =
  395. goog.getMsg('{$num} days ago', {'num': deltaFormatted});
  396. return delta == 1 ? MSG_DAYS_AGO_SINGULAR : MSG_DAYS_AGO_PLURAL;
  397. } else if (future && unit == goog.date.relative.Unit.DAYS) {
  398. /**
  399. * @desc Relative date indicating in how many days something happens
  400. * (singular).
  401. */
  402. var MSG_IN_DAYS_SINGULAR =
  403. goog.getMsg('in {$num} day', {'num': deltaFormatted});
  404. /**
  405. * @desc Relative date indicating in how many days something happens
  406. * (plural).
  407. */
  408. var MSG_IN_DAYS_PLURAL =
  409. goog.getMsg('in {$num} days', {'num': deltaFormatted});
  410. return delta == 1 ? MSG_IN_DAYS_SINGULAR : MSG_IN_DAYS_PLURAL;
  411. } else {
  412. return '';
  413. }
  414. };
  415. goog.date.relative.setTimeDeltaFormatter(goog.date.relative.getMessage_);