This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
time64.c: Refactor Perl_localtime64_r()
authorKarl Williamson <khw@cpan.org>
Sat, 14 Mar 2020 20:33:05 +0000 (14:33 -0600)
committerKarl Williamson <khw@cpan.org>
Thu, 19 Mar 2020 00:08:57 +0000 (18:08 -0600)
There are two main cases in Perl_localtime64_r(), call them A and B.

Prior to this commit, it was structured

if (A) {
 codeA
 return;
}

codeB
return;

The problem this commit solves is that there is common stuff in codeA
and codeB that will now only be specified once.  Currently that common
stuff is minimal, and so acceptable, but the next few commits will make
the common stuff more complex, so making it be only once is helpful.

time64.c

index 475134f..5e36e27 100644 (file)
--- a/time64.c
+++ b/time64.c
@@ -476,15 +476,8 @@ struct TM *Perl_localtime64_r (const Time64_T *time, struct TM *local_tm)
         safe_time = (time_t)*time;
 
         TIME64_TRACE1("Using system localtime for %lld\n", *time);
-
-        LOCALTIME_R(&safe_time, &safe_date);
-
-        S_copy_little_tm_to_big_TM(&safe_date, local_tm);
-        assert(S_check_tm(local_tm));
-
-        return local_tm;
     }
-
+    else {
     if( Perl_gmtime64_r(time, &gm_tm) == NULL ) {
         TIME64_TRACE1("gmtime64_r returned null for %lld\n", *time);
         return NULL;
@@ -501,6 +494,8 @@ struct TM *Perl_localtime64_r (const Time64_T *time, struct TM *local_tm)
     }
 
     safe_time = (time_t)S_timegm64(&gm_tm);
+    }
+
     if( LOCALTIME_R(&safe_time, &safe_date) == NULL ) {
         TIME64_TRACE1("localtime_r(%d) returned NULL\n", (int)safe_time);
         return NULL;
@@ -508,6 +503,8 @@ struct TM *Perl_localtime64_r (const Time64_T *time, struct TM *local_tm)
 
     S_copy_little_tm_to_big_TM(&safe_date, local_tm);
 
+    if (! use_system) {
+
     local_tm->tm_year = orig_year;
     if( local_tm->tm_year != orig_year ) {
         TIME64_TRACE2("tm_year overflow: tm_year %lld, orig_year %lld\n",
@@ -545,6 +542,8 @@ struct TM *Perl_localtime64_r (const Time64_T *time, struct TM *local_tm)
     if( !IS_LEAP(local_tm->tm_year) && local_tm->tm_yday == 365 )
         local_tm->tm_yday--;
 
+    }
+
     assert(S_check_tm(local_tm));
 
     return local_tm;