Last active
August 29, 2015 14:24
-
-
Save edhaase/fc248ec9fad74c0fded7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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