Files
Iris/src/js/components/Dater.js

102 lines
2.6 KiB
JavaScript
Raw Normal View History

2016-10-31 16:49:42 +13:00
import React, { PropTypes } from 'react'
export default class Dater extends React.Component{
constructor(props) {
super(props);
}
/**
* Format time duration
*
* @param milliseconds int
* @return string (HH:mm:ss)
**/
durationTime( milliseconds = false ){
if( !milliseconds ) return null
2016-11-02 21:23:35 +13:00
var string = '';
var total_hours, total_minutes, total_seconds, minutes, seconds;
// get total values for each
total_seconds = Math.floor( milliseconds / 1000 )
total_minutes = Math.floor( milliseconds / (1000 * 60) )
total_hours = Math.floor(milliseconds / (1000 * 60 * 60))
// get left-over number of seconds
seconds = total_seconds - ( total_minutes * 60 )
if( seconds <= 9 ) seconds = '0'+ seconds
2016-11-02 21:23:35 +13:00
// get left-over number of minutes
minutes = total_minutes - ( total_hours * 60 )
2016-11-02 21:23:35 +13:00
if( minutes <= 9 && total_hours ) minutes = '0'+ minutes
2016-11-03 08:57:16 +13:00
if( minutes == 0 ) minutes = '0'
2016-11-02 21:23:35 +13:00
if( total_hours ) string += total_hours+':'
if( minutes ) string += minutes+':'
if( seconds ) string += seconds
2016-11-03 08:57:16 +13:00
return string
}
/**
* Format time duration as a human-friendly sentence
*
* @param milliseconds int
* @return string (eg 2+ hours)
**/
durationSentence( milliseconds = false ){
if( !milliseconds ) return null
var string = '';
var total_hours, total_minutes, total_seconds, minutes, seconds;
// get total values for each
total_seconds = Math.floor( milliseconds / 1000 )
total_minutes = Math.floor( milliseconds / (1000 * 60) )
total_hours = Math.floor(milliseconds / (1000 * 60 * 60))
if( total_hours > 1 ) return total_hours+'+ hrs'
if( total_minutes > 1 ) return total_minutes+' mins'
if( total_seconds ) return total_seconds+' sec'
}
2016-10-31 16:49:42 +13:00
calculate(){
switch(this.props.type){
case 'total-time':
var duration = 0;
var tracks = this.props.data
for( var i = 0; i < tracks.length; i++ ){
if( tracks[i].duration_ms ) duration += parseInt(tracks[i].duration_ms);
if( tracks[i].length ) duration += parseInt(tracks[i].length);
}
return this.durationSentence(duration)
break
2016-10-31 16:49:42 +13:00
case 'length':
return this.durationTime(this.props.data)
2016-10-31 16:49:42 +13:00
break
case 'date':
2016-11-02 21:23:35 +13:00
var date = new Date(this.props.data)
return date.getDate()+'/'+date.getMonth()+'/'+date.getFullYear()
break
case 'ago':
var date = new Date(this.props.data)
var diff = new Date() - date
// todo: calculate nice diff
return diff
2016-10-31 16:49:42 +13:00
break
default:
return null
}
}
render(){
return <span className="dater">{ this.calculate() }</span>
}
}