Back to packages list

Vals using date-fns-tz

Description from the NPM package:
Time zone support for date-fns v3 with the Intl API
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { weekRange as weekRange2 } from "https://esm.town/v/andreterron/weekRange";
import { workedOutByDay } from "https://esm.town/v/andreterron/workedOutByDay";
import { addDays, format } from "npm:date-fns@2.30.0";
import { formatInTimeZone, utcToZonedTime } from "npm:date-fns-tz@2.0.0";
export const workoutDays = async (
byDay: Record<string, any>,
timezone: string = "America/Los_Angeles",
) => {
const todayKey = formatInTimeZone(
new Date(),
timezone,
"yyyy-MM-dd",
);
const weekRange = await weekRange2();
return weekRange.map((day) => {
const key = format(day, "yyyy-MM-dd");
const previousKey = format(addDays(day, -1), "yyyy-MM-dd");
const workedOut = !!byDay[key];
const past = key < todayKey;
return {
day: key,
workedOut,
// Timezones are hard, so we compare strings
past,
today: key === todayKey,
failed: past && !workedOut && !byDay[previousKey],
};
});
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import { eachDayOfInterval, endOfWeek, startOfDay, startOfWeek } from "npm:date-fns@2.30.0";
import { formatInTimeZone, utcToZonedTime } from "npm:date-fns-tz@2.0.0";
export const weekRange = (timezone: string = "America/Los_Angeles") => {
const now = new Date();
const nowTZ = new Date(
formatInTimeZone(now, timezone, "yyyy-MM-dd"),
);
// return now;
const result = eachDayOfInterval({
start: startOfWeek(nowTZ, { weekStartsOn: 1 }),
end: endOfWeek(nowTZ, { weekStartsOn: 1 }),
});
return result;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import { myPelotonWorkouts } from "https://esm.town/v/andreterron/myPelotonWorkouts";
import { format } from "npm:date-fns@2.30.0";
import { utcToZonedTime } from "npm:date-fns-tz@2.0.0";
export const workedOutByDay = async (timezone: string = "America/Los_Angeles") => {
const workouts = await myPelotonWorkouts();
const byDay: {
[k: string]: any;
} = {};
for (let w of workouts) {
byDay[
format(
utcToZonedTime(new Date(w.start_time * 1000), timezone),
"yyyy-MM-dd",
)
] = w;
}
return byDay;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { weekRange as weekRange2 } from "https://esm.town/v/andreterron/weekRange";
import { WorkoutIcon } from "https://esm.town/v/andreterron/workoutIcons";
import { addDays, format } from "npm:date-fns@2.30.0";
import { formatInTimeZone, utcToZonedTime } from "npm:date-fns-tz";
export function weekWorkoutIcons(
byDay: Record<string, any>,
timezone: string = "America/Los_Angeles",
): WorkoutIcon[] {
const todayKey = formatInTimeZone(
new Date(),
timezone,
"yyyy-MM-dd",
);
const weekRange = weekRange2(timezone);
return weekRange.map((day): WorkoutIcon => {
const key = format(day, "yyyy-MM-dd");
const previousKey = format(addDays(day, -1), "yyyy-MM-dd");
const workedOut = !!byDay[key];
const past = key < todayKey;
if (workedOut) return "done";
// Never skip twice
if (past && !workedOut && !byDay[previousKey]) return "failed";
if (past) return "skipped";
if (key === todayKey) return "today";
return "future";
});
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { email } from "https://esm.town/v/std/email?v=9";
import { fetch } from "https://esm.town/v/std/fetch";
export const shitIsMySiteDown = async () => {
const URL = "https://withseismic.com";
const TIMEZONE: string = "UTC";
const { format } = await import("npm:date-fns-tz");
const timestamp = format(new Date(), "dd-MM-yyyy'T'HH:mm:ssXXX", { timeZone: TIMEZONE });
let status = "up";
try {
const response = await fetch(URL);
if (response.status !== 200) {
throw new Error(`status code: ${response.status}`);
}
} catch (error) {
status = "down";
const subject = `Website down alert: ${URL}`;
const body = `As of ${timestamp} (${TIMEZONE}), ${URL} is down. Reason: ${error.message}.`;
// Send email using the updated email function.
await email({
subject: subject,
text: body,
});
} finally {
console.log(`Checked website status at ${timestamp} in TIMEZONE ${TIMEZONE}: ${status}.`);
return { status }
}
};
1
2
3
4
5
6
7
export async function toDatesWithTz(
dateStrings: string[],
timeZone: string,
): Promise<Date[]> {
const { toDate } = await import("npm:date-fns-tz");
return dateStrings.map((s) => toDate(s, { timeZone }));
}
1
Next