1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | import * as Classes from "./classes";
|
17 | import { get12HourFrom24Hour, get24HourFrom12Hour } from "./dateUtils";
|
18 |
|
19 | export var TimeUnit;
|
20 | (function (TimeUnit) {
|
21 |
|
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 |
|
29 | export 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 |
|
40 | export 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 |
|
57 | export 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 |
|
80 | export function isTimeUnitValid(unit, time) {
|
81 | return time != null && !isNaN(time) && getTimeUnitMin(unit) <= time && time <= getTimeUnitMax(unit);
|
82 | }
|
83 |
|
84 | export 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 | }
|
95 | export function getTimeUnitClassName(unit) {
|
96 | return TimeUnitMetadata[unit].className;
|
97 | }
|
98 | export function getTimeUnitMax(unit) {
|
99 | return TimeUnitMetadata[unit].max;
|
100 | }
|
101 | export function getTimeUnitMin(unit) {
|
102 | return TimeUnitMetadata[unit].min;
|
103 | }
|
104 | export function getDefaultMinTime() {
|
105 | return new Date(0, 0, 0, DEFAULT_MIN_HOUR, DEFAULT_MIN_MINUTE, DEFAULT_MIN_SECOND, DEFAULT_MIN_MILLISECOND);
|
106 | }
|
107 | export function getDefaultMaxTime() {
|
108 | return new Date(0, 0, 0, DEFAULT_MAX_HOUR, DEFAULT_MAX_MINUTE, DEFAULT_MAX_SECOND, DEFAULT_MAX_MILLISECOND);
|
109 | }
|
110 | const DEFAULT_MIN_HOUR = 0;
|
111 | const MERIDIEM_MIN_HOUR = 1;
|
112 | const DEFAULT_MIN_MINUTE = 0;
|
113 | const DEFAULT_MIN_SECOND = 0;
|
114 | const DEFAULT_MIN_MILLISECOND = 0;
|
115 | const DEFAULT_MAX_HOUR = 23;
|
116 | const MERIDIEM_MAX_HOUR = 12;
|
117 | const DEFAULT_MAX_MINUTE = 59;
|
118 | const DEFAULT_MAX_SECOND = 59;
|
119 | const DEFAULT_MAX_MILLISECOND = 999;
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | const 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 |
|
\ | No newline at end of file |