Skip to content

Commit

Permalink
Merge pull request #27 from peteriman/Fixes-#24-TimezoneFormat
Browse files Browse the repository at this point in the history
Fixes timezone format
  • Loading branch information
lamweili committed Jan 10, 2022
2 parents 3d3e67d + 289bf73 commit 6319fa5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -38,11 +38,11 @@ Format string can be anything, but the following letters will be replaced (and l
* mm - `date.getMinutes()`
* ss - `date.getSeconds()`
* SSS - `date.getMilliseconds()`
* O - timezone offset in +hm format (note that time will still be local if displaying offset)
* O - timezone offset in ±hhmm format (note that time will still be local if displaying offset)

Built-in formats:
* `format.ISO8601_FORMAT` - `2017-03-14T14:10:20.391` (local time used)
* `format.ISO8601_WITH_TZ_OFFSET_FORMAT` - `2017-03-14T14:10:20.391+1100` (local + TZ used)
* `format.ISO8601_WITH_TZ_OFFSET_FORMAT` - `2017-03-14T14:10:20.391+11:00` (local + TZ used)
* `format.DATETIME_FORMAT` - `14 03 2017 14:10:20.391` (local time used)
* `format.ABSOLUTETIME_FORMAT` - `14:10:20.391` (local time used)

Expand Down
7 changes: 5 additions & 2 deletions lib/index.js
Expand Up @@ -27,7 +27,7 @@ function offset(timezoneOffset) {
if (m.length === 1) {
m = "0" + m;
}
return timezoneOffset < 0 ? "+" + h + m : "-" + h + m;
return timezoneOffset === 0 ? "Z" : (timezoneOffset < 0 ? "+" : "-") + h + ":" + m;
}

function asString(format, date) {
Expand Down Expand Up @@ -126,11 +126,14 @@ function extractDateParts(pattern, str, missingValuesDate) {
},
{
pattern: /O/,
regexp: "[+-]\\d{3,4}|Z",
regexp: "[+-]\\d{1,2}:?\\d{2}?|Z",
fn: function(date, value) {
if (value === "Z") {
value = 0;
}
else {
value = value.replace(":", "");
}
var offset = Math.abs(value);
var timezoneOffset = (value > 0 ? -1 : 1 ) * ((offset % 100) + Math.floor(offset / 100) * 60);
// Per ISO8601 standard: UTC = local time - offset
Expand Down
6 changes: 3 additions & 3 deletions test/date_format-test.js
Expand Up @@ -35,15 +35,15 @@ describe('date_format', function() {

// when tz offset is in the pattern, the date should be in local time
dateFormat.asString(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, tzDate)
.should.eql('2010-01-11T14:31:30.005+1100');
.should.eql('2010-01-11T14:31:30.005+11:00');

tzDate = createFixedDate();
tzDate.getTimezoneOffset = function () {
return 120;
};

dateFormat.asString(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, tzDate)
.should.eql('2010-01-11T14:31:30.005-0200');
.should.eql('2010-01-11T14:31:30.005-02:00');
});

it('should provide a just-the-time format', function() {
Expand All @@ -56,6 +56,6 @@ describe('date_format', function() {
return 120;
};

dateFormat.asString('O.SSS.ss.mm.hh.dd.MM.yy', customDate).should.eql('-0200.005.30.31.14.11.01.10');
dateFormat.asString('O.SSS.ss.mm.hh.dd.MM.yy', customDate).should.eql('-02:00.005.30.31.14.11.01.10');
});
});

0 comments on commit 6319fa5

Please sign in to comment.