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

Parse date into ISO 8601 format instead of seconds since UTC epoch #28

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 10 additions & 76 deletions lib/date.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,18 @@
function parseNumber(s) {
return parseInt(s, 10);
}

//in seconds
var hours = 3600;
var minutes = 60;

//take date (year, month, day) and time (hour, minutes, seconds) digits in UTC
//and return a timestamp in seconds
function parseDateTimeParts(dateParts, timeParts) {
dateParts = dateParts.map(parseNumber);
timeParts = timeParts.map(parseNumber);
var year = dateParts[0];
var month = dateParts[1] - 1;
var day = dateParts[2];
var hours = timeParts[0];
var minutes = timeParts[1];
var seconds = timeParts[2];
var date = Date.UTC(year, month, day, hours, minutes, seconds, 0);
var timestamp = date / 1000;
return timestamp;
}

//parse date with "2004-09-04T23:39:06-08:00" format,
//one of the formats supported by ISO 8601, and
//convert to utc timestamp in seconds
function parseDateWithTimezoneFormat(dateTimeStr) {

var dateParts = dateTimeStr.substr(0, 10).split('-');
var timeParts = dateTimeStr.substr(11, 8).split(':');
var timezoneStr = dateTimeStr.substr(19, 6);
var timezoneParts = timezoneStr.split(':').map(parseNumber);
var timezoneOffset = (timezoneParts[0] * hours) +
(timezoneParts[1] * minutes);

var timestamp = parseDateTimeParts(dateParts, timeParts);
//minus because the timezoneOffset describes
//how much the described time is ahead of UTC
timestamp -= timezoneOffset;

if(typeof timestamp === 'number' && !isNaN(timestamp)) {
return timestamp;
}
}

//parse date with "YYYY:MM:DD hh:mm:ss" format, convert to utc timestamp in seconds
function parseDateWithSpecFormat(dateTimeStr) {
var parts = dateTimeStr.split(' '),
dateParts = parts[0].split(':'),
timeParts = parts[1].split(':');

var timestamp = parseDateTimeParts(dateParts, timeParts);

if(typeof timestamp === 'number' && !isNaN(timestamp)) {
return timestamp;
}
}

function parseExifDate(dateTimeStr) {
//some easy checks to determine two common date formats
//some easy checks to determine common date formats

//is the date in the standard "YYYY:MM:DD hh:mm:ss" format?
var isSpecFormat = dateTimeStr.length === 19 &&
dateTimeStr.charAt(4) === ':';
//is the date in the non-standard format,
//"2004-09-04T23:39:06-08:00" to include a timezone?
var isTimezoneFormat = dateTimeStr.length === 25 &&
dateTimeStr.charAt(10) === 'T';
var timestamp;

if(isTimezoneFormat) {
return parseDateWithTimezoneFormat(dateTimeStr);
}
else if(isSpecFormat) {
return parseDateWithSpecFormat(dateTimeStr);
}
//if so, convert to ISO 8601 by replacing first two ':'s with '-'s
if (dateTimeStr.length === 19 &&
dateTimeStr.charAt(4) === ':' &&
dateTimeStr.charAt(7) === ':') {
dateTimeStr = dateTimeStr.slice(0, 4) + '-' +
dateTimeStr.slice(5, 7) + '-' + dateTimeStr.slice(8);
}

return dateTimeStr;
}

module.exports = {
parseDateWithSpecFormat: parseDateWithSpecFormat,
parseDateWithTimezoneFormat: parseDateWithTimezoneFormat,
parseExifDate: parseExifDate
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"devDependencies": {
"browserify": "^7.0.0",
"uglify-js": "^2.4.15"
"uglify-js": "^2.4.15",
"nodeunit": "^0.11.3"
}
}
60 changes: 0 additions & 60 deletions test/test-date.js

This file was deleted.

17 changes: 13 additions & 4 deletions test/test-simplify.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
var simplify = require('../lib/simplify');

function parseDate(date) {
// Force UTC time for testing purposes.
if (date.indexOf(':') >= 0 &&
(date.indexOf('Z') < 0 && date.indexOf('T') < 0)) {
date += 'Z';
}
return new Date(date).getTime();
}

module.exports = {
'test castDateValues': function(test) {
var values = {
Expand All @@ -16,9 +25,9 @@ module.exports = {
}
simplify.castDateValues(getTagValue, setTagValue);
test.strictEqual(Object.keys(setValues).length, 3);
test.strictEqual(setValues.DateTimeOriginal, 0);
test.strictEqual(setValues.CreateDate, 5 * 3600);
test.strictEqual(setValues.ModifyDate, 5 * 3600);
test.strictEqual(parseDate(setValues.DateTimeOriginal), 0);
test.strictEqual(parseDate(setValues.CreateDate), 5 * 3600 * 1000);
test.strictEqual(parseDate(setValues.ModifyDate), 5 * 3600 * 1000);
test.done();
}
}
}