Debian Codegraph @

closure-library/closure/goog/date/duration.js

raw
1 // Copyright 2013 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 Functions for formatting duration values. Such as "3 days"
17 * "3 hours", "14 minutes", "2 hours 45 minutes".
18 *
19 */
20
21 goog.provide('goog.date.duration');
22
23 goog.require('goog.i18n.DateTimeFormat');
24 goog.require('goog.i18n.MessageFormat');
25
26
27 /**
28 * Number of milliseconds in a minute.
29 * @type {number}
30 * @private
31 */
32 goog.date.duration.MINUTE_MS_ = 60000;
33
34
35 /**
36 * Number of milliseconds in an hour.
37 * @type {number}
38 * @private
39 */
40 goog.date.duration.HOUR_MS_ = 3600000;
41
42
43 /**
44 * Number of milliseconds in a day.
45 * @type {number}
46 * @private
47 */
48 goog.date.duration.DAY_MS_ = 86400000;
49
50
51 /**
52 * Accepts a duration in milliseconds and outputs an absolute duration time in
53 * form of "1 day", "2 hours", "20 minutes", "2 days 1 hour 15 minutes" etc.
54 * @param {number} durationMs Duration in milliseconds.
55 * @return {string} The formatted duration.
56 */
57 goog.date.duration.format = function(durationMs) {
58 var ms = Math.abs(durationMs);
59
60 // Handle durations shorter than 1 minute.
61 if (ms < goog.date.duration.MINUTE_MS_) {
62 /**
63 * @desc Duration time of zero minutes.
64 */
65 var MSG_ZERO_MINUTES = goog.getMsg('0 minutes');
66 return MSG_ZERO_MINUTES;
67 }
68
69 var days = Math.floor(ms / goog.date.duration.DAY_MS_);
70 ms %= goog.date.duration.DAY_MS_;
71
72 var hours = Math.floor(ms / goog.date.duration.HOUR_MS_);
73 ms %= goog.date.duration.HOUR_MS_;
74
75 var minutes = Math.floor(ms / goog.date.duration.MINUTE_MS_);
76
77 // Localized number representations.
78 var daysText = goog.i18n.DateTimeFormat.localizeNumbers(days);
79 var hoursText = goog.i18n.DateTimeFormat.localizeNumbers(hours);
80 var minutesText = goog.i18n.DateTimeFormat.localizeNumbers(minutes);
81
82 // We need a space after the days if there are hours or minutes to come.
83 var daysSeparator = days * (hours + minutes) ? ' ' : '';
84 // We need a space after the hours if there are minutes to come.
85 var hoursSeparator = hours * minutes ? ' ' : '';
86
87 /**
88 * @desc The days part of the duration message: 1 day, 5 days.
89 */
90 var MSG_DURATION_DAYS = goog.getMsg(
91 '{COUNT, plural, ' +
92 '=0 {}' +
93 '=1 {{TEXT} day}' +
94 'other {{TEXT} days}}');
95 /**
96 * @desc The hours part of the duration message: 1 hour, 5 hours.
97 */
98 var MSG_DURATION_HOURS = goog.getMsg(
99 '{COUNT, plural, ' +
100 '=0 {}' +
101 '=1 {{TEXT} hour}' +
102 'other {{TEXT} hours}}');
103 /**
104 * @desc The minutes part of the duration message: 1 minute, 5 minutes.
105 */
106 var MSG_DURATION_MINUTES = goog.getMsg(
107 '{COUNT, plural, ' +
108 '=0 {}' +
109 '=1 {{TEXT} minute}' +
110 'other {{TEXT} minutes}}');
111
112 var daysPart = goog.date.duration.getDurationMessagePart_(
113 MSG_DURATION_DAYS, days, daysText);
114 var hoursPart = goog.date.duration.getDurationMessagePart_(
115 MSG_DURATION_HOURS, hours, hoursText);
116 var minutesPart = goog.date.duration.getDurationMessagePart_(
117 MSG_DURATION_MINUTES, minutes, minutesText);
118
119 /**
120 * @desc Duration time text concatenated from the individual time unit message
121 * parts. The separator will be a space (e.g. '1 day 2 hours 24 minutes') or
122 * nothing in case one/two of the duration parts is empty (
123 * e.g. '1 hour 30 minutes', '3 days 15 minutes', '2 hours').
124 */
125 var MSG_CONCATENATED_DURATION_TEXT = goog.getMsg(
126 '{$daysPart}{$daysSeparator}{$hoursPart}{$hoursSeparator}{$minutesPart}',
127 {
128 'daysPart': daysPart,
129 'daysSeparator': daysSeparator,
130 'hoursPart': hoursPart,
131 'hoursSeparator': hoursSeparator,
132 'minutesPart': minutesPart
133 });
134
135 return MSG_CONCATENATED_DURATION_TEXT;
136 };
137
138
139 /**
140 * Gets a duration message part for a time unit.
141 * @param {string} pattern The pattern to apply.
142 * @param {number} count The number of units.
143 * @param {string} text The string to use for amount of units in the message.
144 * @return {string} The formatted message part.
145 * @private
146 */
147 goog.date.duration.getDurationMessagePart_ = function(pattern, count, text) {
148 var formatter = new goog.i18n.MessageFormat(pattern);
149 return formatter.format({'COUNT': count, 'TEXT': text});
150 };