-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #199 from github/minor-cleanups
Minor cleanups
- Loading branch information
Showing
6 changed files
with
218 additions
and
233 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,111 @@ | ||
export type Unit = 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year' | ||
|
||
export function timeAgo(date: Date): [number, Unit] { | ||
const ms = new Date().getTime() - date.getTime() | ||
const sec = Math.round(ms / 1000) | ||
const min = Math.round(sec / 60) | ||
const hr = Math.round(min / 60) | ||
const day = Math.round(hr / 24) | ||
const month = Math.round(day / 30) | ||
const year = Math.round(month / 12) | ||
if (ms < 0) { | ||
return [0, 'second'] | ||
} else if (sec < 10) { | ||
return [0, 'second'] | ||
} else if (sec < 45) { | ||
return [-sec, 'second'] | ||
} else if (sec < 90) { | ||
return [-min, 'minute'] | ||
} else if (min < 45) { | ||
return [-min, 'minute'] | ||
} else if (min < 90) { | ||
return [-hr, 'hour'] | ||
} else if (hr < 24) { | ||
return [-hr, 'hour'] | ||
} else if (hr < 36) { | ||
return [-day, 'day'] | ||
} else if (day < 30) { | ||
return [-day, 'day'] | ||
} else if (month < 18) { | ||
return [-month, 'month'] | ||
} else { | ||
return [-year, 'year'] | ||
} | ||
} | ||
|
||
export function microTimeAgo(date: Date): [number, Unit] { | ||
const ms = new Date().getTime() - date.getTime() | ||
const sec = Math.round(ms / 1000) | ||
const min = Math.round(sec / 60) | ||
const hr = Math.round(min / 60) | ||
const day = Math.round(hr / 24) | ||
const month = Math.round(day / 30) | ||
const year = Math.round(month / 12) | ||
if (min < 1) { | ||
return [1, 'minute'] | ||
} else if (min < 60) { | ||
return [min, 'minute'] | ||
} else if (hr < 24) { | ||
return [hr, 'hour'] | ||
} else if (day < 365) { | ||
return [day, 'day'] | ||
} else { | ||
return [year, 'year'] | ||
} | ||
} | ||
|
||
export function timeUntil(date: Date): [number, Unit] { | ||
const ms = date.getTime() - new Date().getTime() | ||
const sec = Math.round(ms / 1000) | ||
const min = Math.round(sec / 60) | ||
const hr = Math.round(min / 60) | ||
const day = Math.round(hr / 24) | ||
const month = Math.round(day / 30) | ||
const year = Math.round(month / 12) | ||
if (month >= 18) { | ||
return [year, 'year'] | ||
} else if (month >= 12) { | ||
return [year, 'year'] | ||
} else if (day >= 45) { | ||
return [month, 'month'] | ||
} else if (day >= 30) { | ||
return [month, 'month'] | ||
} else if (hr >= 36) { | ||
return [day, 'day'] | ||
} else if (hr >= 24) { | ||
return [day, 'day'] | ||
} else if (min >= 90) { | ||
return [hr, 'hour'] | ||
} else if (min >= 45) { | ||
return [hr, 'hour'] | ||
} else if (sec >= 90) { | ||
return [min, 'minute'] | ||
} else if (sec >= 45) { | ||
return [min, 'minute'] | ||
} else if (sec >= 10) { | ||
return [sec, 'second'] | ||
} else { | ||
return [0, 'second'] | ||
} | ||
} | ||
|
||
export function microTimeUntil(date: Date): [number, Unit] { | ||
const ms = date.getTime() - new Date().getTime() | ||
const sec = Math.round(ms / 1000) | ||
const min = Math.round(sec / 60) | ||
const hr = Math.round(min / 60) | ||
const day = Math.round(hr / 24) | ||
const month = Math.round(day / 30) | ||
const year = Math.round(month / 12) | ||
if (day >= 365) { | ||
return [year, 'year'] | ||
} else if (hr >= 24) { | ||
return [day, 'day'] | ||
} else if (min >= 60) { | ||
return [hr, 'hour'] | ||
} else if (min > 1) { | ||
return [min, 'minute'] | ||
} else { | ||
return [1, 'minute'] | ||
} | ||
} |
Oops, something went wrong.