Skip to content

Commit

Permalink
Merge pull request #199 from github/minor-cleanups
Browse files Browse the repository at this point in the history
Minor cleanups
  • Loading branch information
keithamus authored Nov 4, 2022
2 parents 7eb4d8c + 68ae19a commit 4bc8693
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 233 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ So, a relative date phrase is used for up to a month and then the actual date is
| `tense` | `tense` | `'auto'\|'past'\|'future'` | `'auto'` |
| `threshold` | `threshold` | `string` | `'P30D'` |
| `prefix` | `prefix` | `string` | `'on'` |
| `second` | `second` | `'numeric'\|'2-digit'\|undefined` | `undefined` |
| `minute` | `minute` | `'numeric'\|'2-digit'\|undefined` | `undefined` |
| `hour` | `hour` | `'numeric'\|'2-digit'\|undefined` | `undefined` |
| `weekday` | `weekday` | `'short'\|'long'\|'narrow'\|undefined` | `undefined` |
| `day` | `day` | `'numeric'\|'2-digit'\|undefined` | `'numeric'` |
| `month` | `month` | `'numeric'\|'2-digit'\|'short'\|'long'\|'narrow'\|undefined` | `'short'` |
| `year` | `year` | `'numeric'\|'2-digit'\|undefined` | `'numeric'`<sup>*</sup>|
Expand Down Expand Up @@ -148,7 +150,7 @@ When formatting an absolute date (see above `threshold` for more details) it can
</relative-time>
```

##### minute, hour, day, month, year, timeZoneName
##### second, minute, hour, weekday, day, month, year, timeZoneName

For dates outside of the specified `threshold`, the formatting of the date can be configured using these attributes. The values for these attributes are passed to [Intl.DateTimeFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat):

Expand Down
98 changes: 6 additions & 92 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

111 changes: 111 additions & 0 deletions src/duration-format.ts
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']
}
}
Loading

0 comments on commit 4bc8693

Please sign in to comment.