Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/source/filters/date.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ The `date` filter is used to convert a timestamp into the specified format.
* minutes: `-360` means `'+06:00'` and `360` means `'-06:00'`
* timeZone ID: `Asia/Colombo` or `America/New_York`
* See [here](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) for TZ database values
* `%s` (seconds since the Unix epoch) identifies an instant rather than a wall-clock time, so it's not affected by the display timezone.

### Examples
```liquid
Expand Down
9 changes: 9 additions & 0 deletions src/util/liquid-date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ export class LiquidDate {
getTime () {
return this.displayDate.getTime()
}
/**
* The underlying UTC timestamp in milliseconds, unaffected by the display
* timezone. Use this (not `getTime()`) for timezone-invariant values like
* `%s`: `getTime()` reads `displayDate`, which is deliberately shifted by
* the display timezone offset so wall-clock getters can delegate to Date.
*/
dateValue () {
return this.date.getTime()
}
getMilliseconds () {
return this.displayDate.getMilliseconds()
}
Expand Down
2 changes: 1 addition & 1 deletion src/util/strftime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const formatCodes: Record<string, FormatCodeHandler> = {
p: (d: LiquidDate) => (d.getHours() < 12 ? 'AM' : 'PM'),
P: (d: LiquidDate) => (d.getHours() < 12 ? 'am' : 'pm'),
q: (d: LiquidDate) => ordinal(d),
s: (d: LiquidDate) => Math.round(d.getTime() / 1000),
s: (d: LiquidDate) => Math.floor(d.dateValue() / 1000),
S: (d: LiquidDate) => d.getSeconds(),
u: (d: LiquidDate) => d.getDay() || 7,
U: (d: LiquidDate) => getWeekOfYear(d, 0),
Expand Down
12 changes: 12 additions & 0 deletions test/integration/filters/date.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ describe('filters/date', function () {
it('should support timezone name argument when DST is active', function () {
return test('{{ "2021-06-01T23:00:00Z" | date: "%Y-%m-%dT%H:%M:%S", "America/New_York" }}', '2021-06-01T19:00:00')
})
it('should not shift %s by the timezone name argument', function () {
return test('{{ "2026-06-30T21:00:00Z" | date: "%s", "America/Toronto" }}', '1782853200')
})
it('should not shift %s by the timezone offset argument', function () {
return test('{{ "2026-06-30T21:00:00Z" | date: "%s", 360 }}', '1782853200')
})
it('should not shift %s by the timezoneOffset option', function () {
return test('{{ "2026-06-30T21:00:00Z" | date: "%s" }}', '1782853200', undefined, opts)
})
it('should truncate %s toward the epoch like Ruby strftime', function () {
return test('{{ "2026-06-30T17:00:00.500Z" | date: "%s" }}', '1782838800')
})
it('should offset date literal with timezone 00:00 specified', function () {
return test('{{ "1990-12-31T23:00:00+00:00" | date: "%Y-%m-%dT%H:%M:%S"}}', '1990-12-31T17:00:00', undefined, opts)
})
Expand Down
Loading