Skip to content

Instantly share code, notes, and snippets.

@edhaase
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edhaase/fc248ec9fad74c0fded7 to your computer and use it in GitHub Desktop.
Save edhaase/fc248ec9fad74c0fded7 to your computer and use it in GitHub Desktop.
/**
* @module TimeStamp
* @todo Unit tests
*/
'use strict';
var moment = require('moment');
/**
* @class
* @param {TimeStamp|string} [src] - A timestamp to parse
*/
function TimeStamp(src) {
if ( !(this instanceof TimeStamp) )
throw new Error('Requires use of "new" keyword');
this.time = moment.utc();
/* copy constructor? */
if(src && src instanceof TimeStamp)
this.time = src.time;
else
this.time = moment.utc(src);
Object.seal(this);
};
/**
* @returns String representation of TimeStamp in ISO 8601
*/
TimeStamp.prototype.toString = function TimeStampFormatter() {
return this.time.toISOString();
}
/** Updates timestamp to current time.
* @returns This for chaining.
*/
TimeStamp.prototype.update = function() {
this.time = moment.utc();
return this;
}
/**
* @returns Age of timestamp
*/
TimeStamp.prototype.age = function() {
return moment.duration(this.time.diff(moment()));
}
/**
* @returns Age of timestamp in seconds
*/
TimeStamp.prototype.ageInSeconds = function() {
return Math.abs( this.age().asSeconds() );
}
// TimeStamp.prototype.valueOf
module.exports = TimeStamp;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment