-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use last modified date as "version" with time as the semver tag
- Loading branch information
1 parent
633586f
commit 7d5c9de
Showing
3 changed files
with
45 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Parses a date string in UTC format and returns a Date object. | ||
* | ||
* @param dateString - The date string to parse in the format "YYYY-MM-DDTHH:mm:ss.sssZ". | ||
* @returns - The parsed Date object in UTC. | ||
*/ | ||
export function parseUTCDate(dateString: string): Date { | ||
const dateMatch = dateString | ||
.match(/^(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)(?:\.(\d{1,3}))?\d*Z$/) | ||
|
||
if (!dateMatch) { | ||
throw new Error("Invalid date format"); | ||
} | ||
|
||
const dateComponents = dateMatch | ||
.slice(1) | ||
.map((value) => value ? parseInt(value) : 0); | ||
|
||
return new Date(Date.UTC(dateComponents[0], dateComponents[1] - 1, dateComponents[2], dateComponents[3], dateComponents[4], dateComponents[5], dateComponents[6])); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Converts a Date object to a semver-like string. | ||
* If no date is provided, the current date and time is used. | ||
* | ||
* @param inputDate - The date to be converted. Defaults to the current date and time. | ||
* @returns The date and time in the format "YYYY.MM.DD-HHmmssSSSZ". | ||
*/ | ||
export function semverDate(inputDate: Date = new Date()): string { | ||
const isoString = inputDate.toISOString(); | ||
const parts = isoString.split("T").map((value, index) => { | ||
if (index == 0) { | ||
return value.replace(/-0*/g, "."); | ||
} else { | ||
return value.replace(/[:\.]/g, ""); | ||
} | ||
}); | ||
|
||
return parts.join("-"); | ||
} |