Skip to content

Commit

Permalink
Implement date format for ms and seconds since epoc
Browse files Browse the repository at this point in the history
  • Loading branch information
kjellmorten committed Aug 4, 2020
1 parent c118083 commit 78e25b2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,14 @@ const template = 'http://example.com/{section|prepend(local_)}'

#### `date(format)`
Formats a date according to the given date format string.

Uses `date-and-time` under the hood, so refer to
[their documentation](https://github.com/knowledgecode/date-and-time#formatdateobj-formatstring-utc).

In addition, two custom formats are available: `ms-epoc` and `s-epoc`. They
format the date as number of microseconds or seconds since 1970-01-01. For
seconds, microsencods are rounded off to nearest second.

”Example:
```
const params = {updatedAfter: new Date('2020-03-19T14:08:44Z')}
Expand Down
20 changes: 20 additions & 0 deletions lib/filters/date-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,23 @@ test('should return iso string when format is not a string', (t) => {

t.is(ret, expected)
})

test('should format date as microseconds since epoc', (t) => {
const value = new Date('2020-03-18T19:59:03.113Z')
const format = 'ms-epoc'
const expected = '1584561543113'

const ret = date(value, format)

t.is(ret, expected)
})

test('should format date as seconds since epoc', (t) => {
const value = new Date('2020-03-18T19:59:03.113Z')
const format = 's-epoc'
const expected = '1584561543'

const ret = date(value, format)

t.is(ret, expected)
})
7 changes: 6 additions & 1 deletion lib/filters/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ function date (value, format) {
}
if (isDatish(value) && !Number.isNaN(value.getTime())) {
if (typeof format === 'string' && format !== '') {
return dateTime.format(value, format)
if (format.endsWith('s-epoc')) {
const ms = value.getTime()
return format === 'ms-epoc' ? String(ms) : String(Math.round(ms / 1000))
} else {
return dateTime.format(value, format)
}
} else {
return value.toISOString()
}
Expand Down
13 changes: 13 additions & 0 deletions tests/filterFunctions-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,16 @@ test('should generate uri with date filter functions', (t) => {

t.is(uri, expected)
})

test('should generate uri with date as seconds since epoc', (t) => {
const template = 'http://example.com/all{?since=updatedAfter|date(s-epoc)?,until=updatedBefore|date(YYYY-MM-DD)?}'
const params = {
updatedAfter: new Date('2020-03-20T18:43:11.875Z')
}
const expected = 'http://example.com/all?since=1584729792'

const compiled = compile(template)
const uri = generate(compiled, params)

t.is(uri, expected)
})

0 comments on commit 78e25b2

Please sign in to comment.