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

Function getTzOffsetTransitions

pkgs/core/src/_lib/test/tzOffsetTransitions.ts:91–143  ·  view source on GitHub ↗
(year: number)

Source from the content-addressed store, hash-verified

89
90 */
91export function getTzOffsetTransitions(year: number) {
92 // start at the end of the previous day
93 let date = firstTickInLocalDay(new Date(year, 0, 1));
94 if (!isValidDate(date)) {
95 throw new Error("Invalid Date");
96 }
97 let baseTzOffset = previousTickTimezoneOffset(date);
98 const transitions = [];
99 do {
100 let tzOffset = date.getTimezoneOffset();
101 if (baseTzOffset !== tzOffset) {
102 if (tzOffset !== previousTickTimezoneOffset(date)) {
103 // Transition is the first tick of a local day.
104 transitions.push({
105 date: date,
106 type: tzOffset < baseTzOffset ? "forward" : "back",
107 before: -baseTzOffset,
108 after: -tzOffset,
109 });
110 baseTzOffset = tzOffset;
111 } else {
112 // transition was not at the start of the day, so it must have happened
113 // yesterday. Back up one day and find the minute where it happened.
114 let transitionDate = new Date(date.getTime());
115 transitionDate.setDate(transitionDate.getDate() - 1);
116
117 // Iterate through each 5 mins of the day until we find a transition.
118 // TODO: this could be optimized to search hours then minutes or by or
119 // by using a binary search.
120 const dayNumber = transitionDate.getDate();
121 while (
122 isValidDate(transitionDate) &&
123 transitionDate.getDate() === dayNumber
124 ) {
125 tzOffset = transitionDate.getTimezoneOffset();
126 if (baseTzOffset !== tzOffset) {
127 transitions.push({
128 date: transitionDate,
129 type: tzOffset < baseTzOffset ? "forward" : "back",
130 before: -baseTzOffset,
131 after: -tzOffset,
132 });
133 baseTzOffset = tzOffset;
134 break; // assuming only 1 transition per day
135 }
136 transitionDate = fiveMinutesLater(transitionDate);
137 }
138 }
139 }
140 date = oneDayLater(date);
141 } while (date.getFullYear() === year);
142 return transitions;
143}

Callers 1

getDstTransitionsFunction · 0.85

Calls 6

firstTickInLocalDayFunction · 0.85
isValidDateFunction · 0.85
fiveMinutesLaterFunction · 0.85
oneDayLaterFunction · 0.85
getTimezoneOffsetMethod · 0.45

Tested by

no test coverage detected