From 93326a4d733b3fa20356b60ff8d4ba4cdd1c31e1 Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Fri, 29 May 2015 17:45:20 -0700 Subject: [PATCH] fix cached timestamps, closes #63 --- strftime.js | 3 +++ test.js | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/strftime.js b/strftime.js index c05c3f0..d34ad58 100644 --- a/strftime.js +++ b/strftime.js @@ -169,6 +169,9 @@ _cachedDate = new Date(_cachedDateTimestamp + getTimestampToUtcOffsetFor(_cachedDate) + _customTimezoneOffset); } } + else { + timestamp = _cachedDateTimestamp; + } date = _cachedDate; } else { diff --git a/test.js b/test.js index bc60e7b..66db148 100755 --- a/test.js +++ b/test.js @@ -195,6 +195,25 @@ assert.formatTZ('%F %r %z', '2011-06-07 11:21:45 AM -0730', '-0730'); assert.formatTZ('%F %r %:z', '2011-06-07 11:21:45 AM -07:30', '-0730'); ok('Time zone offset'); +/// caching +(function() { + // this test fails when the 2 calls cross a millisecond boundary, so try a number of times + var CacheAttempts = 10; + var MaxFailures = 1; + var failures = 0; + for (var i = 0; i < CacheAttempts; ++i) { + var expectedMillis = strftime('%L'); + var millis = strftime('%L'); + if (expectedMillis != millis) { + ++failures; + } + } + if (failures > MaxFailures) { + assert.fail('timestamp caching appears to be broken (' + failures + ' failed attempts out of ' + CacheAttempts + ')'); + } +}()); +ok('Cached timestamps'); + /// helpers