Skip to content
This repository has been archived by the owner on Apr 2, 2019. It is now read-only.

Commit

Permalink
Allow for timestamps to be passed into DateTimeFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Kashishian committed May 3, 2013
1 parent ade8f25 commit 05d7e81
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,19 @@ _.extend(DatetimeFormatter.prototype, {
ISO_SPLITTER_RE: /T|Z| +/,

_convert: function (data, validate) {
data = data.trim();
var parts = data.split(this.ISO_SPLITTER_RE) || [];
var date, time = null;
if (_.isNumber(data)) {
var actualData = new Date(data * 1000);
date = lpad(actualData.getUTCFullYear(), 4, 0) + '-' + lpad(actualData.getUTCMonth() + 1, 2, 0) + '-' + lpad(actualData.getUTCDate(), 2, 0);
time = lpad(actualData.getUTCHours(), 2, 0) + ':' + lpad(actualData.getUTCMinutes(), 2, 0) + ':' + lpad(actualData.getUTCSeconds(), 2, 0);
}
else {
data = data.trim();
var parts = data.split(this.ISO_SPLITTER_RE) || [];
date = this.DATE_RE.test(parts[0]) ? parts[0] : '';
time = date && parts[1] ? parts[1] : this.TIME_RE.test(parts[0]) ? parts[0] : '';
}

var date = this.DATE_RE.test(parts[0]) ? parts[0] : '';
var time = date && parts[1] ? parts[1] : this.TIME_RE.test(parts[0]) ? parts[0] : '';

var YYYYMMDD = this.DATE_RE.exec(date) || [];
var HHmmssSSS = this.TIME_RE.exec(time) || [];
Expand Down
5 changes: 5 additions & 0 deletions test/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ describe("A DatetimeFormatter", function () {
expect(formatter.fromRaw("2012-02-29")).toBe("2012-02-29T00:00:00Z");
});

it(".fromRaw() can convert an unix timestamp to an ISO datetime string", function () {
var formatter = new Backgrid.DatetimeFormatter;
expect(formatter.fromRaw(1356998400)).toBe("2013-01-01T00:00:00Z");
});

it(".fromRaw() can convert an ISO date string to an ISO date string", function () {
var formatter = new Backgrid.DatetimeFormatter({
includeTime: false
Expand Down

0 comments on commit 05d7e81

Please sign in to comment.