MCPcopy
hub / github.com/date-fns/date-fns / tzScan

Function tzScan

pkgs/tz/src/tzScan/index.ts:33–99  ·  view source on GitHub ↗
(
  timeZone: string,
  interval: TZChangeInterval,
)

Source from the content-addressed store, hash-verified

31 * @returns Array of time zone changes
32 */
33export function tzScan(
34 timeZone: string,
35 interval: TZChangeInterval,
36): TZChange[] {
37 const changes: TZChange[] = [];
38
39 const monthDate = new Date(interval.start);
40 monthDate.setUTCSeconds(0, 0);
41
42 const endDate = new Date(interval.end);
43 endDate.setUTCSeconds(0, 0);
44
45 const endMonthTime = +endDate;
46 let lastOffset = tzOffset(timeZone, monthDate);
47 while (+monthDate < endMonthTime) {
48 // Month forward
49 monthDate.setUTCMonth(monthDate.getUTCMonth() + 1);
50
51 // Find the month where the offset changes
52 const offset = tzOffset(timeZone, monthDate);
53 if (offset != lastOffset) {
54 // Rewind a month back to find the day where the offset changes
55 const dayDate = new Date(monthDate);
56 dayDate.setUTCMonth(dayDate.getUTCMonth() - 1);
57
58 const endDayTime = +monthDate;
59 lastOffset = tzOffset(timeZone, dayDate);
60 while (+dayDate < endDayTime) {
61 // Day forward
62 dayDate.setUTCDate(dayDate.getUTCDate() + 1);
63
64 // Find the day where the offset changes
65 const offset = tzOffset(timeZone, dayDate);
66 if (offset != lastOffset) {
67 // Rewind a day back to find the time where the offset changes
68 const hourDate = new Date(dayDate);
69 hourDate.setUTCDate(hourDate.getUTCDate() - 1);
70
71 const endHourTime = +dayDate;
72 lastOffset = tzOffset(timeZone, hourDate);
73 while (+hourDate < endHourTime) {
74 // Hour forward
75 hourDate.setUTCHours(hourDate.getUTCHours() + 1);
76
77 // Find the hour where the offset changes
78 const hourOffset = tzOffset(timeZone, hourDate);
79 if (hourOffset !== lastOffset) {
80 changes.push({
81 date: new Date(hourDate),
82 change: hourOffset - lastOffset,
83 offset: hourOffset,
84 });
85 }
86
87 lastOffset = hourOffset;
88 }
89 }
90

Callers 2

benchmark.tsFile · 0.90
tests.tsFile · 0.90

Calls 1

tzOffsetFunction · 0.90

Tested by

no test coverage detected