UNPKG

5.2 kBJavaScriptView Raw
1/*
2 * Copyright 2018 Palantir Technologies, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import * as Classes from "./classes";
17import { get12HourFrom24Hour, get24HourFrom12Hour } from "./dateUtils";
18/** describes a component of time. `H:MM:SS.MS` */
19export var TimeUnit;
20(function (TimeUnit) {
21 // NOTE: string enum so we can use it in Record<> type at the end of this file, which requires string keys
22 TimeUnit["HOUR_24"] = "hour24";
23 TimeUnit["HOUR_12"] = "hour12";
24 TimeUnit["MINUTE"] = "minute";
25 TimeUnit["SECOND"] = "second";
26 TimeUnit["MS"] = "ms";
27})(TimeUnit || (TimeUnit = {}));
28/** Gets a descriptive label representing the plural of the given time unit. */
29export function getTimeUnitPrintStr(unit) {
30 const timeUnitToPrintstr = {
31 [TimeUnit.HOUR_24]: "hours (24hr clock)",
32 [TimeUnit.HOUR_12]: "hours (12hr clock)",
33 [TimeUnit.MINUTE]: "minutes",
34 [TimeUnit.SECOND]: "seconds",
35 [TimeUnit.MS]: "milliseconds",
36 };
37 return timeUnitToPrintstr[unit];
38}
39/** Returns the given time unit component of the date. */
40export function getTimeUnit(unit, date) {
41 switch (unit) {
42 case TimeUnit.HOUR_24:
43 return date.getHours();
44 case TimeUnit.HOUR_12:
45 return get12HourFrom24Hour(date.getHours());
46 case TimeUnit.MINUTE:
47 return date.getMinutes();
48 case TimeUnit.SECOND:
49 return date.getSeconds();
50 case TimeUnit.MS:
51 return date.getMilliseconds();
52 default:
53 throw Error("Invalid TimeUnit");
54 }
55}
56/** Sets the given time unit to the given time in date object. Modifies given `date` object and returns it. */
57export function setTimeUnit(unit, time, date, isPm) {
58 switch (unit) {
59 case TimeUnit.HOUR_24:
60 date.setHours(time);
61 break;
62 case TimeUnit.HOUR_12:
63 date.setHours(get24HourFrom12Hour(time, isPm));
64 break;
65 case TimeUnit.MINUTE:
66 date.setMinutes(time);
67 break;
68 case TimeUnit.SECOND:
69 date.setSeconds(time);
70 break;
71 case TimeUnit.MS:
72 date.setMilliseconds(time);
73 break;
74 default:
75 throw Error("Invalid TimeUnit");
76 }
77 return date;
78}
79/** Returns true if `time` is a valid value */
80export function isTimeUnitValid(unit, time) {
81 return time != null && !isNaN(time) && getTimeUnitMin(unit) <= time && time <= getTimeUnitMax(unit);
82}
83/** If unit of time is greater than max, returns min. If less than min, returns max. Otherwise, returns time. */
84export function wrapTimeAtUnit(unit, time) {
85 const max = getTimeUnitMax(unit);
86 const min = getTimeUnitMin(unit);
87 if (time > max) {
88 return min;
89 }
90 else if (time < min) {
91 return max;
92 }
93 return time;
94}
95export function getTimeUnitClassName(unit) {
96 return TimeUnitMetadata[unit].className;
97}
98export function getTimeUnitMax(unit) {
99 return TimeUnitMetadata[unit].max;
100}
101export function getTimeUnitMin(unit) {
102 return TimeUnitMetadata[unit].min;
103}
104export function getDefaultMinTime() {
105 return new Date(0, 0, 0, DEFAULT_MIN_HOUR, DEFAULT_MIN_MINUTE, DEFAULT_MIN_SECOND, DEFAULT_MIN_MILLISECOND);
106}
107export function getDefaultMaxTime() {
108 return new Date(0, 0, 0, DEFAULT_MAX_HOUR, DEFAULT_MAX_MINUTE, DEFAULT_MAX_SECOND, DEFAULT_MAX_MILLISECOND);
109}
110const DEFAULT_MIN_HOUR = 0;
111const MERIDIEM_MIN_HOUR = 1;
112const DEFAULT_MIN_MINUTE = 0;
113const DEFAULT_MIN_SECOND = 0;
114const DEFAULT_MIN_MILLISECOND = 0;
115const DEFAULT_MAX_HOUR = 23;
116const MERIDIEM_MAX_HOUR = 12;
117const DEFAULT_MAX_MINUTE = 59;
118const DEFAULT_MAX_SECOND = 59;
119const DEFAULT_MAX_MILLISECOND = 999;
120/**
121 * A datastore (internal to this file) mapping TimeUnits to useful information about them.
122 * Use the `get*` methods above to access these fields.
123 */
124const TimeUnitMetadata = {
125 [TimeUnit.HOUR_24]: {
126 className: Classes.TIMEPICKER_HOUR,
127 max: DEFAULT_MAX_HOUR,
128 min: DEFAULT_MIN_HOUR,
129 },
130 [TimeUnit.HOUR_12]: {
131 className: Classes.TIMEPICKER_HOUR,
132 max: MERIDIEM_MAX_HOUR,
133 min: MERIDIEM_MIN_HOUR,
134 },
135 [TimeUnit.MINUTE]: {
136 className: Classes.TIMEPICKER_MINUTE,
137 max: DEFAULT_MAX_MINUTE,
138 min: DEFAULT_MIN_MINUTE,
139 },
140 [TimeUnit.SECOND]: {
141 className: Classes.TIMEPICKER_SECOND,
142 max: DEFAULT_MAX_SECOND,
143 min: DEFAULT_MIN_SECOND,
144 },
145 [TimeUnit.MS]: {
146 className: Classes.TIMEPICKER_MILLISECOND,
147 max: DEFAULT_MAX_MILLISECOND,
148 min: DEFAULT_MIN_MILLISECOND,
149 },
150};
151//# sourceMappingURL=timeUnit.js.map
\No newline at end of file