Skip to content

Commit

Permalink
fix: timezones
Browse files Browse the repository at this point in the history
  • Loading branch information
badosz0 committed Jan 8, 2024
1 parent 1803ef4 commit 1bd8bb9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "holy-time",
"version": "3.4.3",
"version": "3.4.4",
"description": "Utility functions",
"repository": "Yet another (type-safe) date time library",
"main": "dist/index.js",
Expand Down
16 changes: 11 additions & 5 deletions src/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class HolyTime {

timeZone = TIMEZONE_MAP[timeZone] ?? timeZone;

return new Date(new Date(date.toLocaleString('en-US', { timeZone })).setMilliseconds(0));
return new Date(date.toLocaleString('en-US', { timeZone }));
}

private static getUnit(unit: HumanUnit): ValueOf<typeof HolyTime.Units> {
Expand Down Expand Up @@ -142,8 +142,11 @@ export class HolyTime {
}

public static startOf(unit: IntervalUnit, time: TimeResolvable = new Date(), timeZone?: TimeZone): HolyTime {
const date = HolyTime.adjustToTimeZone(HolyTime.resolveDate(time), timeZone);
const offset = HolyTime.between(date, time);
const resolved = HolyTime.resolveDate(time);
resolved.setMilliseconds(0);

const date = HolyTime.adjustToTimeZone(resolved, timeZone);
const offset = HolyTime.between(date, time) * (HolyTime.isBefore(date, time) ? 1 : -1);

switch (unit) {
case 'hour':
Expand All @@ -168,8 +171,11 @@ export class HolyTime {
}

public static endOf(unit: IntervalUnit, time: TimeResolvable = new Date(), timeZone?: TimeZone): HolyTime {
const date = HolyTime.adjustToTimeZone(HolyTime.resolveDate(time), timeZone);
const offset = HolyTime.between(date, time);
const resolved = HolyTime.resolveDate(time);
resolved.setMilliseconds(0);

const date = HolyTime.adjustToTimeZone(resolved, timeZone);
const offset = HolyTime.between(date, time) * (HolyTime.isBefore(date, time) ? 1 : -1);

switch (unit) {
case 'hour':
Expand Down

0 comments on commit 1bd8bb9

Please sign in to comment.