import toDate from '../toDate/index' ; import isValid from '../isValid/index' ; import addLeadingZeros from '../_lib/addLeadingZeros/index' ; import toInteger from '../_lib/toInteger/index' ; export interface FormatRFC3339Options { fractionDigits : 0 | 1 | 2 | 3 ; } name category summary description param { Date | Number } date param options param { 0 | 1 | 2 | 3 } options . fractionDigits returns { String } throws throws throws example example example export default function formatRFC3339 ( dirtyDate , dirtyOptions ? ) { if ( arguments . length < 1 ) { throw new TypeError ( `1 arguments required, but only ${ arguments . length } present` ) ; } const originalDate = toDate ( dirtyDate ) ; if ( ! isValid ( originalDate ) ) { throw new RangeError ( 'Invalid time value' ) ; } const { fractionDigits = 0 , } = dirtyOptions || { } ; if ( ! ( fractionDigits >= 0 && fractionDigits <= 3 ) ) { throw new RangeError ( 'fractionDigits must be between 0 and 3 inclusively' ) ; } const day = addLeadingZeros ( originalDate . getDate ( ) , 2 ) ; const month = addLeadingZeros ( originalDate . getMonth ( ) + 1 , 2 ) ; const year = originalDate . getFullYear ( ) ; const hour = addLeadingZeros ( originalDate . getHours ( ) , 2 ) ; const minute = addLeadingZeros ( originalDate . getMinutes ( ) , 2 ) ; const second = addLeadingZeros ( originalDate . getSeconds ( ) , 2 ) ; let fractionalSecond = '' ; if ( fractionDigits > 0 ) { const milliseconds = originalDate . getMilliseconds ( ) ; const fractionalSeconds = Math . floor ( milliseconds * Math . pow ( 10 , fractionDigits - 3 ) ) ; fractionalSecond = '.' + addLeadingZeros ( fractionalSeconds , fractionDigits ) ; } let offset = '' ; const tzOffset = originalDate . getTimezoneOffset ( ) ; if ( tzOffset !== 0 ) { const absoluteOffset = Math . abs ( tzOffset ) ; const hourOffset = addLeadingZeros ( toInteger ( absoluteOffset / 60 ) , 2 ) ; const minuteOffset = addLeadingZeros ( absoluteOffset % 60 , 2 ) ; const sign = tzOffset < 0 ? '+' : '-' ; offset = `${ sign }${ hourOffset }:${ minuteOffset }` ; } else { offset = 'Z' ; } return `${ year }-${ month }-${ day }T${ hour }:${ minute }:${ second }${ fractionalSecond }${ offset }` ; }