-
Notifications
You must be signed in to change notification settings - Fork 162
feat: support convert milliseconds and fix timezone #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1057de1
Fixes #35: Added the unit function to convert milliseconds to multipl…
abu-sufyan1 e40e367
Fixes #36: timezone wrong on Pacific Daylight Time
abu-sufyan1 dbd7ed2
LRU implementation
abu-sufyan1 3e04e5b
Update src/date.ts
abu-sufyan1 9dd1c22
made ylru a production dependency
abu-sufyan1 cd17642
Update src/date.ts
abu-sufyan1 9df3ab4
fix
abu-sufyan1 70010bd
Update date.ts
fengmk2 1e0bab0
Update src/date.ts
fengmk2 6bc1073
Update date.ts
fengmk2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,19 +1,17 @@ | ||
| // only set once. | ||
| let TIMEZONE = ''; | ||
| export function resetTimezone() { | ||
| TIMEZONE = ''; | ||
| let _hourOffset = Math.floor(-(new Date().getTimezoneOffset()) / 60); | ||
| if (_hourOffset >= 0) { | ||
| TIMEZONE += '+'; | ||
| } else { | ||
| TIMEZONE += '-'; | ||
| } | ||
| _hourOffset = Math.abs(_hourOffset); | ||
| const _hourOffsetStr = _hourOffset < 10 ? `0${_hourOffset}` : `${_hourOffset}`; | ||
| TIMEZONE += `${_hourOffsetStr}00`; | ||
| import { LRU } from 'ylru'; | ||
| const lru = new LRU(1000); // Cache up to 1000 entries | ||
|
|
||
| export function resetTimezone(date: Date) { | ||
| let TIMEZONE: string = ''; | ||
| const offsetInMinutes = date.getTimezoneOffset(); | ||
| const _hourOffset: number = Math.floor(-offsetInMinutes / 60); | ||
| const _minuteOffset: number = Math.abs(offsetInMinutes % 60); | ||
|
|
||
| TIMEZONE += _hourOffset >= 0 ? '+' : '-'; | ||
| TIMEZONE += `${String(Math.abs(_hourOffset)).padStart(2, '0')}${String(_minuteOffset).padStart(2, '0')}`; | ||
|
|
||
| return TIMEZONE; | ||
| } | ||
| resetTimezone(); | ||
|
|
||
| const MONTHS: Record<string, string> = { | ||
| '01': 'Jan', | ||
|
|
@@ -61,9 +59,19 @@ | |
| // 16/Apr/2013:16:40:09 +0800 | ||
| d = d || new Date(); | ||
| const [ year, month, date, hours, minutes, seconds ] = getDateStringParts(d); | ||
| const TIMEZONE = getTimezone(d); | ||
| return `${date}/${MONTHS[month]}/${year}:${hours}:${minutes}:${seconds} ${TIMEZONE}`; | ||
| } | ||
|
|
||
| export function getTimezone(d: Date) { | ||
| const key = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate(); | ||
| const timeZone = lru.get(key); | ||
| if (timeZone === undefined) { | ||
| lru.set(key, resetTimezone(d), { maxAge: 86400000 }); // Cache for 24 hours | ||
| return lru.get(key); | ||
| } | ||
| return timeZone; | ||
| } | ||
| /** | ||
| * Normal log format date. format: `moment().format('YYYY-MM-DD HH:mm:ss.SSS')` | ||
| */ | ||
|
|
@@ -186,3 +194,39 @@ | |
| export function parseTimestamp(t: number | string): Date { | ||
| return timestamp(t) as Date; | ||
| } | ||
|
|
||
| /** | ||
| * Convert Date object to Unix timestamp in seconds. | ||
| */ | ||
| export function dateToUnixTimestamp(date: Date): number { | ||
| return Math.round(date.getTime() / 1000); | ||
| } | ||
|
|
||
| export enum DateFormat { | ||
| DateTimeWithTimeZone = 'DateTimeWithTimeZone', | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change enum value to string is better than number. |
||
| DateTimeWithMilliSeconds = 'DateTimeWithMilliSeconds', | ||
| DateTimeWithSeconds = 'DateTimeWithSeconds', | ||
| UnixTimestamp = 'UnixTimestamp', | ||
| } | ||
|
|
||
| /** | ||
| * Provide milliseconds, return a formatted string. | ||
| */ | ||
| export function getDateFromMilliseconds(milliseconds: number, format?: DateFormat): string { | ||
| if (!Number.isFinite(milliseconds)) { | ||
| throw new Error('Invalid milliseconds value'); | ||
| } | ||
|
|
||
| switch (format) { | ||
| case DateFormat.DateTimeWithTimeZone: | ||
| return accessLogDate(new Date(milliseconds)); | ||
| case DateFormat.DateTimeWithMilliSeconds: | ||
| return logDate(new Date(milliseconds)); | ||
| case DateFormat.DateTimeWithSeconds: | ||
| return YYYYMMDDHHmmss(new Date(milliseconds)); | ||
| case DateFormat.UnixTimestamp: | ||
| return dateToUnixTimestamp(new Date(milliseconds)).toString(); | ||
| default: | ||
| return YYYYMMDD(new Date(milliseconds)); | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.