94 shaares
1 résultat
taggé
time
Je met de coté ici une petite classe technique qui permet de faire des opérations sur des horaires (addition, soustractions, division etc)
Elle était a la base faite pour un script google sheets du coup le jour où je veux l'utiliser il faudra se débarasser des classes de chez google (genre le Logger)
function test(){
Logger.log("plus = 09:30 : ", new Time("08:00").plusTime(new Time("01:30")))
Logger.log("plus = 10:00 : ", new Time("08:00").plusTime(new Time("02:00")))
Logger.log("plus = 08:01 : ", new Time("08:00").plusTime(new Time("00:01")))
Logger.log("minus = 07:59 : ", new Time("08:00").minusTime(new Time("00:01")))
Logger.log("minus = 23:00 : ", new Time("01:00").minusTime(new Time("02:00")))
Logger.log("minus = 22:59 : ", new Time("01:00").minusTime(new Time("02:01")))
Logger.log("minus = 22:58 : ", new Time("01:00").minusTime(new Time("26:02")))
Logger.log("divide = 03:20 : ", new Time("10:00").divideTime(new Time("03:00")))
Logger.log("divide = 04:00 : ", new Time("08:00").divideTime(new Time("02:00")))
Logger.log("divide = 08:00 : ", new Time("08:00").divideTime(new Time("01:00")))
Logger.log("modulo = 00:15 : ", new Time("01:00").moduloTime(new Time("00:45")))
Logger.log("modulo = 01:00 : ", new Time("02:30").moduloTime(new Time("01:30")))
Logger.log("asDate = AUJOURD'HUIT01:00:00 : ", new Time("01:00").asDate())
Logger.log("asDecimal = 0.5 : ", new Time("00:30").asDecimal())
Logger.log("asDecimal = 1.5 : ", new Time("01:30").asDecimal())
Logger.log("asDecimal = 1.25 : ", new Time("01:15").asDecimal())
Logger.log("fromDecimal = 12:30 : ", Time.fromDecimal(12.5))
Logger.log("fromDecimal = 00:45 : ", Time.fromDecimal(0.75))
Logger.log("fromDecimal = 01:45 : ", Time.fromDecimal(1.75))
Logger.log("fromDecimal = 02:30 : ", Time.fromDecimal(2.5))
Logger.log("fromMinutes = 23:45 : ", Time.fromMinutes(-15))
Logger.log("fromMinutes = 02:00 : ", Time.fromMinutes(120))
}
/**
* Représente une heure de la journée au format "HH:MM"
*/
class Time{
/**
* Constructeur.
* Param :
* hour:String - L'heure au format "HH:MM"
*/
constructor(hour){
this.hour = hour
}
/**
* Initialise un Time à partir d'un nombre de minutes
* Param :
* mins:String - L'heure au format "HH:MM"
*/
static fromMinutes(value){
function addZeroToHour(hour, minutes){
if(minutes < 0){
hour -= 1
}
return hour >= 0 ? ((hour<10 ? '0' : '') + hour) : addZeroToHour(24 + hour)
}
function addZeroToMinutes(minutes){
return minutes >= 0 ? ((minutes<10 ? '0' : '') + minutes) : addZeroToMinutes(60 + minutes)
}
var hour = (value/60 |0) % 24
var minutes = value % 60
return new Time(addZeroToHour(hour, minutes) + ':' + addZeroToMinutes(minutes))
}
static fromDecimal(value){
let hour = Math.floor(value)
let minutes = (value % 1) * 60
return new Time((hour < 10 ? '0' : '') + Math.floor(hour) + ":" + (minutes<10 ? '0' : '') + Math.floor(minutes))
}
asDate(){
var splitted = this.hour.split(':')
let date = new Date()
date.setHours(splitted[0], splitted[1], 0)
return date
}
asMinutes(){
var splitted = this.hour.split(':')
return splitted[0]*60 + +splitted[1]
}
asDecimal(){
return this.asMinutes() * (1/60)
}
asHours(){
return this.hour
}
plusTime(other) {
return Time.fromMinutes(this.asMinutes() + other.asMinutes())
}
minusTime(other) {
return Time.fromMinutes(this.asMinutes() - other.asMinutes())
}
divideTime(other){
return Time.fromDecimal((this.asMinutes() / other.asMinutes()))
}
moduloTime(other){
return Time.fromMinutes(this.asMinutes() % other.asMinutes())
}
}