Skip to content

Commit

Permalink
fix: make date filter working with UTC by default and add support for…
Browse files Browse the repository at this point in the history
… more formats
  • Loading branch information
Qing Ye committed Jul 20, 2019
1 parent 53bff4d commit 103f2ad
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
13 changes: 13 additions & 0 deletions src/builtin/filters/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,26 @@ export default {
let date = v
if (v === 'now') {
date = new Date()
} else if (!isNaN(Number(v))) {
date = toUTCDate(new Date(Number(v) * 1000))
} else if (isString(v)) {
date = new Date(v)
}
return isValidDate(date) ? strftime(date, arg) : v
}
}

function toUTCDate(date: Date): Date {
return new Date(
date.getUTCFullYear(),
date.getUTCMonth(),
date.getUTCDate(),
date.getUTCHours(),
date.getUTCMinutes(),
date.getUTCSeconds()
)
}

function isValidDate (date: any): date is Date {
return date instanceof Date && !isNaN(date.getTime())
}
24 changes: 19 additions & 5 deletions src/util/strftime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,17 @@ const formatCodes = {
return monthNames[d.getMonth()]
},
c: function (d: Date) {
return d.toLocaleString()
return convert(d, '%a %b %e %T %Y')
},
C: function (d: Date) {
return _date.century(d)
},
d: function (d: Date) {
return padStart(d.getDate(), 2, '0')
},
D: function (d: Date) {
return convert(d, '%m/%d/%y')
},
e: function (d: Date) {
return padStart(d.getDate(), 2)
},
Expand Down Expand Up @@ -118,12 +121,21 @@ const formatCodes = {
q: function (d: Date) {
return _date.getSuffix(d)
},
r: function (d: Date) {
return convert(d, '%I:%M:%S %p')
},
R: function (d: Date) {
return convert(d, '%H:%M')
},
s: function (d: Date) {
return Math.round(d.valueOf() / 1000)
return Date.UTC(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds()) / 1000;
},
S: function (d: Date) {
return padStart(d.getSeconds(), 2, '0')
},
T: function (d: Date) {
return convert(d, '%H:%M:%S')
},
u: function (d: Date) {
return d.getDay() || 7
},
Expand All @@ -137,10 +149,10 @@ const formatCodes = {
return _date.getWeekOfYear(d, 1)
},
x: function (d: Date) {
return d.toLocaleDateString()
return convert(d, '%m/%d/%y')
},
X: function (d: Date) {
return d.toLocaleTimeString()
return convert(d, '%H:%M:%S')
},
y: function (d: Date) {
return d.getFullYear().toString().substring(2, 4)
Expand All @@ -159,7 +171,7 @@ const formatCodes = {
(formatCodes as any).h = formatCodes.b;
(formatCodes as any).N = formatCodes.L

export default function (d: Date, format: string) {
function convert (d: Date, format: string) {
let output = ''
let remaining = format

Expand All @@ -182,3 +194,5 @@ export default function (d: Date, format: string) {
output += func ? func(d) : '%' + ch
}
}

export default convert
18 changes: 13 additions & 5 deletions test/integration/builtin/filters/date.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { test, ctx } from '../../../stub/render'
import { test } from '../../../stub/render'

describe('filters/date', function () {
it('should support date: %a %b %d %Y', function () {
const str = ctx.date.toDateString()
return test('{{ date | date:"%a %b %d %Y"}}', str)
})
it('should create a new Date when given "now"', function () {
return test('{{ "now" | date: "%Y"}}', (new Date()).getFullYear().toString())
})
Expand All @@ -17,4 +13,16 @@ describe('filters/date', function () {
it('should render object as string if not valid', function () {
return test('{{ obj | date: "%Y"}}', '{"foo":"bar"}')
})
it('should convert to epoch timestamp', function () {
return test('{{ "2019-05-18T10:30:00" | date: "%s" }}', '1558175400')
})
it('should convert from epoch timestamp', function () {
return test('{{ 1558175400 | date: "%Y-%m-%dT%H:%M:%S" }}', '2019-05-18T10:30:00')
})
it('should convert from epoch timestamp in string format', function () {
return test('{{ "1558175400" | date: "%Y-%m-%dT%H:%M:%S" }}', '2019-05-18T10:30:00')
})
it('should support %c correctly', function () {
return test('{{ "2019-05-18T10:30:00" | date: "%c" }}', 'Sat May 18 10:30:00 2019')
})
})

0 comments on commit 103f2ad

Please sign in to comment.