Skip to content

Commit

Permalink
allow string timezone offsets of the form [-+]HHMM, closes #33
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonjs committed Jan 29, 2014
1 parent 61f6193 commit 99382d2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
7 changes: 7 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ Time zones can be passed in as an offset from GMT in minutes.
console.log(strftimeTZ('%F %T', new Date(1307472705067), 120)) // => 2011-06-07 20:51:45


Alternatively you can use the timezone format used by ISO 8601, `+HHMM` or `-HHMM`.

var strftimeTZ = require('strftime').strftimeTZ
console.log(strftimeTZ('', new Date(1307472705067), '-0700')) // => June 07, 11 11:51:45
console.log(strftimeTZ('%F %T', new Date(1307472705067), '+0200')) // => 2011-06-07 20:51:45


Supported Specifiers
====================

Expand Down
25 changes: 20 additions & 5 deletions strftime.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@
// locale is optional
namespace.strftimeTZ = strftime.strftimeTZ = strftimeTZ;
function strftimeTZ(fmt, d, locale, timezone) {
if (typeof locale == 'number' && timezone == null) {
if ((typeof locale == 'number' || typeof locale == 'string') && timezone == null) {
timezone = locale;
locale = undefined;
}
return _strftime(fmt, d, locale, { timezone: timezone });
return _strftime(fmt, d, locale, { timezone: timezone, utc: true });
}

namespace.strftimeUTC = strftime.strftimeUTC = strftimeUTC;
Expand Down Expand Up @@ -88,12 +88,27 @@
// Hang on to this Unix timestamp because we might mess with it directly below.
var timestamp = d.getTime();

if (options.utc || typeof options.timezone == 'number') {
var tz = options.timezone;
var tzType = typeof tz;

if (options.utc || tzType == 'number' || tzType == 'string') {
d = dateToUTC(d);
}

if (typeof options.timezone == 'number') {
d = new Date(d.getTime() + (options.timezone * 60000));
if (tz) {
// ISO 8601 format timezone string, [-+]HHMM
//
// Convert to the number of minutes and it'll be applied to the date below.
if (tzType == 'string') {
var sign = tz[0] == '-' ? -1 : 1;
var hours = parseInt(tz.slice(1, 3), 10);
var mins = parseInt(tz.slice(3, 5), 10);
tz = sign * (60 * hours) + mins;
}

if (tzType) {
d = new Date(d.getTime() + (tz * 60000));
}
}

// Most of the specifiers supported by C's strftime, and some from Ruby.
Expand Down

0 comments on commit 99382d2

Please sign in to comment.