Skip to contentSkip to conversation

Support dates outside of 1901..2038 range

PATCH Superseded Andrew Hamilton 3 participants 10 messages Apr 18, 2025 – Aug 25, 2025

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover Support dates outside of 1901..2038 range

Support dates outside of 1901..2038.
Add tests for dates outside this range.
Vast majority of the work was done by Vladimir Serbinenko

V2 -> V3: Added additional commentary to datetime.c to describe
the origin of some of the numbers used in calculations.

Andrew Hamilton (2):
datetime: Support dates outside of 1901..2038 range
date_unit_test: test dates outside of 32-bit unix range

 grub-core/lib/datetime.c | 48 ++++++++++++++++++++++++++++++++--------
 include/grub/datetime.h  | 15 ++++++-------
 tests/date_unit_test.c   | 35 +++++++++++++++++++++--------
 3 files changed, 72 insertions(+), 26 deletions(-)
Signature · 7 lines
2.39.5


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#1 lore raw

PATCH v3 1/2 datetime: Support dates outside of 1901..2038 range

Support dates outside of 1901..2038.

---
 grub-core/lib/datetime.c | 48 ++++++++++++++++++++++++++++++++--------
 include/grub/datetime.h  | 15 ++++++-------
 2 files changed, 46 insertions(+), 17 deletions(-)
grub-core/lib/datetime.c +39 -9
index 8f0922fb0..4e68eabc6 100644
@@ -64,6 +64,10 @@ grub_get_weekday_name (struct grub_datetime *datetime)
 #define SECPERDAY (24*SECPERHOUR)
 #define DAYSPERYEAR 365
 #define DAYSPER4YEARS (4*DAYSPERYEAR+1)
+/* 24 leap years in 100 years */
+#define DAYSPER100YEARS (100*DAYSPERYEAR+24)
+/* 97 leap years in 400 years */
+#define DAYSPER400YEARS (400*DAYSPERYEAR+97)
 
 
 void
@@ -76,7 +80,7 @@ grub_unixtime2datetime (grub_int64_t nix, struct grub_datetime *datetime)
   /* Convenience: let's have 3 consecutive non-bissextile years
      at the beginning of the counting date. So count from 1901. */
   int days_epoch;
-  /* Number of days since 1st Januar, 1901.  */
+  /* Number of days since 1st January, 1 (proleptic).  */
   unsigned days;
   /* Seconds into current day.  */
   unsigned secs_in_day;
@@ -92,12 +96,39 @@ grub_unixtime2datetime (grub_int64_t nix, struct grub_datetime *datetime)
     days_epoch = grub_divmod64 (nix, SECPERDAY, NULL);
 
   secs_in_day = nix - days_epoch * SECPERDAY;
-  days = days_epoch + 69 * DAYSPERYEAR + 17;
+  /* 1970 is Unix Epoch. Adjust to a year 1 epoch:
+     Leap year logic:
+      - Years evenly divisible by 400 are leap years
+      - Otherwise, if divisible by 100 are not leap years
+      - Otherwise, if divisible by 4 are leap years
+     There are four 400-year periods (1600 years worth of days with leap days)
+     There are three 100-year periods worth of leap days (3*24)
+     There are 369 years in addition to the four 400 year periods
+     There are 17 leap days in 69 years (beyond the three 100 year periods) */
+  days = days_epoch + 369 * DAYSPERYEAR + 17 + 24 * 3 + 4 * DAYSPER400YEARS;
 
-  datetime->year = 1901 + 4 * (days / DAYSPER4YEARS);
+  datetime->year = 1 + 400 * (days / DAYSPER400YEARS);
+  days %= DAYSPER400YEARS;
+
+  /* On 31st December of bissextile (leap) years 365 days from the beginning
+     of the year elapsed but year isn't finished yet - every 400 years
+     396 is 4 years less than 400 year leap cycle
+     96 is 1 day less than number of leap days in 400 years */
+  if (days / DAYSPER100YEARS == 4)
+    {
+      datetime->year += 396;
+      days -= 396*DAYSPERYEAR + 96;
+    }
+  else
+    {
+      datetime->year += 100 * (days / DAYSPER100YEARS);
+      days %= DAYSPER100YEARS;
+    }
+
+  datetime->year += 4 * (days / DAYSPER4YEARS);
   days %= DAYSPER4YEARS;
-  /* On 31st December of bissextile years 365 days from the beginning
-     of the year elapsed but year isn't finished yet */
+  /* On 31st December of bissextile (leap) years 365 days from the beginning
+     of the year elapsed but year isn't finished yet - every 4 years */
   if (days / DAYSPERYEAR == 4)
     {
       datetime->year += 3;
@@ -108,11 +139,10 @@ grub_unixtime2datetime (grub_int64_t nix, struct grub_datetime *datetime)
       datetime->year += days / DAYSPERYEAR;
       days %= DAYSPERYEAR;
     }
+  int isbisextile = datetime->year % 4 == 0 && (datetime->year % 100 != 0 || datetime->year % 400 == 0);
   for (i = 0; i < 12
-	 && days >= (i==1 && datetime->year % 4 == 0
-		      ? 29 : months[i]); i++)
-    days -= (i==1 && datetime->year % 4 == 0
-			    ? 29 : months[i]);
+        && days >= (i==1 && isbisextile ? 29 : months[i]); i++)
+    days -= (i==1 && isbisextile ? 29 : months[i]);
   datetime->month = i + 1;
   datetime->day = 1 + days;
   datetime->hour = (secs_in_day / SECPERHOUR);
include/grub/datetime.h +7 -8
index bcec636f0..9289b0d00 100644
@@ -54,7 +54,7 @@ void grub_unixtime2datetime (grub_int64_t nix,
 static inline int
 grub_datetime2unixtime (const struct grub_datetime *datetime, grub_int64_t *nix)
 {
-  grub_int32_t ret;
+  grub_int64_t ret;
   int y4, ay;
   const grub_uint16_t monthssum[12]
     = { 0,
@@ -75,15 +75,11 @@ grub_datetime2unixtime (const struct grub_datetime *datetime, grub_int64_t *nix)
   const int SECPERHOUR = 60 * SECPERMIN;
   const int SECPERDAY = 24 * SECPERHOUR;
   const int SECPERYEAR = 365 * SECPERDAY;
-  const int SECPER4YEARS = 4 * SECPERYEAR + SECPERDAY;
+  const grub_int64_t SECPER4YEARS = 4 * SECPERYEAR + SECPERDAY;
 
-  if (datetime->year > 2038 || datetime->year < 1901)
-    return 0;
   if (datetime->month > 12 || datetime->month < 1)
     return 0;
 
-  /* In the period of validity of unixtime all years divisible by 4
-     are bissextile*/
   /* Convenience: let's have 3 consecutive non-bissextile years
      at the beginning of the epoch. So count from 1973 instead of 1970 */
   ret = 3 * SECPERYEAR + SECPERDAY;
@@ -94,13 +90,16 @@ grub_datetime2unixtime (const struct grub_datetime *datetime, grub_int64_t *nix)
   ret += y4 * SECPER4YEARS;
   ret += ay * SECPERYEAR;
 
+  ret -= ((datetime->year - 1) / 100 - (datetime->year - 1) / 400 - 15) * SECPERDAY;
+
   ret += monthssum[datetime->month - 1] * SECPERDAY;
-  if (ay == 3 && datetime->month >= 3)
+  int isbisextile = ay == 3 && (datetime->year % 100 != 0 || datetime->year % 400 == 0);
+  if (isbisextile && datetime->month >= 3)
     ret += SECPERDAY;
 
   ret += (datetime->day - 1) * SECPERDAY;
   if ((datetime->day > months[datetime->month - 1]
-       && (!ay || datetime->month != 2 || datetime->day != 29))
+       && !(isbisextile && datetime->month == 2 && datetime->day == 29))
       || datetime->day < 1)
     return 0;
 
Signature · 7 lines
2.39.5


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#2 lore raw

Re: [PATCH v3 1/2] datetime: Support dates outside of 1901..2038 range

Quoted 40 lines · Andrew Hamilton

Support dates outside of 1901..2038.

---
 grub-core/lib/datetime.c | 48 ++++++++++++++++++++++++++++++++--------
 include/grub/datetime.h  | 15 ++++++-------
 2 files changed, 46 insertions(+), 17 deletions(-)
grub-core/lib/datetime.c +5 -1
index 8f0922fb0..4e68eabc6 100644
@@ -64,6 +64,10 @@ grub_get_weekday_name (struct grub_datetime *datetime)
 #define SECPERDAY (24*SECPERHOUR)
 #define DAYSPERYEAR 365
 #define DAYSPER4YEARS (4*DAYSPERYEAR+1)
+/* 24 leap years in 100 years */
+#define DAYSPER100YEARS (100*DAYSPERYEAR+24)
+/* 97 leap years in 400 years */
+#define DAYSPER400YEARS (400*DAYSPERYEAR+97)
 
 
 void
@@ -76,7 +80,7 @@ grub_unixtime2datetime (grub_int64_t nix, struct grub_datetime *datetime)
   /* Convenience: let's have 3 consecutive non-bissextile years
      at the beginning of the counting date. So count from 1901. */
   int days_epoch;
-  /* Number of days since 1st Januar, 1901.  */
+  /* Number of days since 1st January, 1 (proleptic).  */
   unsigned days;
   /* Seconds into current day.  */
   unsigned secs_in_day;
@@ -92,12 +96,39 @@ grub_unixtime2datetime (grub_int64_t nix, struct grub_datetime *datetime)
     days_epoch = grub_divmod64 (nix, SECPERDAY, NULL);
 
   secs_in_day = nix - days_epoch * SECPERDAY;

grub-core/lib/datetime.c grub_unixtime2datetime (grub_int64_t nix, struct grub_datetime *datetime) · line 107

Quoted 10 lines
grub-core/lib/datetime.c +9 -1
-  days = days_epoch + 69 * DAYSPERYEAR + 17;
+  /* 1970 is Unix Epoch. Adjust to a year 1 epoch:
+     Leap year logic:
+      - Years evenly divisible by 400 are leap years
+      - Otherwise, if divisible by 100 are not leap years
+      - Otherwise, if divisible by 4 are leap years
+     There are four 400-year periods (1600 years worth of days with leap days)
+     There are three 100-year periods worth of leap days (3*24)
+     There are 369 years in addition to the four 400 year periods
+     There are 17 leap days in 69 years (beyond the three 100 year periods) */

May I ask you to stick to comments coding style [1]?

grub-core/lib/datetime.c grub_unixtime2datetime (grub_int64_t nix, struct grub_datetime *datetime) · line 116

Quoted 10 lines
grub-core/lib/datetime.c +8 -1
+  days = days_epoch + 369 * DAYSPERYEAR + 17 + 24 * 3 + 4 * DAYSPER400YEARS;
 
-  datetime->year = 1901 + 4 * (days / DAYSPER4YEARS);
+  datetime->year = 1 + 400 * (days / DAYSPER400YEARS);
+  days %= DAYSPER400YEARS;
+
+  /* On 31st December of bissextile (leap) years 365 days from the beginning
+     of the year elapsed but year isn't finished yet - every 400 years
+     396 is 4 years less than 400 year leap cycle
+     96 is 1 day less than number of leap days in 400 years */

Ditto... Here and below please...

Quoted 21 lines
grub-core/lib/datetime.c +14 -2
+  if (days / DAYSPER100YEARS == 4)
+    {
+      datetime->year += 396;
+      days -= 396*DAYSPERYEAR + 96;
+    }
+  else
+    {
+      datetime->year += 100 * (days / DAYSPER100YEARS);
+      days %= DAYSPER100YEARS;
+    }
+
+  datetime->year += 4 * (days / DAYSPER4YEARS);
   days %= DAYSPER4YEARS;
-  /* On 31st December of bissextile years 365 days from the beginning
-     of the year elapsed but year isn't finished yet */
+  /* On 31st December of bissextile (leap) years 365 days from the beginning
+     of the year elapsed but year isn't finished yet - every 4 years */
   if (days / DAYSPERYEAR == 4)
     {
       datetime->year += 3;

grub-core/lib/datetime.c grub_unixtime2datetime (grub_int64_t nix, struct grub_datetime *datetime) · line 142

Quoted 4 lines
grub-core/lib/datetime.c +1 -0
@@ -108,11 +139,10 @@ grub_unixtime2datetime (grub_int64_t nix, struct grub_datetime *datetime)
       datetime->year += days / DAYSPERYEAR;
       days %= DAYSPERYEAR;
     }
+  int isbisextile = datetime->year % 4 == 0 && (datetime->year % 100 != 0 || datetime->year % 400 == 0);

Please do not define variables in random places. I prefer if it is done at the beginning of the function or block.

And I think this should be a bool set with ternary operator...

... and s/isbisextile/is_bisextile/

grub-core/lib/datetime.c grub_unixtime2datetime (grub_int64_t nix, struct grub_datetime *datetime) · line 145

Quoted 7 lines
grub-core/lib/datetime.c +2 -4
   for (i = 0; i < 12
-	 && days >= (i==1 && datetime->year % 4 == 0
-		      ? 29 : months[i]); i++)
-    days -= (i==1 && datetime->year % 4 == 0
-			    ? 29 : months[i]);
+        && days >= (i==1 && isbisextile ? 29 : months[i]); i++)
+    days -= (i==1 && isbisextile ? 29 : months[i]);

If you touch the code then please fix its coding style, e.g.:

  days -= (i == 1 && is_bisextile == true) ? 29 : months[i]

On the occasion it seems more readable...

Quoted 34 lines
grub-core/lib/datetime.c +0 -0
   datetime->month = i + 1;
   datetime->day = 1 + days;
   datetime->hour = (secs_in_day / SECPERHOUR);
include/grub/datetime.h +2 -6
index bcec636f0..9289b0d00 100644
@@ -54,7 +54,7 @@ void grub_unixtime2datetime (grub_int64_t nix,
 static inline int
 grub_datetime2unixtime (const struct grub_datetime *datetime, grub_int64_t *nix)
 {
-  grub_int32_t ret;
+  grub_int64_t ret;
   int y4, ay;
   const grub_uint16_t monthssum[12]
     = { 0,
@@ -75,15 +75,11 @@ grub_datetime2unixtime (const struct grub_datetime *datetime, grub_int64_t *nix)
   const int SECPERHOUR = 60 * SECPERMIN;
   const int SECPERDAY = 24 * SECPERHOUR;
   const int SECPERYEAR = 365 * SECPERDAY;
-  const int SECPER4YEARS = 4 * SECPERYEAR + SECPERDAY;
+  const grub_int64_t SECPER4YEARS = 4 * SECPERYEAR + SECPERDAY;
 
-  if (datetime->year > 2038 || datetime->year < 1901)
-    return 0;
   if (datetime->month > 12 || datetime->month < 1)
     return 0;
 
-  /* In the period of validity of unixtime all years divisible by 4
-     are bissextile*/
   /* Convenience: let's have 3 consecutive non-bissextile years
      at the beginning of the epoch. So count from 1973 instead of 1970 */
   ret = 3 * SECPERYEAR + SECPERDAY;

include/grub/datetime.h grub_datetime2unixtime (const struct grub_datetime *datetime, grub_int64_t *nix) · line 93

Quoted 4 lines
include/grub/datetime.h +1 -0
@@ -94,13 +90,16 @@ grub_datetime2unixtime (const struct grub_datetime *datetime, grub_int64_t *nix)
   ret += y4 * SECPER4YEARS;
   ret += ay * SECPERYEAR;
 
+  ret -= ((datetime->year - 1) / 100 - (datetime->year - 1) / 400 - 15) * SECPERDAY;

What does this line do? And where 15 comes from?

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#3 lore raw

Re: [PATCH v3 1/2] datetime: Support dates outside of 1901..2038 range

Quoted 65 lines · Andrew Hamilton
Quoted 40 lines

Support dates outside of 1901..2038.

---
 grub-core/lib/datetime.c | 48 ++++++++++++++++++++++++++++++++--------
 include/grub/datetime.h  | 15 ++++++-------
 2 files changed, 46 insertions(+), 17 deletions(-)
grub-core/lib/datetime.c +5 -1
index 8f0922fb0..4e68eabc6 100644
@@ -64,6 +64,10 @@ grub_get_weekday_name (struct grub_datetime *datetime)
 #define SECPERDAY (24*SECPERHOUR)
 #define DAYSPERYEAR 365
 #define DAYSPER4YEARS (4*DAYSPERYEAR+1)
+/* 24 leap years in 100 years */
+#define DAYSPER100YEARS (100*DAYSPERYEAR+24)
+/* 97 leap years in 400 years */
+#define DAYSPER400YEARS (400*DAYSPERYEAR+97)
 
 
 void
@@ -76,7 +80,7 @@ grub_unixtime2datetime (grub_int64_t nix, struct grub_datetime *datetime)
   /* Convenience: let's have 3 consecutive non-bissextile years
      at the beginning of the counting date. So count from 1901. */
   int days_epoch;
-  /* Number of days since 1st Januar, 1901.  */
+  /* Number of days since 1st January, 1 (proleptic).  */
   unsigned days;
   /* Seconds into current day.  */
   unsigned secs_in_day;
@@ -92,12 +96,39 @@ grub_unixtime2datetime (grub_int64_t nix, struct grub_datetime *datetime)
     days_epoch = grub_divmod64 (nix, SECPERDAY, NULL);
 
   secs_in_day = nix - days_epoch * SECPERDAY;

grub-core/lib/datetime.c grub_unixtime2datetime (grub_int64_t nix, struct grub_datetime *datetime) · line 107

Quoted 10 lines
grub-core/lib/datetime.c +9 -1
-  days = days_epoch + 69 * DAYSPERYEAR + 17;
+  /* 1970 is Unix Epoch. Adjust to a year 1 epoch:
+     Leap year logic:
+      - Years evenly divisible by 400 are leap years
+      - Otherwise, if divisible by 100 are not leap years
+      - Otherwise, if divisible by 4 are leap years
+     There are four 400-year periods (1600 years worth of days with leap days)
+     There are three 100-year periods worth of leap days (3*24)
+     There are 369 years in addition to the four 400 year periods
+     There are 17 leap days in 69 years (beyond the three 100 year periods) */

May I ask you to stick to comments coding style [1]?

grub-core/lib/datetime.c grub_unixtime2datetime (grub_int64_t nix, struct grub_datetime *datetime) · line 116

Quoted 10 lines
grub-core/lib/datetime.c +8 -1
+  days = days_epoch + 369 * DAYSPERYEAR + 17 + 24 * 3 + 4 * DAYSPER400YEARS;
 
-  datetime->year = 1901 + 4 * (days / DAYSPER4YEARS);
+  datetime->year = 1 + 400 * (days / DAYSPER400YEARS);
+  days %= DAYSPER400YEARS;
+
+  /* On 31st December of bissextile (leap) years 365 days from the beginning
+     of the year elapsed but year isn't finished yet - every 400 years
+     396 is 4 years less than 400 year leap cycle
+     96 is 1 day less than number of leap days in 400 years */
Quoted 1 line

Ditto... Here and below please...

Will do, I will fix the comment formatting in the next version.

Quoted 18 lines
Quoted 10 lines
grub-core/lib/datetime.c +3 -2
+  datetime->year += 4 * (days / DAYSPER4YEARS);
   days %= DAYSPER4YEARS;
-  /* On 31st December of bissextile years 365 days from the beginning
-     of the year elapsed but year isn't finished yet */
+  /* On 31st December of bissextile (leap) years 365 days from the beginning
+     of the year elapsed but year isn't finished yet - every 4 years */
   if (days / DAYSPERYEAR == 4)
     {
       datetime->year += 3;

grub-core/lib/datetime.c grub_unixtime2datetime (grub_int64_t nix, struct grub_datetime *datetime) · line 142

Quoted 4 lines
grub-core/lib/datetime.c +1 -0
@@ -108,11 +139,10 @@ grub_unixtime2datetime (grub_int64_t nix, struct grub_datetime *datetime)
       datetime->year += days / DAYSPERYEAR;
       days %= DAYSPERYEAR;
     }
+  int isbisextile = datetime->year % 4 == 0 && (datetime->year % 100 != 0 || datetime->year % 400 == 0);

Please do not define variables in random places. I prefer if it is done at the beginning of the function or block.

Quoted 4 lines

And I think this should be a bool set with ternary operator...

... and s/isbisextile/is_bisextile/

I will correct the variable declaration location and change to bool / changing naming as well in the next version.

Quoted 8 lines

grub-core/lib/datetime.c grub_unixtime2datetime (grub_int64_t nix, struct grub_datetime *datetime) · line 145

Quoted 7 lines
grub-core/lib/datetime.c +2 -4
   for (i = 0; i < 12
-      && days >= (i==1 && datetime->year % 4 == 0
-                   ? 29 : months[i]); i++)
-    days -= (i==1 && datetime->year % 4 == 0
-                         ? 29 : months[i]);
+        && days >= (i==1 && isbisextile ? 29 : months[i]); i++)
+    days -= (i==1 && isbisextile ? 29 : months[i]);
Quoted 5 lines

If you touch the code then please fix its coding style, e.g.:

  days -= (i == 1 && is_bisextile == true) ? 29 : months[i]

On the occasion it seems more readable...

I will correct this in the next version.

Quoted 38 lines
Quoted 38 lines
grub-core/lib/datetime.c +0 -0
   datetime->month = i + 1;
   datetime->day = 1 + days;
   datetime->hour = (secs_in_day / SECPERHOUR);
include/grub/datetime.h +2 -6
index bcec636f0..9289b0d00 100644
@@ -54,7 +54,7 @@ void grub_unixtime2datetime (grub_int64_t nix,
 static inline int
 grub_datetime2unixtime (const struct grub_datetime *datetime, grub_int64_t *nix)
 {
-  grub_int32_t ret;
+  grub_int64_t ret;
   int y4, ay;
   const grub_uint16_t monthssum[12]
     = { 0,
@@ -75,15 +75,11 @@ grub_datetime2unixtime (const struct grub_datetime *datetime, grub_int64_t *nix)
   const int SECPERHOUR = 60 * SECPERMIN;
   const int SECPERDAY = 24 * SECPERHOUR;
   const int SECPERYEAR = 365 * SECPERDAY;
-  const int SECPER4YEARS = 4 * SECPERYEAR + SECPERDAY;
+  const grub_int64_t SECPER4YEARS = 4 * SECPERYEAR + SECPERDAY;
 
-  if (datetime->year > 2038 || datetime->year < 1901)
-    return 0;
   if (datetime->month > 12 || datetime->month < 1)
     return 0;
 
-  /* In the period of validity of unixtime all years divisible by 4
-     are bissextile*/
   /* Convenience: let's have 3 consecutive non-bissextile years
      at the beginning of the epoch. So count from 1973 instead of 1970 */
   ret = 3 * SECPERYEAR + SECPERDAY;
@@ -94,13 +90,16 @@ grub_datetime2unixtime (const struct grub_datetime *datetime, grub_int64_t *nix)
   ret += y4 * SECPER4YEARS;
   ret += ay * SECPERYEAR;
 
Quoted 3 lines

include/grub/datetime.h grub_datetime2unixtime (const struct grub_datetime *datetime, grub_int64_t *nix) · line 93

Quoted 1 line
include/grub/datetime.h +1 -0
+  ret -= ((datetime->year - 1) / 100 - (datetime->year - 1) / 400 - 15) * SECPERDAY;

What does this line do? And where 15 comes from?

I will investigate the origin / meaning of "15" here and add comments to explain this in the next version.

Thank you for the comments, I will try to get a V4 out tonight.

Thanks!
Andrew

Quoted 152 lines · Daniel Kiper
Quoted 40 lines · Andrew Hamilton

Support dates outside of 1901..2038.

---
 grub-core/lib/datetime.c | 48 ++++++++++++++++++++++++++++++++--------
 include/grub/datetime.h  | 15 ++++++-------
 2 files changed, 46 insertions(+), 17 deletions(-)
grub-core/lib/datetime.c +5 -1
index 8f0922fb0..4e68eabc6 100644
@@ -64,6 +64,10 @@ grub_get_weekday_name (struct grub_datetime *datetime)
 #define SECPERDAY (24*SECPERHOUR)
 #define DAYSPERYEAR 365
 #define DAYSPER4YEARS (4*DAYSPERYEAR+1)
+/* 24 leap years in 100 years */
+#define DAYSPER100YEARS (100*DAYSPERYEAR+24)
+/* 97 leap years in 400 years */
+#define DAYSPER400YEARS (400*DAYSPERYEAR+97)
 
 
 void
@@ -76,7 +80,7 @@ grub_unixtime2datetime (grub_int64_t nix, struct grub_datetime *datetime)
   /* Convenience: let's have 3 consecutive non-bissextile years
      at the beginning of the counting date. So count from 1901. */
   int days_epoch;
-  /* Number of days since 1st Januar, 1901.  */
+  /* Number of days since 1st January, 1 (proleptic).  */
   unsigned days;
   /* Seconds into current day.  */
   unsigned secs_in_day;
@@ -92,12 +96,39 @@ grub_unixtime2datetime (grub_int64_t nix, struct grub_datetime *datetime)
     days_epoch = grub_divmod64 (nix, SECPERDAY, NULL);
 
   secs_in_day = nix - days_epoch * SECPERDAY;

grub-core/lib/datetime.c grub_unixtime2datetime (grub_int64_t nix, struct grub_datetime *datetime) · line 107

Quoted 10 lines
grub-core/lib/datetime.c +9 -1
-  days = days_epoch + 69 * DAYSPERYEAR + 17;
+  /* 1970 is Unix Epoch. Adjust to a year 1 epoch:
+     Leap year logic:
+      - Years evenly divisible by 400 are leap years
+      - Otherwise, if divisible by 100 are not leap years
+      - Otherwise, if divisible by 4 are leap years
+     There are four 400-year periods (1600 years worth of days with leap days)
+     There are three 100-year periods worth of leap days (3*24)
+     There are 369 years in addition to the four 400 year periods
+     There are 17 leap days in 69 years (beyond the three 100 year periods) */

May I ask you to stick to comments coding style [1]?

grub-core/lib/datetime.c grub_unixtime2datetime (grub_int64_t nix, struct grub_datetime *datetime) · line 116

Quoted 10 lines
grub-core/lib/datetime.c +8 -1
+  days = days_epoch + 369 * DAYSPERYEAR + 17 + 24 * 3 + 4 * DAYSPER400YEARS;
 
-  datetime->year = 1901 + 4 * (days / DAYSPER4YEARS);
+  datetime->year = 1 + 400 * (days / DAYSPER400YEARS);
+  days %= DAYSPER400YEARS;
+
+  /* On 31st December of bissextile (leap) years 365 days from the beginning
+     of the year elapsed but year isn't finished yet - every 400 years
+     396 is 4 years less than 400 year leap cycle
+     96 is 1 day less than number of leap days in 400 years */

Ditto... Here and below please...

Quoted 21 lines
grub-core/lib/datetime.c +14 -2
+  if (days / DAYSPER100YEARS == 4)
+    {
+      datetime->year += 396;
+      days -= 396*DAYSPERYEAR + 96;
+    }
+  else
+    {
+      datetime->year += 100 * (days / DAYSPER100YEARS);
+      days %= DAYSPER100YEARS;
+    }
+
+  datetime->year += 4 * (days / DAYSPER4YEARS);
   days %= DAYSPER4YEARS;
-  /* On 31st December of bissextile years 365 days from the beginning
-     of the year elapsed but year isn't finished yet */
+  /* On 31st December of bissextile (leap) years 365 days from the beginning
+     of the year elapsed but year isn't finished yet - every 4 years */
   if (days / DAYSPERYEAR == 4)
     {
       datetime->year += 3;

grub-core/lib/datetime.c grub_unixtime2datetime (grub_int64_t nix, struct grub_datetime *datetime) · line 142

Quoted 4 lines
grub-core/lib/datetime.c +1 -0
@@ -108,11 +139,10 @@ grub_unixtime2datetime (grub_int64_t nix, struct grub_datetime *datetime)
       datetime->year += days / DAYSPERYEAR;
       days %= DAYSPERYEAR;
     }
+  int isbisextile = datetime->year % 4 == 0 && (datetime->year % 100 != 0 || datetime->year % 400 == 0);

Please do not define variables in random places. I prefer if it is done at the beginning of the function or block.

And I think this should be a bool set with ternary operator...

... and s/isbisextile/is_bisextile/

grub-core/lib/datetime.c grub_unixtime2datetime (grub_int64_t nix, struct grub_datetime *datetime) · line 145

Quoted 7 lines
grub-core/lib/datetime.c +2 -4
   for (i = 0; i < 12
-      && days >= (i==1 && datetime->year % 4 == 0
-                   ? 29 : months[i]); i++)
-    days -= (i==1 && datetime->year % 4 == 0
-                         ? 29 : months[i]);
+        && days >= (i==1 && isbisextile ? 29 : months[i]); i++)
+    days -= (i==1 && isbisextile ? 29 : months[i]);

If you touch the code then please fix its coding style, e.g.:

  days -= (i == 1 && is_bisextile == true) ? 29 : months[i]

On the occasion it seems more readable...

Quoted 37 lines
grub-core/lib/datetime.c +0 -0
   datetime->month = i + 1;
   datetime->day = 1 + days;
   datetime->hour = (secs_in_day / SECPERHOUR);
include/grub/datetime.h +2 -6
index bcec636f0..9289b0d00 100644
@@ -54,7 +54,7 @@ void grub_unixtime2datetime (grub_int64_t nix,
 static inline int
 grub_datetime2unixtime (const struct grub_datetime *datetime, grub_int64_t *nix)
 {
-  grub_int32_t ret;
+  grub_int64_t ret;
   int y4, ay;
   const grub_uint16_t monthssum[12]
     = { 0,
@@ -75,15 +75,11 @@ grub_datetime2unixtime (const struct grub_datetime *datetime, grub_int64_t *nix)
   const int SECPERHOUR = 60 * SECPERMIN;
   const int SECPERDAY = 24 * SECPERHOUR;
   const int SECPERYEAR = 365 * SECPERDAY;
-  const int SECPER4YEARS = 4 * SECPERYEAR + SECPERDAY;
+  const grub_int64_t SECPER4YEARS = 4 * SECPERYEAR + SECPERDAY;
 
-  if (datetime->year > 2038 || datetime->year < 1901)
-    return 0;
   if (datetime->month > 12 || datetime->month < 1)
     return 0;
 
-  /* In the period of validity of unixtime all years divisible by 4
-     are bissextile*/
   /* Convenience: let's have 3 consecutive non-bissextile years
      at the beginning of the epoch. So count from 1973 instead of 1970 */
   ret = 3 * SECPERYEAR + SECPERDAY;
@@ -94,13 +90,16 @@ grub_datetime2unixtime (const struct grub_datetime *datetime, grub_int64_t *nix)
   ret += y4 * SECPER4YEARS;
   ret += ay * SECPERYEAR;
 
Quoted 5 lines

include/grub/datetime.h grub_datetime2unixtime (const struct grub_datetime *datetime, grub_int64_t *nix) · line 93

Quoted 1 line
include/grub/datetime.h +1 -0
+  ret -= ((datetime->year - 1) / 100 - (datetime->year - 1) / 400 - 15) * SECPERDAY;

What does this line do? And where 15 comes from?

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#4 lore raw

PATCH v3 2/2 date_unit_test: test dates outside of 32-bit unix range

In reply to Andrew Hamilton
---
 tests/date_unit_test.c | 35 ++++++++++++++++++++++++++---------
 1 file changed, 26 insertions(+), 9 deletions(-)
tests/date_unit_test.c +26 -9
index 99774f199..c76f37cb5 100644
@@ -25,12 +25,13 @@
 #include <grub/test.h>
 
 static void
-date_test (grub_int32_t v)
+date_test (grub_int64_t v)
 {
   struct grub_datetime dt;
   time_t t = v;
   struct tm *g;
   int w;
+  grub_int64_t back = 0;
 
   g = gmtime (&t);
 
@@ -38,28 +39,33 @@ date_test (grub_int32_t v)
 
   w = grub_get_weekday (&dt);
 
-  grub_test_assert (g->tm_sec == dt.second, "time %d bad second: %d vs %d", v,
+  grub_datetime2unixtime (&dt, &back);
+
+  grub_test_assert (g->tm_sec == dt.second, "time %lld bad second: %d vs %d", (long long)v,
 		    g->tm_sec, dt.second);
-  grub_test_assert (g->tm_min == dt.minute, "time %d bad minute: %d vs %d", v,
+  grub_test_assert (g->tm_min == dt.minute, "time %lld bad minute: %d vs %d", (long long)v,
 		    g->tm_min, dt.minute);
-  grub_test_assert (g->tm_hour == dt.hour, "time %d bad hour: %d vs %d", v,
+  grub_test_assert (g->tm_hour == dt.hour, "time %lld bad hour: %d vs %d", (long long)v,
 		    g->tm_hour, dt.hour);
-  grub_test_assert (g->tm_mday == dt.day, "time %d bad day: %d vs %d", v,
+  grub_test_assert (g->tm_mday == dt.day, "time %lld bad day: %d vs %d", (long long)v,
 		    g->tm_mday, dt.day);
-  grub_test_assert (g->tm_mon + 1 == dt.month, "time %d bad month: %d vs %d", v,
+  grub_test_assert (g->tm_mon + 1 == dt.month, "time %lld bad month: %d vs %d",(long long)v,
 		    g->tm_mon + 1, dt.month);
   grub_test_assert (g->tm_year + 1900 == dt.year,
-		    "time %d bad year: %d vs %d", v,
+                   "time %lld bad year: %d vs %d", (long long)v,
 		    g->tm_year + 1900, dt.year);
-  grub_test_assert (g->tm_wday == w, "time %d bad week day: %d vs %d", v,
+  grub_test_assert (g->tm_wday == w, "time %lld bad week day: %d vs %d", (long long)v,
 		    g->tm_wday, w);
+  grub_test_assert (back == v, "time %lld bad back transform: %lld", (long long)v,
+                   (long long)back);
 }
 
 static void
 date_test_iter (void)
 {
-  grub_int32_t tests[] = { -1, 0, +1, -2133156255, GRUB_INT32_MIN,
+  grub_int32_t tests[] = { -1, 0, +1, 978224552, -2133156255, -2110094321, GRUB_INT32_MIN,
 			   GRUB_INT32_MAX };
+  grub_int64_t tests64[] = { 5774965200LL, 4108700725LL, -5029179792LL };
   unsigned i;
 
   for (i = 0; i < ARRAY_SIZE (tests); i++)
@@ -71,6 +77,17 @@ date_test_iter (void)
       date_test (x);
       date_test (-x);
     }
+  if (sizeof (time_t) > 4)
+    {
+      for (i = 0; i < ARRAY_SIZE (tests64); i++)
+        date_test (tests64[i]);
+      for (i = 0; i < 10000000; i++)
+        {
+          /* Ranges from 0064 to 6869.  */
+          grub_int64_t x = rand () + (rand () % 100 - 28) * (grub_uint64_t)GRUB_INT32_MAX;
+          date_test (x);
+        }
+    }
 }
 
 GRUB_UNIT_TEST ("date_unit_test", date_test_iter);
Signature · 7 lines
2.39.5


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#5 lore raw

Re: [PATCH v3 2/2] date_unit_test: test dates outside of 32-bit unix range

Quoted 26 lines · Andrew Hamilton
---
 tests/date_unit_test.c | 35 ++++++++++++++++++++++++++---------
 1 file changed, 26 insertions(+), 9 deletions(-)
tests/date_unit_test.c +2 -1
index 99774f199..c76f37cb5 100644
@@ -25,12 +25,13 @@
 #include <grub/test.h>
 
 static void
-date_test (grub_int32_t v)
+date_test (grub_int64_t v)
 {
   struct grub_datetime dt;
   time_t t = v;
   struct tm *g;
   int w;
+  grub_int64_t back = 0;
 
   g = gmtime (&t);
 

tests/date_unit_test.c date_test (grub_int32_t v) · line 44

Quoted 7 lines
tests/date_unit_test.c +3 -1
@@ -38,28 +39,33 @@ date_test (grub_int32_t v)
 
   w = grub_get_weekday (&dt);
 
-  grub_test_assert (g->tm_sec == dt.second, "time %d bad second: %d vs %d", v,
+  grub_datetime2unixtime (&dt, &back);
+
+  grub_test_assert (g->tm_sec == dt.second, "time %lld bad second: %d vs %d", (long long)v,

Missing space before "v"...

tests/date_unit_test.c date_test (grub_int32_t v) · line 46

Quoted 3 lines
tests/date_unit_test.c +1 -1
 		    g->tm_sec, dt.second);
-  grub_test_assert (g->tm_min == dt.minute, "time %d bad minute: %d vs %d", v,
+  grub_test_assert (g->tm_min == dt.minute, "time %lld bad minute: %d vs %d", (long long)v,

Ditto... Here and below...

Quoted 18 lines
tests/date_unit_test.c +6 -5
 		    g->tm_min, dt.minute);
-  grub_test_assert (g->tm_hour == dt.hour, "time %d bad hour: %d vs %d", v,
+  grub_test_assert (g->tm_hour == dt.hour, "time %lld bad hour: %d vs %d", (long long)v,
 		    g->tm_hour, dt.hour);
-  grub_test_assert (g->tm_mday == dt.day, "time %d bad day: %d vs %d", v,
+  grub_test_assert (g->tm_mday == dt.day, "time %lld bad day: %d vs %d", (long long)v,
 		    g->tm_mday, dt.day);
-  grub_test_assert (g->tm_mon + 1 == dt.month, "time %d bad month: %d vs %d", v,
+  grub_test_assert (g->tm_mon + 1 == dt.month, "time %lld bad month: %d vs %d",(long long)v,
 		    g->tm_mon + 1, dt.month);
   grub_test_assert (g->tm_year + 1900 == dt.year,
-		    "time %d bad year: %d vs %d", v,
+                   "time %lld bad year: %d vs %d", (long long)v,
 		    g->tm_year + 1900, dt.year);
-  grub_test_assert (g->tm_wday == w, "time %d bad week day: %d vs %d", v,
+  grub_test_assert (g->tm_wday == w, "time %lld bad week day: %d vs %d", (long long)v,
 		    g->tm_wday, w);
+  grub_test_assert (back == v, "time %lld bad back transform: %lld", (long long)v,

tests/date_unit_test.c date_test (grub_int32_t v) · line 68

Quoted 10 lines
tests/date_unit_test.c +3 -1
+                   (long long)back);
 }
 
 static void
 date_test_iter (void)
 {
-  grub_int32_t tests[] = { -1, 0, +1, -2133156255, GRUB_INT32_MIN,
+  grub_int32_t tests[] = { -1, 0, +1, 978224552, -2133156255, -2110094321, GRUB_INT32_MIN,
 			   GRUB_INT32_MAX };
+  grub_int64_t tests64[] = { 5774965200LL, 4108700725LL, -5029179792LL };

It would be nice to know how these values were chosen...

Quoted 4 lines
tests/date_unit_test.c +0 -0
   unsigned i;
 
   for (i = 0; i < ARRAY_SIZE (tests); i++)

tests/date_unit_test.c date_test_iter (void) · line 86

Quoted 10 lines
tests/date_unit_test.c +7 -0
@@ -71,6 +77,17 @@ date_test_iter (void)
       date_test (x);
       date_test (-x);
     }
+  if (sizeof (time_t) > 4)
+    {
+      for (i = 0; i < ARRAY_SIZE (tests64); i++)
+        date_test (tests64[i]);
+      for (i = 0; i < 10000000; i++)
+        {
+          /* Ranges from 0064 to 6869.  */

How were you come up with this range?

tests/date_unit_test.c date_test_iter (void) · line 87

Quoted 1 line
tests/date_unit_test.c +1 -0
+          grub_int64_t x = rand () + (rand () % 100 - 28) * (grub_uint64_t)GRUB_INT32_MAX;

Where these numbers come from?

... and missing space before GRUB_INT32_MAX...

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#6 lore raw

Re: [PATCH v3 2/2] date_unit_test: test dates outside of 32-bit unix range

In reply to Andrew Hamilton
Quoted 3 lines · Andrew Hamilton

static void

[...]

tests/date_unit_test.c date_test (grub_int32_t v) · line 68

Quoted 6 lines

date_test_iter (void)
{

tests/date_unit_test.c +2 -1
-  grub_int32_t tests[] = { -1, 0, +1, -2133156255, GRUB_INT32_MIN,
+  grub_int32_t tests[] = { -1, 0, +1, 978224552, -2133156255, -2110094321, GRUB_INT32_MIN,
 			   GRUB_INT32_MAX };
+  grub_int64_t tests64[] = { 5774965200LL, 4108700725LL, -5029179792LL };

... and I think it would be more correct to cast these values to grub_int64_t instead of using "LL"...

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#7 lore raw

Re: [PATCH v3 2/2] date_unit_test: test dates outside of 32-bit unix range

Quoted 6 lines

tests/date_unit_test.c date_test (grub_int32_t v) · line 44

Quoted 4 lines
tests/date_unit_test.c +3 -1
-  grub_test_assert (g->tm_sec == dt.second, "time %d bad second: %d vs %d", v,
+  grub_datetime2unixtime (&dt, &back);
+
+  grub_test_assert (g->tm_sec == dt.second, "time %lld bad second: %d vs %d", (long long)v,

Missing space before "v"...

Agreed, will fix in v4.

Quoted 5 lines

tests/date_unit_test.c date_test (grub_int32_t v) · line 46

Quoted 3 lines
                  g->tm_sec, dt.second);
tests/date_unit_test.c +1 -1
-  grub_test_assert (g->tm_min == dt.minute, "time %d bad minute: %d vs %d", v,
+  grub_test_assert (g->tm_min == dt.minute, "time %lld bad minute: %d vs %d", (long long)v,

Ditto... Here and below...

Agreed, will fix in v4.

Quoted 6 lines

tests/date_unit_test.c date_test (grub_int32_t v) · line 68

Quoted 4 lines
tests/date_unit_test.c +2 -1
-  grub_int32_t tests[] = { -1, 0, +1, -2133156255, GRUB_INT32_MIN,
+  grub_int32_t tests[] = { -1, 0, +1, 978224552, -2133156255, -2110094321, GRUB_INT32_MIN,
                         GRUB_INT32_MAX };
+  grub_int64_t tests64[] = { 5774965200LL, 4108700725LL, -5029179792LL };

It would be nice to know how these values were chosen...

Agreed, I will investigate and add appropriate commentary in v4.

Quoted 7 lines

tests/date_unit_test.c date_test_iter (void) · line 86

Quoted 5 lines
tests/date_unit_test.c +5 -0
+      for (i = 0; i < ARRAY_SIZE (tests64); i++)
+        date_test (tests64[i]);
+      for (i = 0; i < 10000000; i++)
+        {
+          /* Ranges from 0064 to 6869.  */

How were you come up with this range?

I will investigate and add appropriate commentary in v4.

Quoted 4 lines
Quoted 3 lines
Quoted 2 lines

+ grub_int64_t x = rand () + (rand () % 100 - 28) * (grub_uint64_t)GRUB_INT32_MAX;

Where these numbers come from?

I will investigate and add appropriate commentary in v4.

Quoted 2 lines

... and missing space before GRUB_INT32_MAX...

Agreed, will fix in V4.

Quoted 10 lines

tests/date_unit_test.c date_test (grub_int32_t v) · line 68

Quoted 7 lines

static void
date_test_iter (void)
{

tests/date_unit_test.c +2 -1
-  grub_int32_t tests[] = { -1, 0, +1, -2133156255, GRUB_INT32_MIN,
+  grub_int32_t tests[] = { -1, 0, +1, 978224552, -2133156255, -2110094321, GRUB_INT32_MIN,
                         GRUB_INT32_MAX };
+  grub_int64_t tests64[] = { 5774965200LL, 4108700725LL, -5029179792LL };

... and I think it would be more correct to cast these values to grub_int64_t instead of using "LL"...

Ok, I will make this change in v4.

Thanks,
Andrew

Quoted 15 lines · Daniel Kiper
Quoted 3 lines · Andrew Hamilton

static void

[...]

tests/date_unit_test.c date_test (grub_int32_t v) · line 68

Quoted 6 lines

date_test_iter (void)
{

tests/date_unit_test.c +2 -1
-  grub_int32_t tests[] = { -1, 0, +1, -2133156255, GRUB_INT32_MIN,
+  grub_int32_t tests[] = { -1, 0, +1, 978224552, -2133156255, -2110094321, GRUB_INT32_MIN,
                         GRUB_INT32_MAX };
+  grub_int64_t tests64[] = { 5774965200LL, 4108700725LL, -5029179792LL };
Quoted 4 lines

... and I think it would be more correct to cast these values to grub_int64_t instead of using "LL"...

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#8 lore raw
In reply to Andrew Hamilton

Hello,

I was wondering if this was still on the radar.

Thanks,
Andrew

Quoted 14 lines · Andrew Hamilton

Support dates outside of 1901..2038.
Add tests for dates outside this range.
Vast majority of the work was done by Vladimir Serbinenko

V2 -> V3: Added additional commentary to datetime.c to describe
the origin of some of the numbers used in calculations.

Andrew Hamilton (2):
datetime: Support dates outside of 1901..2038 range
date_unit_test: test dates outside of 32-bit unix range

Quoted 9 lines
 grub-core/lib/datetime.c | 48 ++++++++++++++++++++++++++++++++--------
 include/grub/datetime.h  | 15 ++++++-------
 tests/date_unit_test.c   | 35 +++++++++++++++++++++--------
 3 files changed, 72 insertions(+), 26 deletions(-)

--
2.39.5

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel