fix(date): compute %C and %y from the numeric year (#961)

Both were sliced from the year string, so years with other than four
digits gave the wrong century and two-digit year, and %y was a string
that the - and _ flags could not unpad. Compute them arithmetically and
pad them to two digits, matching Ruby's strftime.
This commit is contained in:
KBS
2026-09-30 00:47:25 +08:00
committed by GitHub
parent 28ae3fba5d
commit e68514f066
2 changed files with 13 additions and 3 deletions
+8
View File
@@ -18,6 +18,14 @@ describe('util/strftime', function () {
it('should format %y as 2-digit year', function () {
expect(t(now, '%y')).toBe('16')
})
it('should format %C and %y from the numeric year', function () {
const year999 = new TestDate('0999-06-15 00:00:00')
expect(t(year999, '%C')).toBe('09')
expect(t(year999, '%y')).toBe('99')
const year2005 = new TestDate('2005-06-15 00:00:00')
expect(t(year2005, '%y')).toBe('05')
expect(t(year2005, '%-y')).toBe('5')
})
describe('%j', function () {
it('should format %j as day of year', function () {
expect(t(then, '%j')).toBe('066')