import { Constants } from './Constants' ; import { Parse } from './Parse' ; import { TimeFormat } from './TimeFormat' ; see export type TimeInput = Time | number | string | { hour : number ; minute ? : number ; second ? : number ; millisecond ? : number ; } ; /**
 * A class which holds a specific time during in any day.
 */ export class Time { /**
     * The regular expression used to parse a time from a string.
     *
     * - ## = hour
     * - ##:## = hour & minute
     * - ##:##:## = hour, minute, & second
     * - ##:##:##.### = hour, minute, second, and milliseconds
     */ public static REGEX = /^(\d\d?):?(\d\d)?:?(\d\d)?\.?(\d\d\d)?$/ ; /**
     * The hour between 0 and 23
     */ public hour : number ; /**
     * The minute between 0 and 59
     */ public minute : number ; /**
     * The second between 0 and 59
     */ public second : number ; /**
     * The millisecond between 0 and 999
     */ public millisecond : number ; param hour param minute param second param millisecond public constructor ( hour , minute = Constants . MINUTE_MIN , second = Constants . SECOND_MIN , millisecond = Constants . MILLIS_MIN ) { this . hour = hour ; this . minute = minute ; this . second = second ; this . millisecond = millisecond ; } param format returns see public format ( format ) { return TimeFormat . format ( format , this ) ; } param time returns public matches ( time ) { return this . hour === time . hour && this . minute === time . minute && this . second === time . second && this . millisecond === time . millisecond ; } param time returns public matchesHour ( time ) { return this . hour === time . hour ; } param time returns public matchesMinute ( time ) { return this . hour === time . hour && this . minute === time . minute ; } param time returns public matchesSecond ( time ) { return this . hour === time . hour && this . minute === time . minute && this . second === time . second ; } param input returns public set ( input ) { const parsed = Time . parse ( input ) ; const valid = ! ! parsed ; if ( valid ) { this . hour = parsed . hour ; this . minute = parsed . minute ; this . second = parsed . second ; this . millisecond = parsed . millisecond ; } return valid ; } returns public toMilliseconds ( ) { return this . hour * Constants . MILLIS_IN_HOUR + this . minute * Constants . MILLIS_IN_MINUTE + this . second * Constants . MILLIS_IN_SECOND + this . millisecond ; } returns public toString ( ) { if ( this . millisecond ) return this . format ( 'HH:mm:ss.SSS' ) ; if ( this . second ) return this . format ( 'HH:mm:ss' ) ; if ( this . minute ) return this . format ( 'HH:mm' ) ; return this . format ( 'HH' ) ; } returns public toIdentifier ( ) { return this . hour + this . minute * 100 + this . second * 10000 + this . millisecond * 10000000 ; } returns public toObject ( ) { const out = { hour : this . hour } ; if ( this . minute ) out . minute = this . minute ; if ( this . second ) out . second = this . second ; if ( this . millisecond ) out . millisecond = this . millisecond ; return out ; } param input returns see public static parse ( input ) { return Parse . time ( input ) ; } param time returns see public static fromString ( time ) { const matches = this . REGEX . exec ( time ) ; if ( ! matches ) { return null ; } const h = parseInt ( matches [ 1 ] ) || 0 ; const m = parseInt ( matches [ 2 ] ) || 0 ; const s = parseInt ( matches [ 3 ] ) || 0 ; const l = parseInt ( matches [ 4 ] ) || 0 ; return this . build ( h , m , s , l ) ; } param time returns public static fromIdentifier ( time ) { const h = time % 100 ; const m = Math . floor ( time / 100 ) % 100 ; const s = Math . floor ( time / 10000 ) % 100 ; const l = Math . floor ( time / 10000000 ) % 1000 ; return this . build ( h , m , s , l ) ; } param hour param minute param second param millisecond returns public static build ( hour , minute = Constants . MINUTE_MIN , second = Constants . SECOND_MIN , millisecond = Constants . MILLIS_MIN ) { return new Time ( hour , minute , second , millisecond ) ; } }