import { DateTime } from "luxon" ; import { validDateTime } from "./DateTime" ; import { first , firstDefinedThunk , map , Maybe } from "./Maybe" ; import { blank , pad2 , toS } from "./String" ; /**
 * Encodes an ExifDate
 */ export class ExifDate { static fromISO ( text ) { return this . fromDateTime ( DateTime . fromISO ( text ) , text ) ; } private static fromPatterns ( text , fmts ) { if ( blank ( text ) ) return ; text = toS ( text ) . trim ( ) ; return first ( fmts , ( fmt ) => map ( DateTime . fromFormat ( text , fmt ) , ( dt ) => this . fromDateTime ( dt , text ) ) ) ; } static fromExifStrict ( text ) { return this . fromPatterns ( text , [ "y:MM:dd" , "y-MM-dd" , "y:M:d" ] ) ; } static fromExifLoose ( text ) { return this . fromPatterns ( text , [ "MMM d y" , "MMMM d y" ] ) ; } static fromEXIF ( text ) { return firstDefinedThunk ( [ ( ) => this . fromExifStrict ( text ) , ( ) => this . fromExifLoose ( text ) , ] ) ; } static fromDateTime ( dt , rawValue ? ) { return validDateTime ( dt ) ? new ExifDate ( dt . year , dt . month , dt . day , rawValue ) : undefined ; } constructor ( readonly year , readonly month , readonly day , readonly rawValue ? ) { } toDate ( ) { return new Date ( this . year , this . month - 1 , this . day ) ; } toISOString ( ) { return this . toString ( "-" ) ; } toExifString ( ) { return this . toString ( ":" ) ; } toString ( sep = "-" ) { return `${ this . year }${ sep }${ pad2 ( this . month , this . day ) . join ( sep ) }` ; } toJSON ( ) { return { _ctor : "ExifDate" , year : this . year , month : this . month , day : this . day , rawValue : this . rawValue , } ; } static fromJSON ( json ) { return new ExifDate ( json . year , json . month , json . day , json . rawValue ) ; } }