@@ -338,7 +338,50 @@ static void do_adjust_special_early(timelib_time* time)
338338 timelib_do_normalize (time );
339339}
340340
341- static void do_adjust_timezone (timelib_time * tz , timelib_tzinfo * tzi )
341+ static bool timelib_adjust_timezone_for_overlap (timelib_time * tz , timelib_tzinfo * tzi , int32_t preferred_offset )
342+ {
343+ timelib_sll earlier_sse = INT64_MAX ;
344+ timelib_sll second_earliest_sse = INT64_MAX ;
345+ timelib_sll matching_offset_sse = INT64_MAX ;
346+ uint64_t type_count = tzi -> bit64 .typecnt ;
347+
348+ for (uint64_t i = 0 ; i < type_count ; i ++ ) {
349+ int32_t offset = tzi -> type [i ].offset ;
350+ int32_t actual_offset ;
351+
352+ if ((offset > 0 && tz -> sse < INT64_MIN + offset ) || (offset < 0 && tz -> sse > INT64_MAX + offset )) {
353+ continue ;
354+ }
355+
356+ timelib_sll candidate_sse = tz -> sse - offset ;
357+ if (!timelib_get_time_zone_offset_info (candidate_sse , tzi , & actual_offset , NULL , NULL ) || actual_offset != offset ) {
358+ continue ;
359+ }
360+
361+ if (candidate_sse < earlier_sse ) {
362+ second_earliest_sse = earlier_sse ;
363+ earlier_sse = candidate_sse ;
364+ } else if (candidate_sse > earlier_sse && candidate_sse < second_earliest_sse ) {
365+ second_earliest_sse = candidate_sse ;
366+ }
367+
368+ if (offset == preferred_offset && candidate_sse < matching_offset_sse ) {
369+ matching_offset_sse = candidate_sse ;
370+ }
371+ }
372+
373+ if (second_earliest_sse == INT64_MAX ) {
374+ return false;
375+ }
376+
377+ /* Keep the previous offset if it is valid for the new local time. */
378+ tz -> sse = matching_offset_sse != INT64_MAX ? matching_offset_sse : earlier_sse ;
379+ tz -> is_localtime = 1 ;
380+ timelib_set_timezone (tz , tzi );
381+ return true;
382+ }
383+
384+ static void do_adjust_timezone (timelib_time * tz , timelib_tzinfo * tzi , bool resolve_overlap , int32_t preferred_offset )
342385{
343386 switch (tz -> zone_type ) {
344387 case TIMELIB_ZONETYPE_OFFSET :
@@ -373,33 +416,31 @@ static void do_adjust_timezone(timelib_time *tz, timelib_tzinfo *tzi)
373416 if (!tzi ) {
374417 return ;
375418 }
419+ if (resolve_overlap && timelib_adjust_timezone_for_overlap (tz , tzi , preferred_offset )) {
420+ return ;
421+ }
376422
377423 timelib_get_time_zone_offset_info (tz -> sse , tzi , & current_offset , & current_transition_time , & current_is_dst );
378424 timelib_get_time_zone_offset_info (tz -> sse - current_offset , tzi , & after_offset , & after_transition_time , NULL );
379425 actual_offset = after_offset ;
380426 actual_transition_time = after_transition_time ;
381427 if (current_offset == after_offset && tz -> have_zone ) {
382- /* Make sure we're not missing a DST change because we don't know the actual offset yet */
383428 if (current_offset >= 0 && tz -> dst && !current_is_dst ) {
384- /* Timezone or its DST at or east of UTC, so the local time, interpreted as UTC, leaves DST (as defined in the actual timezone) before the actual local time */
385- int32_t earlier_offset ;
386- timelib_sll earlier_transition_time ;
387- timelib_get_time_zone_offset_info (tz -> sse - current_offset - 7200 , tzi , & earlier_offset , & earlier_transition_time , NULL );
388- if ((earlier_offset != after_offset ) && (tz -> sse - earlier_offset < after_transition_time )) {
389- /* Looking behind a bit clarified the actual offset to use */
390- actual_offset = earlier_offset ;
391- actual_transition_time = earlier_transition_time ;
392- }
429+ int32_t earlier_offset ;
430+ timelib_sll earlier_transition_time ;
431+ timelib_get_time_zone_offset_info (tz -> sse - current_offset - 7200 , tzi , & earlier_offset , & earlier_transition_time , NULL );
432+ if ((earlier_offset != after_offset ) && (tz -> sse - earlier_offset < after_transition_time )) {
433+ actual_offset = earlier_offset ;
434+ actual_transition_time = earlier_transition_time ;
435+ }
393436 } else if (current_offset <= 0 && current_is_dst && !tz -> dst ) {
394- /* Timezone west of UTC, so the local time, interpreted as UTC, leaves DST (as defined in the actual timezone) after the actual local time */
395- int32_t later_offset ;
396- timelib_sll later_transition_time ;
397- timelib_get_time_zone_offset_info (tz -> sse - current_offset + 7200 , tzi , & later_offset , & later_transition_time , NULL );
398- if ((later_offset != after_offset ) && (tz -> sse - later_offset >= later_transition_time )) {
399- /* Looking ahead a bit clarified the actual offset to use */
400- actual_offset = later_offset ;
401- actual_transition_time = later_transition_time ;
402- }
437+ int32_t later_offset ;
438+ timelib_sll later_transition_time ;
439+ timelib_get_time_zone_offset_info (tz -> sse - current_offset + 7200 , tzi , & later_offset , & later_transition_time , NULL );
440+ if ((later_offset != after_offset ) && (tz -> sse - later_offset >= later_transition_time )) {
441+ actual_offset = later_offset ;
442+ actual_transition_time = later_transition_time ;
443+ }
403444 }
404445 }
405446
@@ -439,8 +480,13 @@ timelib_sll timelib_epoch_days_from_time(timelib_time *time)
439480 return era * DAYS_PER_ERA + day_of_era - HINNANT_EPOCH_SHIFT ;
440481}
441482
442- void timelib_update_ts (timelib_time * time , timelib_tzinfo * tzi )
483+ static void timelib_update_ts_internal (timelib_time * time , timelib_tzinfo * tzi , bool resolve_overlap )
443484{
485+ int32_t preferred_offset = time -> z ;
486+ if (time -> sse_uptodate && time -> zone_type == TIMELIB_ZONETYPE_ID && time -> tz_info ) {
487+ timelib_get_time_zone_offset_info (time -> sse , time -> tz_info , & preferred_offset , NULL , NULL );
488+ }
489+
444490 do_adjust_special_early (time );
445491 do_adjust_relative (time );
446492 do_adjust_special (time );
@@ -456,12 +502,22 @@ void timelib_update_ts(timelib_time* time, timelib_tzinfo* tzi)
456502 time -> sse += timelib_epoch_days_from_time (time ) * (SECS_PER_DAY / 2 );
457503
458504 // This modifies time->sse, if needed
459- do_adjust_timezone (time , tzi );
505+ do_adjust_timezone (time , tzi , resolve_overlap , preferred_offset );
460506
461507 time -> sse_uptodate = 1 ;
462508 time -> have_relative = time -> relative .have_weekday_relative = time -> relative .have_special_relative = time -> relative .first_last_day_of = 0 ;
463509}
464510
511+ void timelib_update_ts (timelib_time * time , timelib_tzinfo * tzi )
512+ {
513+ timelib_update_ts_internal (time , tzi , false);
514+ }
515+
516+ void timelib_update_ts_first (timelib_time * time , timelib_tzinfo * tzi )
517+ {
518+ timelib_update_ts_internal (time , tzi , true);
519+ }
520+
465521#if 0
466522int main (void )
467523{
0 commit comments