import { IStorage } from "./istorage" ; export enum Season { Spring = 0 , Summer = 1 , Autumn = 2 , Winter = 3 , LAST = 4 } export interface ITime { minute ? : number ; hour ? : number ; day ? : number ; season ? : Season ; year ? : number ; } export interface ITimeStorage { minute ? : string ; hour ? : string ; day ? : string ; season ? : string ; year ? : string ; } export class Time { public year : number ; public season : Season ; public day : number ; public hour : number ; public minute : number ; constructor ( year = 0 , season = 0 , day = 0 , hour = 0 , minute = 0 ) { this . year = year || 0 ; this . season = season || Season . Spring ; this . day = day || 0 ; this . hour = hour || 0 ; this . minute = minute || 0 ; } public ToStorage ( ) { const storage = { } ; if ( this . year !== 0 ) { storage . y = this . year . toString ( ) ; } if ( this . season !== 0 ) { storage . s = this . season . toString ( ) ; } if ( this . day !== 0 ) { storage . d = this . day . toString ( ) ; } if ( this . hour !== 0 ) { storage . h = this . hour . toString ( ) ; } if ( this . minute !== 0 ) { storage . m = this . minute . toString ( ) ; } return storage ; } public FromStorage ( storage ? ) { storage = storage || { } ; this . year = parseInt ( storage . y , 10 ) || this . year ; this . season = parseInt ( storage . s , 10 ) || this . season ; this . day = parseInt ( storage . d , 10 ) || this . day ; this . hour = parseInt ( storage . h , 10 ) || this . hour ; this . minute = parseInt ( storage . m , 10 ) || this . minute ; } public Clone ( ) { const time = new Time ( this . year , this . season , this . day , this . hour , this . minute ) ; return time ; } public Leq ( time ) { if ( this . year > time . year ) { return false ; } if ( this . season > time . season ) { return false ; } if ( this . day > time . day ) { return false ; } if ( this . hour > time . hour ) { return false ; } if ( this . minute > time . minute ) { return false ; } return true ; } public TimeToHour ( hour ? , minute ? ) { hour = hour || 0 ; minute = minute || 0 ; const step = { hour : hour - this . hour , minute : minute - this . minute , } ; if ( step . minute < 0 ) { step . minute += 60 ; step . hour -- ; } if ( step . hour < 0 ) { step . hour += 24 ; } return step ; } public Inc ( time ) { const minutes = time . minute || 0 ; const hours = time . hour || 0 ; const days = time . day || 0 ; const seasons = time . season || 0 ; const years = time . year || 0 ; this . minute += minutes ; this . hour += hours ; this . day += days ; this . season += seasons ; this . year += years ; while ( this . minute >= 60 ) { this . minute -= 60 ; this . hour ++ ; } while ( this . hour >= 24 ) { this . hour -= 24 ; this . day ++ ; } while ( this . day >= 30 ) { this . day -= 30 ; this . season ++ ; } while ( this . season >= Season . LAST ) { this . season -= Season . LAST ; this . year ++ ; } } public Dec ( time ) { const minutes = time . minute || 0 ; const hours = time . hour || 0 ; const days = time . day || 0 ; const seasons = time . season || 0 ; const years = time . year || 0 ; this . minute -= minutes ; this . hour -= hours ; this . day -= days ; this . season -= seasons ; this . year -= years ; while ( this . minute < 0 ) { this . minute += 60 ; this . hour -- ; } while ( this . hour < 0 ) { this . hour += 24 ; this . day -- ; } while ( this . day < 0 ) { this . day += 30 ; this . season -- ; } while ( this . season < 0 ) { this . season += Season . LAST ; this . year -- ; } if ( this . year < 0 ) { this . minute = 0 ; this . hour = 0 ; this . day = 0 ; this . season = 0 ; this . year = 0 ; } } public Expired ( ) { return ( this . year <= 0 ) && ( this . season <= 0 ) && ( this . day <= 0 ) && ( this . hour <= 0 ) && ( this . minute <= 0 ) ; } public DateString ( ) { let season ; if ( this . season === Season . Spring ) { season = "Spring" ; } else if ( this . season === Season . Summer ) { season = "Summer" ; } else if ( this . season === Season . Autumn ) { season = "Autumn" ; } else { season = "Winter" ; } const day = this . day + 1 ; let dateExtension = "th" ; if ( day < 10 || day > 20 ) { if ( day % 10 === 1 ) { dateExtension = "st" ; } else if ( day % 10 === 2 ) { dateExtension = "nd" ; } else if ( day % 10 === 3 ) { dateExtension = "rd" ; } } return day + dateExtension + " of " + season + ", year " + ( this . year + 1 ) ; } public DateStringShort ( ) { let season ; if ( this . season === Season . Spring ) { season = "Spring" ; } else if ( this . season === Season . Summer ) { season = "Summer" ; } else if ( this . season === Season . Autumn ) { season = "Autumn" ; } else { season = "Winter" ; } const day = this . day + 1 ; let dateExtension = "th" ; if ( day < 10 || day > 20 ) { if ( day % 10 === 1 ) { dateExtension = "st" ; } else if ( day % 10 === 2 ) { dateExtension = "nd" ; } else if ( day % 10 === 3 ) { dateExtension = "rd" ; } } return day + dateExtension + " of " + season ; } public TimeString ( ) { let minutes = "00" + this . minute ; minutes = minutes . substr ( minutes . length - 2 ) ; return this . hour + ":" + minutes ; } public DayTime ( ) { return this . hour >= 5 && this . hour < 10 ? "morning" : this . hour >= 10 && this . hour < 17 ? "day" : this . hour >= 17 && this . hour < 21 ? "evening" : "night" ; } public IsDay ( ) { return this . hour >= 8 && this . hour < 20 ; } public LightStr ( light , dark ) { return this . hour >= 6 && this . hour < 21 ? light : dark ; } public ToDays ( ) { let day = this . day + this . hour / 24 + this . minute / 24 / 60 ; day += this . season * 30 ; day += this . year * Season . LAST * 30 ; return day ; } public ToHours ( ) { let hour = this . hour + this . minute / 60 ; hour += this . day * 24 ; hour += this . season * 30 * 24 ; hour += this . year * Season . LAST * 30 * 24 ; return hour ; } public ToMinutes ( ) { let minute = this . minute ; minute += this . hour * 60 ; minute += this . day * 24 * 60 ; minute += this . season * 30 * 24 * 60 ; minute += this . year * Season . LAST * 30 * 24 * 60 ; return minute ; } }