Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'date'

  • Loading branch information...
commit a5c9cb8c15b84c78159cbd78df4f3e10ffe31af5 2 parents 4c41241 + a490b0a
@drewish drewish authored
Showing with 73 additions and 0 deletions.
  1. +40 −0 exiv2.js
  2. +33 −0 test/test.js
View
40 exiv2.js
@@ -2,3 +2,43 @@ var exiv2 = require('./build/Release/exiv2.node')
// Export the properties implemented in the extension.
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;
+}
View
33 test/test.js
@@ -1,5 +1,6 @@
var exiv = require('../exiv2')
, fs = require('fs')
+ , util = require('util')
, should = require('should')
, dir = __dirname + '/images';
@@ -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');
+ });
+ })
})
Please sign in to comment.
Something went wrong with that request. Please try again.