From 14ad6dc9021bea56ca53af87e8b1b7f0f63d8b91 Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Sun, 8 Feb 2015 20:42:58 -0800 Subject: [PATCH 1/6] release v0.8.3 for bower --- bower.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bower.json b/bower.json index 8ed1185..84ad15f 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "strftime", - "version": "0.8.2", + "version": "0.8.3", "main": "strftime.js", "ignore": [ "Readme.md", From 9e191bf5c6a60201c7493e45807c968074bec498 Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Sun, 8 Feb 2015 20:44:15 -0800 Subject: [PATCH 2/6] ignore test directory in bower.json --- bower.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bower.json b/bower.json index 84ad15f..ea48f3b 100644 --- a/bower.json +++ b/bower.json @@ -5,7 +5,7 @@ "ignore": [ "Readme.md", "Makefile", - "test/*", + "test", "*.json" ], "dependencies": {}, From b0a4d5a84a64cc887f80213fb8a2f68fc107c324 Mon Sep 17 00:00:00 2001 From: Alexandr Nikitin Date: Tue, 3 Mar 2015 12:26:23 +0200 Subject: [PATCH 3/6] Fix conversion of date to UTC Method based on one of SO answers: http://stackoverflow.com/questions/948532/how-do-you-convert-a-javascript-date-to-utc --- strftime.js | 14 ++++++++++---- test/test.js | 1 + 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/strftime.js b/strftime.js index cdc5d45..6845863 100644 --- a/strftime.js +++ b/strftime.js @@ -279,10 +279,16 @@ }); } - function dateToUTC(d) { - var msDelta = (d.getTimezoneOffset() || 0) * 60000; - return new Date(d.getTime() + msDelta); - } + function dateToUTC(d) { + return new Date( + d.getUTCFullYear(), + d.getUTCMonth(), + d.getUTCDate(), + d.getUTCHours(), + d.getUTCMinutes(), + d.getUTCSeconds(), + d.getUTCMilliseconds()); + } var RequiredDateMethods = ['getTime', 'getTimezoneOffset', 'getDay', 'getDate', 'getMonth', 'getFullYear', 'getYear', 'getHours', 'getMinutes', 'getSeconds']; function quacksLikeDate(x) { diff --git a/test/test.js b/test/test.js index 48572cd..e2f2ec3 100755 --- a/test/test.js +++ b/test/test.js @@ -104,6 +104,7 @@ assert.format('%y', '11') assert.format('%Z', null, 'GMT') assert.format('%z', null, '+0000') assert.format('%%', '%') // any other char +assert.format('%F %T', null, '1970-01-01 00:00:00', new Date(0)) ok('GMT') From f7799a1d216ddda62a8b156d9846ff65aa115533 Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Wed, 4 Mar 2015 09:14:58 -0800 Subject: [PATCH 4/6] correct fuzzy interpretation of old years by new Date() --- strftime.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/strftime.js b/strftime.js index 6845863..1d06d10 100644 --- a/strftime.js +++ b/strftime.js @@ -280,14 +280,22 @@ } function dateToUTC(d) { - return new Date( - d.getUTCFullYear(), + var year = d.getUTCFullYear(); + var date = new Date( + year, d.getUTCMonth(), d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(), - d.getUTCMilliseconds()); + d.getUTCMilliseconds() + ); + // In old dates, years is incorrectly interpreted as a 2-digit year with base 1900. + // Correct this by setting the year explicitly after the fuzzy creation process. + if (date.getFullYear() != year) { + date.setFullYear(year); + } + return date; } var RequiredDateMethods = ['getTime', 'getTimezoneOffset', 'getDay', 'getDate', 'getMonth', 'getFullYear', 'getYear', 'getHours', 'getMinutes', 'getSeconds']; From 4de2693e51005eb8cefdebf747256fbedde74e45 Mon Sep 17 00:00:00 2001 From: Cory Heslip Date: Wed, 4 Mar 2015 16:50:39 -0700 Subject: [PATCH 5/6] added support for extended offset format --- strftime.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/strftime.js b/strftime.js index 1d06d10..2955183 100644 --- a/strftime.js +++ b/strftime.js @@ -114,8 +114,8 @@ // Most of the specifiers supported by C's strftime, and some from Ruby. // Some other syntax extensions from Ruby are supported: %-, %_, and %0 // to pad with nothing, space, or zero (respectively). - return fmt.replace(/%([-_0]?.)/g, function(_, c) { - var mod, padding; + return fmt.replace(/%([-_0:]?.)/g, function(_, c) { + var mod, padding, ext; if (c.length == 2) { mod = c[0]; @@ -131,6 +131,9 @@ else if (mod == '0') { padding = '0'; } + else if (mod == ":") { + ext = true; + } else { // unrecognized, return the format return _; @@ -267,11 +270,12 @@ // '+0000' case 'z': if (options.utc) { - return "+0000"; + return ext ? "+00:00" : "+0000"; } else { var off = typeof tz == 'number' ? tz : -d.getTimezoneOffset(); - return (off < 0 ? '-' : '+') + pad(Math.floor(Math.abs(off) / 60)) + pad(Math.abs(off) % 60); + var sep = ext ? ":" : ""; // separator for extended offset + return (off < 0 ? '-' : '+') + pad(Math.floor(Math.abs(off) / 60)) + sep + pad(Math.abs(off) % 60); } default: return c; From 1222467e47d310659f535ad95d0a578a737e54bb Mon Sep 17 00:00:00 2001 From: Cory Heslip Date: Wed, 4 Mar 2015 17:18:19 -0700 Subject: [PATCH 6/6] added tests and updated docs --- Readme.md | 5 +++-- test/test.js | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Readme.md b/Readme.md index f61a89e..ee67fd0 100644 --- a/Readme.md +++ b/Readme.md @@ -111,11 +111,12 @@ e.g. `%q` becomes `q`. Use `%%` to get a literal `%` sign. For more detail see `man 3 strftime` as the format specifiers should behave identically. If behaviour differs please [file a bug](https://github.com/samsonjs/strftime/issues/new). -Any specifier can be modified with `-`, `_`, or `0` as well, as in Ruby. +Any specifier can be modified with `-`, `_`, `0`, or `:` as well, as in Ruby. Using `%-` will omit any leading zeroes or spaces, `%_` will force spaces for padding instead of the default, and `%0` will force zeroes for padding. There's some redundancy here as `%-d` and `%e` have the same result, but it -solves some awkwardness with formats like `%l`. +solves some awkwardness with formats like `%l`. Using `%:` for time zone offset, +as in `%:z` will insert a colon as a delimiter. Contributors ============ diff --git a/test/test.js b/test/test.js index e2f2ec3..c6f91e9 100755 --- a/test/test.js +++ b/test/test.js @@ -103,6 +103,7 @@ assert.format('%Y', '2011') assert.format('%y', '11') assert.format('%Z', null, 'GMT') assert.format('%z', null, '+0000') +assert.format('%:z', null, '+00:00') assert.format('%%', '%') // any other char assert.format('%F %T', null, '1970-01-01 00:00:00', new Date(0)) ok('GMT') @@ -175,6 +176,7 @@ assert.formatTZ('%F %r %z', '2011-06-07 08:51:45 PM +0200', '+0200') assert.formatTZ('%F %r %z', '2011-06-07 11:51:45 AM -0700', -420) assert.formatTZ('%F %r %z', '2011-06-07 11:51:45 AM -0700', '-0700') 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') @@ -221,5 +223,6 @@ function testTimezone(regex) { assert.format('%T', T, '18:51:45') assert.format('%Z', tz, 'GMT') assert.format('%z', sign + '0' + Math.abs(hourDiff) + '00', '+0000') + assert.format('%:z', sign + '0' + Math.abs(hourDiff) + ':00', '+00:00') } }