Path: blob/trunk/third_party/closure/goog/date/duration.js
2868 views
// Copyright 2013 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 Functions for formatting duration values. Such as "3 days"16* "3 hours", "14 minutes", "2 hours 45 minutes".17*18*/1920goog.provide('goog.date.duration');2122goog.require('goog.i18n.DateTimeFormat');23goog.require('goog.i18n.MessageFormat');242526/**27* Number of milliseconds in a minute.28* @type {number}29* @private30*/31goog.date.duration.MINUTE_MS_ = 60000;323334/**35* Number of milliseconds in an hour.36* @type {number}37* @private38*/39goog.date.duration.HOUR_MS_ = 3600000;404142/**43* Number of milliseconds in a day.44* @type {number}45* @private46*/47goog.date.duration.DAY_MS_ = 86400000;484950/**51* Accepts a duration in milliseconds and outputs an absolute duration time in52* form of "1 day", "2 hours", "20 minutes", "2 days 1 hour 15 minutes" etc.53* @param {number} durationMs Duration in milliseconds.54* @return {string} The formatted duration.55*/56goog.date.duration.format = function(durationMs) {57var ms = Math.abs(durationMs);5859// Handle durations shorter than 1 minute.60if (ms < goog.date.duration.MINUTE_MS_) {61/**62* @desc Duration time of zero minutes.63*/64var MSG_ZERO_MINUTES = goog.getMsg('0 minutes');65return MSG_ZERO_MINUTES;66}6768var days = Math.floor(ms / goog.date.duration.DAY_MS_);69ms %= goog.date.duration.DAY_MS_;7071var hours = Math.floor(ms / goog.date.duration.HOUR_MS_);72ms %= goog.date.duration.HOUR_MS_;7374var minutes = Math.floor(ms / goog.date.duration.MINUTE_MS_);7576// Localized number representations.77var daysText = goog.i18n.DateTimeFormat.localizeNumbers(days);78var hoursText = goog.i18n.DateTimeFormat.localizeNumbers(hours);79var minutesText = goog.i18n.DateTimeFormat.localizeNumbers(minutes);8081// We need a space after the days if there are hours or minutes to come.82var daysSeparator = days * (hours + minutes) ? ' ' : '';83// We need a space after the hours if there are minutes to come.84var hoursSeparator = hours * minutes ? ' ' : '';8586/**87* @desc The days part of the duration message: 1 day, 5 days.88*/89var MSG_DURATION_DAYS = goog.getMsg(90'{COUNT, plural, ' +91'=0 {}' +92'=1 {{TEXT} day}' +93'other {{TEXT} days}}');94/**95* @desc The hours part of the duration message: 1 hour, 5 hours.96*/97var MSG_DURATION_HOURS = goog.getMsg(98'{COUNT, plural, ' +99'=0 {}' +100'=1 {{TEXT} hour}' +101'other {{TEXT} hours}}');102/**103* @desc The minutes part of the duration message: 1 minute, 5 minutes.104*/105var MSG_DURATION_MINUTES = goog.getMsg(106'{COUNT, plural, ' +107'=0 {}' +108'=1 {{TEXT} minute}' +109'other {{TEXT} minutes}}');110111var daysPart = goog.date.duration.getDurationMessagePart_(112MSG_DURATION_DAYS, days, daysText);113var hoursPart = goog.date.duration.getDurationMessagePart_(114MSG_DURATION_HOURS, hours, hoursText);115var minutesPart = goog.date.duration.getDurationMessagePart_(116MSG_DURATION_MINUTES, minutes, minutesText);117118/**119* @desc Duration time text concatenated from the individual time unit message120* parts. The separator will be a space (e.g. '1 day 2 hours 24 minutes') or121* nothing in case one/two of the duration parts is empty (122* e.g. '1 hour 30 minutes', '3 days 15 minutes', '2 hours').123*/124var MSG_CONCATENATED_DURATION_TEXT = goog.getMsg(125'{$daysPart}{$daysSeparator}{$hoursPart}{$hoursSeparator}{$minutesPart}',126{127'daysPart': daysPart,128'daysSeparator': daysSeparator,129'hoursPart': hoursPart,130'hoursSeparator': hoursSeparator,131'minutesPart': minutesPart132});133134return MSG_CONCATENATED_DURATION_TEXT;135};136137138/**139* Gets a duration message part for a time unit.140* @param {string} pattern The pattern to apply.141* @param {number} count The number of units.142* @param {string} text The string to use for amount of units in the message.143* @return {string} The formatted message part.144* @private145*/146goog.date.duration.getDurationMessagePart_ = function(pattern, count, text) {147var formatter = new goog.i18n.MessageFormat(pattern);148return formatter.format({'COUNT': count, 'TEXT': text});149};150151152