Skip to content

Commit

Permalink
Merge branch 'date'
Browse files Browse the repository at this point in the history
  • Loading branch information
drewish committed Mar 27, 2013
2 parents 4c41241 + a490b0a commit a5c9cb8
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
40 changes: 40 additions & 0 deletions exiv2.js
Expand Up @@ -2,3 +2,43 @@ var exiv2 = require('./build/Release/exiv2.node')


// Export the properties implemented in the extension. // Export the properties implemented in the extension.
exports = module.exports = exiv2; exports = module.exports = exiv2;

/**
* Convert exiv2's date-time strings into Date instances.
*
* The first parameter is the hash of tags loaded by getImageTags().
*
* The second parameter is an optional tag name. The default is
* 'Exif.Photo.DateTimeOriginal' with sub-second data being loaded from
* 'Exif.Photo.SubSecTimeOriginal', if it exists. See http://exiv2.org/tags.html
* for a complete list of tags.
*/
exports.getDate = function getDate(tags, dateTimeTag) {
dateTimeTag = dateTimeTag || 'Exif.Photo.DateTimeOriginal';
if (tags[dateTimeTag] === undefined) {
return null;
}

// Date.parse() doesn't recognize the custom format ('2012:04:14 17:45:52') so
// the easiest thing to do is convert it to ISO 8601 (e.g.
// "2012-04-14T17:45:52") which it does recognize. The colons in the date need
// to become hyphens and the space needs to become a 'T'.

// So split it into two...
var datetime = tags[dateTimeTag].split(' ');
// ...then change the colons in date into hyphens...
datetime[0] = datetime[0].replace(/:/g, '-');
// ...then join it back together with a 'T' and parse it.
var d = new Date(Date.parse(datetime.join('T')));

// If there's a matching sub second tag scale it from hundredths of a second
// to thousands and apply it to the date. The extra precision can be useful
// when sorting photos by time because multiple photos could be taken in a
// single second.
var subSecTag = dateTimeTag.replace(/\.DateTime/, '.SubSecTime');
if (tags[subSecTag] !== undefined) {
d.setMilliseconds(tags[subSecTag] * 10);
}

return d;
}
33 changes: 33 additions & 0 deletions test/test.js
@@ -1,5 +1,6 @@
var exiv = require('../exiv2') var exiv = require('../exiv2')
, fs = require('fs') , fs = require('fs')
, util = require('util')
, should = require('should') , should = require('should')
, dir = __dirname + '/images'; , dir = __dirname + '/images';


Expand Down Expand Up @@ -139,4 +140,36 @@ describe('exiv2', function(){
}); });
}); });
}); });

describe('.getDate()', function() {
var tags = {'Exif.Photo.DateTimeOriginal': '2012:04:14 17:45:52'};

it("should return a false value if the tag doesn't exist", function() {
var d = exiv.getDate({});
should.not.exist(d);
});

it("should return a Date", function() {
var d = exiv.getDate(tags);
should.ok(util.isDate(d));
});

it("should be the correct date and time", function() {
var d = exiv.getDate(tags);
d.toISOString().should.equal('2012-04-14T17:45:52.000Z');
});

it("should include milliseconds if available", function() {
tags['Exif.Photo.SubSecTimeOriginal'] = '99';
var d = exiv.getDate(tags);
d.toISOString().should.equal('2012-04-14T17:45:52.990Z');
});

it("should use custom tags", function() {
tags['Exif.Photo.DateTimeDigitized'] = '2012:04:14 17:08:08';
tags['Exif.Photo.SubSecTimeDigitized'] = '88';
var d = exiv.getDate(tags, 'Exif.Photo.DateTimeDigitized');
d.toISOString().should.equal('2012-04-14T17:08:08.880Z');
});
})
}) })

0 comments on commit a5c9cb8

Please sign in to comment.