Skip to content

Commit

Permalink
Fix strftime implementations for format strings "%F" and "%j" (#475)
Browse files Browse the repository at this point in the history
* fix strftime, %F contains %Y (four digit)

* fix strftime, %j with DST compensation
  • Loading branch information
markusmarchewa authored Jan 14, 2025
1 parent 10c5d32 commit e5f1c1f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/php/datetime/strftime.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ module.exports = function strftime(fmt, timestamp) {
// note 1: Uses global: locutus to store locale info
// example 1: strftime("%A", 1062462400); // Return value will depend on date and locale
// returns 1: 'Tuesday'
// bugfixed by: Markus Marchewa
// example 2: strftime('%F', 1577836800);
// returns 2: '2020-01-01'
// example 3: (() => {let e = process.env, tz = e.TZ; e.TZ = 'Europe/Vienna'; let r = strftime('%j', 1680307200); e.TZ = tz; return r;})();
// returns 3: '091'

const setlocale = require('../strings/setlocale')

Expand Down Expand Up @@ -71,10 +76,13 @@ module.exports = function strftime(fmt, timestamp) {
return _xPad(I === 0 ? 12 : I, 0)
},
j: function (d) {
let ms = d - new Date('' + d.getFullYear() + '/1/1 GMT')
// Line differs from Yahoo implementation which would be
// equivalent to replacing it here with:
ms += d.getTimezoneOffset() * 60000
// calculate the difference between the given date and the start of the year (in localtime), DST shifts may lead
// to deltas less than multiples of 24 hours (the day when DST starts has just 23 hours), compensate by adding
// the difference between timezone offsets (subtract since values are negative for positive offsets), e.g.:
// 2020-05-01 00:00:00 CEST, timezone +0200, offset -120
// 2020-01-01 00:00:00 CET , timezone +0100, offset -60
const b = new Date(d.getFullYear(), 0)
const ms = d - b - (d.getTimezoneOffset() - b.getTimezoneOffset()) * 60000
const doy = parseInt(ms / 60000 / 60 / 24, 10) + 1
return _xPad(doy, 0, 100)
},
Expand Down Expand Up @@ -160,7 +168,7 @@ module.exports = function strftime(fmt, timestamp) {
const _aggregates = {
c: 'locale',
D: '%m/%d/%y',
F: '%y-%m-%d',
F: '%Y-%m-%d',
h: '%b',
n: '\n',
r: 'locale',
Expand Down
12 changes: 12 additions & 0 deletions test/generated/php/datetime/test-strftime.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,16 @@ describe('src/php/datetime/strftime.js (tested in test/generated/php/datetime/te
expect(result).to.deep.equal(expected)
done()
})
it('should pass example 2', function (done) {
var expected = '2020-01-01'
var result = strftime('%F', 1577836800);
expect(result).to.deep.equal(expected)
done()
})
it('should pass example 3', function (done) {
var expected = '091'
var result = (() => {let e = process.env, tz = e.TZ; e.TZ = 'Europe/Vienna'; let r = strftime('%j', 1680307200); e.TZ = tz; return r;})();
expect(result).to.deep.equal(expected)
done()
})
})

0 comments on commit e5f1c1f

Please sign in to comment.