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