[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[xen staging] time: shorten year determination loop



commit b244971a8b4d86a1c6ef3814b5c71c5ca74bebe1
Author:     Jan Beulich <jbeulich@xxxxxxxx>
AuthorDate: Wed Sep 2 15:12:14 2026 +0200
Commit:     Jan Beulich <jbeulich@xxxxxxxx>
CommitDate: Wed Sep 2 15:12:14 2026 +0200

    time: shorten year determination loop
    
    For dates very far into the future (the MC146818 RTC's century byte can go
    up to the 99th century), the present year-wise loop would become somewhat
    inefficient (taking perhaps several thousand iterations). Prefix that loop
    with a 400-year granular calculation (somewhat like the earlier loop does
    for dates in the past).
    
    Signed-off-by: Jan Beulich <jbeulich@xxxxxxxx>
    Acked-by: Roger Pau Monné <roger@xxxxxxxxxxxxxx>
---
 xen/common/time.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/xen/common/time.c b/xen/common/time.c
index 2b8c98e2de..0ddf65448d 100644
--- a/xen/common/time.c
+++ b/xen/common/time.c
@@ -29,6 +29,8 @@ bool __ro_after_init NOW_good;
 #define __isleap(year) \
   ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
 
+#define DAYS_IN_400_YEARS (365 * 303 + 366 * 97)
+
 /* How many days are in each month.  */
 static const unsigned short int __mon_lengths[2][12] = {
     /* Normal years.  */
@@ -59,7 +61,7 @@ struct tm gmtime(unsigned long t)
     while ( t & (1UL<<39) )
     {
         y -= 400;
-        t += ((unsigned long)(365 * 303 + 366 * 97)) * SECS_PER_DAY;
+        t += (unsigned long)DAYS_IN_400_YEARS * SECS_PER_DAY;
     }
     t &= (1UL << 40) - 1;
 #endif
@@ -73,6 +75,11 @@ struct tm gmtime(unsigned long t)
     tbuf.tm_sec = rem % 60;
     /* January 1, 1970 was a Thursday.  */
     tbuf.tm_wday = (4 + days) % 7;
+    if ( days >= DAYS_IN_400_YEARS )
+    {
+        y += (days / DAYS_IN_400_YEARS) * 400;
+        days %= DAYS_IN_400_YEARS;
+    }
     while ( days >= (rem = __isleap(y) ? 366 : 365) )
     {
         ++y;
--
generated by git-patchbot for /home/xen/git/xen.git#staging



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.