Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes timezone format #27

Merged
merged 3 commits into from Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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');
});
});