Skip to content

Instantly share code, notes, and snippets.

@kanonji
Created April 20, 2011 16:27
Show Gist options
  • Save kanonji/931834 to your computer and use it in GitHub Desktop.
Save kanonji/931834 to your computer and use it in GitHub Desktop.
Class to handle time for JavaScript.
var Time = function( time, type ){
this.hour = 0;
this.minute = 0;
this.second = 0;
this.millisecond = 0;
var HOURS = 60 * 60 * 1000;
var MINUTES = 60 * 1000;
var SECONDS = 1000;
this._HOUR = function(){ return HOURS };
this._MINUTE = function(){ return MINUTES };
this._SECOND = function(){ return SECONDS };
if (! type ) type = 3;
if ( typeof time == 'number' ){
switch ( type ){
case 'h':
case 0:
case 'hour':
case 'hours':
time = time * this._HOUR();
break;
case 'm':
case 1:
case 'min':
case 'minute':
case 'minutes':
time = time * this._MINUTE();
break;
case 's':
case 2:
case 'sec':
case 'second':
case 'seconds':
time = time * this._SECOND();
break;
case 'ms':
case 3:
case 'msec':
case 'msecond':
case 'mseconds':
case 'millisec':
case 'millisecond':
case 'milliseconds':
default:
time = time;
break;
}
}
this._init( time );
};
Time.prototype.toString = function () {
return this.toISO8601();
}
Time.prototype.toISO8601 = function () {
return this.toStringf( '%H:%M:%S' );
}
Time.prototype.toStringf = function ( format ) {
return format.replace( '%H', this.zerofill( this.hour, 2 ) )
.replace( '%M', this.zerofill( this.minute, 2 ) )
.replace( '%S', this.zerofill( this.second, 2 ) )
.replace( '%N', this.zerofill( this.millisecond, 3) );
}
Time.prototype.addHour = function ( int ) {
this._init( this.getMillisecond() + int * this._HOUR() );
return this;
};
Time.prototype.addMinute = function ( int ) {
this._init( this.getMillisecond() + int * this._MINUTE() );
return this;
};
Time.prototype.addSecond = function ( int ) {
this._init( this.getMillisecond() + int * this._SECOND() );
return this;
};
Time.prototype.addMillisecond = function ( int ) {
this._init( this.getMillisecond() + int );
return this;
};
Time.prototype.getHour = function () {
return this.hour;
};
Time.prototype.getMinute = function () {
return ( this.hour * this._HOUR() + this.minute * this._MINUTE() ) / this._MINUTE();
};
Time.prototype.getSecond = function () {
return ( this.hour * this._HOUR() + this.minute * this._MINUTE() + this.second * this._SECOND() ) / this._SECOND();
};
Time.prototype.getMillisecond = function () {
return this.hour * this._HOUR() + this.minute * this._MINUTE() + this.second * this._SECOND() + this.millisecond;
};
Time.prototype._init = function ( time ) {
if( typeof time == 'string' ){
time = time.split( '.' );
if ( time[1] ) this.millisecond = parseInt( time[1] );
time = time[0].split( ':' );
if ( time[2] ) this.second = parseInt( time[2] );
if ( time[1] ) this.minute = parseInt( time[1] );
if ( time[0] ) this.hour = parseInt( time[0] );
} else {
this.hour = Math.floor( time / this._HOUR() );
time -= this.hour * this._HOUR();
this.minute = Math.floor( time / this._MINUTE() );
time -= this.minute * this._MINUTE();
this.second = Math.floor( time / this._SECOND() );
time -= this.second * this._SECOND();
this.millisecond = time;
}
};
Time.prototype.zerofill = function ( number, size ) {
if ( number < 0 ) throw "illegal argument.";
var s = number != 0 ? Math.log( number ) * Math.LOG10E : 0;
var i, str;
for( i=1,n=size-s,str="";i<n;i++ ) str += "0";
return str+number;
}
/* vim: set ts=2 sw=2 sts=2 et ff=unix ft=javascript : */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment