UNPKG

5.25 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 */
16var _a;
17import * as Classes from "./classes";
18import { get12HourFrom24Hour, get24HourFrom12Hour } from "./dateUtils";
19/** describes a component of time. `H:MM:SS.MS` */
20export var TimeUnit;
21(function (TimeUnit) {
22 // NOTE: string enum so we can use it in Record<> type at the end of this file, which requires string keys
23 TimeUnit["HOUR_24"] = "hour24";
24 TimeUnit["HOUR_12"] = "hour12";
25 TimeUnit["MINUTE"] = "minute";
26 TimeUnit["SECOND"] = "second";
27 TimeUnit["MS"] = "ms";
28})(TimeUnit || (TimeUnit = {}));
29/** Gets a descriptive label representing the plural of the given time unit. */
30export function getTimeUnitPrintStr(unit) {
31 var _a;
32 var timeUnitToPrintstr = (_a = {},
33 _a[TimeUnit.HOUR_24] = "hours (24hr clock)",
34 _a[TimeUnit.HOUR_12] = "hours (12hr clock)",
35 _a[TimeUnit.MINUTE] = "minutes",
36 _a[TimeUnit.SECOND] = "seconds",
37 _a[TimeUnit.MS] = "milliseconds",
38 _a);
39 return timeUnitToPrintstr[unit];
40}
41/** Returns the given time unit component of the date. */
42export function getTimeUnit(unit, date) {
43 switch (unit) {
44 case TimeUnit.HOUR_24:
45 return date.getHours();
46 case TimeUnit.HOUR_12:
47 return get12HourFrom24Hour(date.getHours());
48 case TimeUnit.MINUTE:
49 return date.getMinutes();
50 case TimeUnit.SECOND:
51 return date.getSeconds();
52 case TimeUnit.MS:
53 return date.getMilliseconds();
54 default:
55 throw Error("Invalid TimeUnit");
56 }
57}
58/** Sets the given time unit to the given time in date object. Modifies given `date` object and returns it. */
59export function setTimeUnit(unit, time, date, isPm) {
60 switch (unit) {
61 case TimeUnit.HOUR_24:
62 date.setHours(time);
63 break;
64 case TimeUnit.HOUR_12:
65 date.setHours(get24HourFrom12Hour(time, isPm));
66 break;
67 case TimeUnit.MINUTE:
68 date.setMinutes(time);
69 break;
70 case TimeUnit.SECOND:
71 date.setSeconds(time);
72 break;
73 case TimeUnit.MS:
74 date.setMilliseconds(time);
75 break;
76 default:
77 throw Error("Invalid TimeUnit");
78 }
79 return date;
80}
81/** Returns true if `time` is a valid value */
82export function isTimeUnitValid(unit, time) {
83 return time != null && !isNaN(time) && getTimeUnitMin(unit) <= time && time <= getTimeUnitMax(unit);
84}
85/** If unit of time is greater than max, returns min. If less than min, returns max. Otherwise, returns time. */
86export function wrapTimeAtUnit(unit, time) {
87 var max = getTimeUnitMax(unit);
88 var min = getTimeUnitMin(unit);
89 if (time > max) {
90 return min;
91 }
92 else if (time < min) {
93 return max;
94 }
95 return time;
96}
97export function getTimeUnitClassName(unit) {
98 return TimeUnitMetadata[unit].className;
99}
100export function getTimeUnitMax(unit) {
101 return TimeUnitMetadata[unit].max;
102}
103export function getTimeUnitMin(unit) {
104 return TimeUnitMetadata[unit].min;
105}
106export function getDefaultMinTime() {
107 return new Date(0, 0, 0, DEFAULT_MIN_HOUR, DEFAULT_MIN_MINUTE, DEFAULT_MIN_SECOND, DEFAULT_MIN_MILLISECOND);
108}
109export function getDefaultMaxTime() {
110 return new Date(0, 0, 0, DEFAULT_MAX_HOUR, DEFAULT_MAX_MINUTE, DEFAULT_MAX_SECOND, DEFAULT_MAX_MILLISECOND);
111}
112var DEFAULT_MIN_HOUR = 0;
113var MERIDIEM_MIN_HOUR = 1;
114var DEFAULT_MIN_MINUTE = 0;
115var DEFAULT_MIN_SECOND = 0;
116var DEFAULT_MIN_MILLISECOND = 0;
117var DEFAULT_MAX_HOUR = 23;
118var MERIDIEM_MAX_HOUR = 12;
119var DEFAULT_MAX_MINUTE = 59;
120var DEFAULT_MAX_SECOND = 59;
121var DEFAULT_MAX_MILLISECOND = 999;
122/**
123 * A datastore (internal to this file) mapping TimeUnits to useful information about them.
124 * Use the `get*` methods above to access these fields.
125 */
126var TimeUnitMetadata = (_a = {},
127 _a[TimeUnit.HOUR_24] = {
128 className: Classes.TIMEPICKER_HOUR,
129 max: DEFAULT_MAX_HOUR,
130 min: DEFAULT_MIN_HOUR,
131 },
132 _a[TimeUnit.HOUR_12] = {
133 className: Classes.TIMEPICKER_HOUR,
134 max: MERIDIEM_MAX_HOUR,
135 min: MERIDIEM_MIN_HOUR,
136 },
137 _a[TimeUnit.MINUTE] = {
138 className: Classes.TIMEPICKER_MINUTE,
139 max: DEFAULT_MAX_MINUTE,
140 min: DEFAULT_MIN_MINUTE,
141 },
142 _a[TimeUnit.SECOND] = {
143 className: Classes.TIMEPICKER_SECOND,
144 max: DEFAULT_MAX_SECOND,
145 min: DEFAULT_MIN_SECOND,
146 },
147 _a[TimeUnit.MS] = {
148 className: Classes.TIMEPICKER_MILLISECOND,
149 max: DEFAULT_MAX_MILLISECOND,
150 min: DEFAULT_MIN_MILLISECOND,
151 },
152 _a);
153//# sourceMappingURL=timeUnit.js.map
\No newline at end of file