Skip to content

Commit

Permalink
$mol_time: fixed native in diffferent DST
Browse files Browse the repository at this point in the history
  • Loading branch information
jin committed Aug 30, 2024
1 parent 4a5bf58 commit 28e11ef
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
4 changes: 2 additions & 2 deletions date/date.view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ namespace $.$$ {

if( next ) return next

let moment = $mol_try( ()=> new $mol_time_moment( this.value_changed().replace( /\D+$/, '' ) ) )
if( moment instanceof Error || !moment.year ) return new $mol_time_moment
let moment = $mol_try( ()=> new $mol_time_moment( this.value_changed().replace( /\D+$/, '' ) ).mask( '0000-00' ) )
if( moment instanceof Error || !moment.year ) return new $mol_time_moment().mask( '0000-00' )

if( moment.month === undefined ) {
moment = moment.merge( { month: 0 } )
Expand Down
5 changes: 5 additions & 0 deletions time/moment/moment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ namespace $ {
)
} ,

'renormalization'() {
$mol_assert_equal( new $mol_time_moment( '2024-08' ).normal.toString(), '2024-08' )
$mol_assert_equal( new $mol_time_moment( '2024-11' ).normal.toString(), '2024-11' )
} ,

'iso week day'() {
$mol_assert_equal( new $mol_time_moment( '2017-09-17' ).weekday , $mol_time_moment_weekdays.sunday )
$mol_assert_equal( new $mol_time_moment( '2017-09-18' ).weekday , $mol_time_moment_weekdays.monday )
Expand Down
40 changes: 25 additions & 15 deletions time/moment/moment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,29 @@ namespace $ {

_native : Date | undefined
get native() {

if( this._native ) return this._native

const utc = this.toOffset( 'Z' )

return this._native = new Date( Date.UTC(
utc.year ?? 0 ,
utc.month ?? 0 ,
( utc.day ?? 0 ) + 1 ,
utc.hour ?? 0 ,
utc.minute ?? 0 ,
utc.second != undefined ? Math.floor( utc.second ) : 0 ,
utc.second != undefined ? Math.floor( ( utc.second - Math.floor( utc.second ) ) * 1000 ) : 0 ,
) )
const second = Math.floor( this.second ?? 0 )

const native = new Date(
this.year ?? 0 ,
this.month ?? 0 ,
( this.day ?? 0 ) + 1 ,
this.hour ?? 0 ,
this.minute ?? 0 ,
second,
Math.floor( ( ( this.second ?? 0 ) - second ) * 1000 ),
)

const offset = native.getTimezoneOffset()
shift: if( this.offset ) {
const target = this.offset.count( 'PT1m' )
if( target === offset ) break shift
native.setMinutes( native.getMinutes() - offset + target )
}

return this._native = native
}

_normal : $mol_time_moment | undefined
Expand Down Expand Up @@ -150,9 +160,9 @@ namespace $ {
shift( config : $mol_time_duration_config ) {
const duration = new $mol_time_duration( config )
const moment = new $mol_time_moment().merge({
year: this.year,
month: this.month,
day: this.day,
year: this.year ?? 0,
month: this.month ?? 0,
day: this.day ?? 0,
hour: this.hour ?? 0,
minute: this.minute ?? 0,
second: this.second ?? 0,
Expand Down Expand Up @@ -204,7 +214,7 @@ namespace $ {
const duration = new $mol_time_duration( config )
const offset = this.offset || new $mol_time_moment().offset!

let with_time = new $mol_time_moment( 'T00:00:00' ).merge( this )
let with_time = new $mol_time_moment( '0001-01-01T00:00:00' ).merge( this )
const moment = with_time.shift( duration.summ( offset.mult( -1 ) ) )

return moment.merge({ offset : duration })
Expand Down

0 comments on commit 28e11ef

Please sign in to comment.