Skip to content

Commit

Permalink
chore: Refactor dateTime helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnns committed Feb 21, 2023
1 parent e378cac commit de0e90d
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 47 deletions.
28 changes: 20 additions & 8 deletions src/lib/dateTime.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
/**
* Returns the ISO string representation of that date
* that is most commonly used in Clockodo, which is:
* - normalized to UTC
* - without milliseconds
*/
export const dateToClockodoIsoString = (date: Date) => {
return date.toISOString().replace(/\.\d{3}Z$/, "Z");
import { IsoDate, IsoUtcDateTime } from "../models/dateTime.js";

export const isoDateFromDateTime = (dateTime: Date) => {
return [
dateTime.getFullYear(),
String(dateTime.getMonth() + 1).padStart(2, "0"),
String(dateTime.getDate()).padStart(2, "0"),
].join("-") as IsoDate;
};

export const isoDateFromTimestamp = (timestamp: number) => {
return isoDateFromDateTime(new Date(timestamp));
};

export const isoUtcDateTimeFromDateTime = (dateTime: Date) => {
return dateTime.toJSON().replace(/\.\d{3}Z$/, "Z") as IsoUtcDateTime;
};

export const isoUtcDateTimeFromTimestamp = (timestamp: number) => {
return isoUtcDateTimeFromDateTime(new Date(timestamp));
};
33 changes: 9 additions & 24 deletions src/lib/mocks.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@
import { faker } from "@faker-js/faker";
import { IsoDate, IsoUtcDateTime } from "../models/dateTime.js";

/**
* The number of milliseconds in a typical day.
* Should only be used for calculating mock data.
*/
export const ONE_DAY = 24 * 60 * 60 * 1000;

/**
* The number of milliseconds in a typical year.
* Should only be used for calculating mock data.
*/
export const ONE_YEAR = 356 * ONE_DAY;

const MAX_ITERATION_COUNT = 10000;

export const isoDateFromDateTime = (dateTime: Date) => {
return [
dateTime.getFullYear(),
String(dateTime.getMonth() + 1).padStart(2, "0"),
String(dateTime.getDate()).padStart(2, "0"),
].join("-") as IsoDate;
};

export const isoUtcDateTimeFromDateTime = (dateTime: Date) => {
return dateTime.toJSON().replace(/\.\d{3}Z$/, "Z") as IsoUtcDateTime;
};

export const isoMonthFromDateTime = (dateTime: Date) => {
return [
dateTime.getFullYear(),
String(dateTime.getMonth() + 1).padStart(2, "0"),
].join("-");
};

export const isoDateFromTimestamp = (timestamp: number) => {
return isoDateFromDateTime(new Date(timestamp));
};

export const startOfDay = (date: Date) => {
return new Date(date.getFullYear(), date.getMonth(), date.getDate());
};
Expand Down
2 changes: 1 addition & 1 deletion src/models/absence.mocks.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { faker } from "@faker-js/faker";
import { isoDateFromDateTime } from "../lib/dateTime.js";
import {
endOfYear,
generateRandomDates,
isoDateFromDateTime,
ONE_DAY,
startOfDay,
toPairs,
Expand Down
6 changes: 3 additions & 3 deletions src/models/entry.mocks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { faker } from "@faker-js/faker";
import { dateToClockodoIsoString } from "../lib/dateTime.js";
import { isoUtcDateTimeFromDateTime } from "../lib/dateTime.js";
import {
Billability,
Entry,
Expand All @@ -19,7 +19,7 @@ const DEFAULT_TO = new Date(2021, 0);
const createCommonEntryMock = (from: Date, to: Date) => {
const hasText = faker.datatype.number({ min: 0, max: 10 }) > 2;
const timeSince = faker.date.between(from, to);
const timeSinceAsIsoString = dateToClockodoIsoString(timeSince);
const timeSinceAsIsoString = isoUtcDateTimeFromDateTime(timeSince);

return {
id: 0,
Expand Down Expand Up @@ -56,7 +56,7 @@ export const createTimeEntryMocks = ({
const timeUntil =
timeEntryType === "clocking"
? null
: dateToClockodoIsoString(
: isoUtcDateTimeFromDateTime(
new Date(
new Date(commonEntry.timeSince).getTime() +
faker.datatype.number({ min: 1, max: 8 * 60 * 60 }) * 1000
Expand Down
4 changes: 2 additions & 2 deletions src/models/entry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dateToClockodoIsoString } from "../lib/dateTime.js";
import { isoUtcDateTimeFromDateTime } from "../lib/dateTime.js";
import { LumpsumService } from "./lumpsumService.js";
import { Project } from "./project.js";

Expand Down Expand Up @@ -142,7 +142,7 @@ export const isLumpsumEntry = (entry: Entry): entry is LumpsumEntry => {
* new Date().toISOString() without milliseconds precision.
*/
export const getEntryTimeUntilNow = (entry: Entry) => {
return entry.timeUntil ?? dateToClockodoIsoString(new Date());
return entry.timeUntil ?? isoUtcDateTimeFromDateTime(new Date());
};

/**
Expand Down
3 changes: 2 additions & 1 deletion src/models/nonbusinessDay.mocks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { faker } from "@faker-js/faker";
import { generateRandomDates, isoDateFromDateTime } from "../lib/mocks.js";
import { isoDateFromDateTime } from "../lib/dateTime.js";
import { generateRandomDates } from "../lib/mocks.js";
import { NonbusinessDay } from "./nonbusinessDay.js";

const DEFAULT_FROM = new Date(2020, 0);
Expand Down
3 changes: 1 addition & 2 deletions src/models/targethours.mocks.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { faker } from "@faker-js/faker";
import { isoDateFromDateTime, isoDateFromTimestamp } from "../lib/dateTime.js";
import {
ONE_YEAR,
ONE_DAY,
generateRandomDates,
generateRandomMonths,
isoDateFromDateTime,
isoDateFromTimestamp,
toPairs,
endOfMonth,
nextDay,
Expand Down
12 changes: 6 additions & 6 deletions src/models/workTimes.mocks.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { faker } from "@faker-js/faker";
import {
isoUtcDateTimeFromTimestamp,
isoDateFromDateTime,
} from "../lib/dateTime.js";
import {
generateRandomDates,
generateWithMaxDuplicates,
isoDateFromDateTime,
isoUtcDateTimeFromDateTime,
startOfDay,
} from "../lib/mocks.js";
import {
Expand Down Expand Up @@ -58,10 +60,8 @@ const generateIntervals = ({
if (index % 2 === 1) return acc;

const interval: WorkTimeDayInterval = {
timeSince: isoUtcDateTimeFromDateTime(new Date(pointInTime)),
timeUntil: isoUtcDateTimeFromDateTime(
new Date(pointsInTime[index + 1])
),
timeSince: isoUtcDateTimeFromTimestamp(pointInTime),
timeUntil: isoUtcDateTimeFromTimestamp(pointsInTime[index + 1]),
};

return [...acc, interval];
Expand Down

0 comments on commit de0e90d

Please sign in to comment.