Skip to contentSkip to conversation

datetime: Support dates outside of 1901..2038 range

PATCH In review Vladimir Serbinenko 3 participants 9 messages Aug 17, 2024 – Apr 17, 2025

lore
Share & tools
lore
#0 lore raw

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

---
 grub-core/lib/datetime.c | 31 ++++++++++++++++++++++++-------
 include/grub/datetime.h  | 15 +++++++--------
 2 files changed, 31 insertions(+), 15 deletions(-)
grub-core/lib/datetime.c +24 -7
index 9120128ca..e4bdc5c4f 100644
@@ -59,6 +59,8 @@ grub_get_weekday_name (struct grub_datetime *datetime)
 #define SECPERDAY (24*SECPERHOUR)
 #define DAYSPERYEAR 365
 #define DAYSPER4YEARS (4*DAYSPERYEAR+1)
+#define DAYSPER100YEARS (100*DAYSPERYEAR+24)
+#define DAYSPER400YEARS (400*DAYSPERYEAR+97)
 
 
 void
@@ -71,7 +73,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;
@@ -87,9 +89,25 @@ 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;
+  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 years 365 days from the beginning
+     of the year elapsed but year isn't finished yet */
+  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 */
@@ -103,11 +121,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.2


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

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

---
 tests/date_unit_test.c | 35 ++++++++++++++++++++++++++---------
 1 file changed, 26 insertions(+), 9 deletions(-)
tests/date_unit_test.c +26 -9
index 99774f199..a489fc3d8 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.2


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

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

Thank you for doing this, it worked perfectly for me when I tested it on x86-64 in a VM.

Sincerely,
Andrew

Quoted 89 lines · Vladimir Serbinenko
---
 tests/date_unit_test.c | 35 ++++++++++++++++++++++++++---------
 1 file changed, 26 insertions(+), 9 deletions(-)
tests/date_unit_test.c +26 -10
index 99774f199..a489fc3d8 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);
--

2.39.2

Quoted 4 lines

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

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

#3 lore raw

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

In reply to Vladimir Serbinenko
Quoted 53 lines · Vladimir Serbinenko
---
 tests/date_unit_test.c | 35 ++++++++++++++++++++++++++---------
 1 file changed, 26 insertions(+), 9 deletions(-)
tests/date_unit_test.c +12 -8
index 99774f199..a489fc3d8 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,

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 };

Where do these numbers come from?

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.  */

Why?

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;

Nit, wrong coding style for cast. I can see similar problems in other places too...

Daniel

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

#4 lore raw
In reply to Vladimir Serbinenko

Thank you for doing this, it worked perfectly for me when I tested it on x86-64 in a VM.

Testing performed:
1. Set various files on FAT32 and Ext4 file systems with various dates in the future (2038, 2040, 2100, etc.) and confirmed correct date / time display via GRUB 'ls' command.
2. Ran your provided updated unit test suite
3. Confirmed booting of a Linux OS in a VM with the updated GRUB worked normally
4. Ran the updated 'grub_datetime2unixtime' function for all years [1900 to 2999] / months [0 to 13] / days [0 to 32] with combinations of random in / out of bounds hours/minutes/seconds - confirmed identical results to an alternate implementation. Also ran this with various compiler sanitizers enabled - no issues seen.
5. Ran the updated 'grub_datetime2unixtime' function with maximum (65535) and minimum (1) valid year / month / day / hour / min / second
- no issues seen.
6. I ran coverage on the updated 'grub_datetime2unixtime' and was able to cover all statements except the overflow check at the end (not saying this has to be taken out - maybe be a good sanity check):
if ((datetime->year > 1980 && ret < 0)
339003: 155: || (datetime->year < 1960 && ret > 0))

    #####:  156:    return 0;

Sincerely,
Andrew

Quoted 125 lines · Vladimir Serbinenko
---
 grub-core/lib/datetime.c | 31 ++++++++++++++++++++++++-------
 include/grub/datetime.h  | 15 +++++++--------
 2 files changed, 31 insertions(+), 15 deletions(-)
grub-core/lib/datetime.c +24 -7
index 9120128ca..e4bdc5c4f 100644
@@ -59,6 +59,8 @@ grub_get_weekday_name (struct grub_datetime *datetime)
 #define SECPERDAY (24*SECPERHOUR)
 #define DAYSPERYEAR 365
 #define DAYSPER4YEARS (4*DAYSPERYEAR+1)
+#define DAYSPER100YEARS (100*DAYSPERYEAR+24)
+#define DAYSPER400YEARS (400*DAYSPERYEAR+97)
 
 
 void
@@ -71,7 +73,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;
@@ -87,9 +89,25 @@ 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;
+  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 years 365 days from the beginning
+     of the year elapsed but year isn't finished yet */
+  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 */
@@ -103,11 +121,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 -9
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;
 
--

2.39.2

Quoted 4 lines

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

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

#5 lore raw
In reply to Vladimir Serbinenko
Quoted 29 lines · Vladimir Serbinenko
---
 grub-core/lib/datetime.c | 31 ++++++++++++++++++++++++-------
 include/grub/datetime.h  | 15 +++++++--------
 2 files changed, 31 insertions(+), 15 deletions(-)
grub-core/lib/datetime.c +3 -1
index 9120128ca..e4bdc5c4f 100644
@@ -59,6 +59,8 @@ grub_get_weekday_name (struct grub_datetime *datetime)
 #define SECPERDAY (24*SECPERHOUR)
 #define DAYSPERYEAR 365
 #define DAYSPER4YEARS (4*DAYSPERYEAR+1)
+#define DAYSPER100YEARS (100*DAYSPERYEAR+24)
+#define DAYSPER400YEARS (400*DAYSPERYEAR+97)
 
 
 void
@@ -71,7 +73,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;

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

Quoted 5 lines
grub-core/lib/datetime.c +1 -1
@@ -87,9 +89,25 @@ 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;
+  days = days_epoch + 369 * DAYSPERYEAR + 17 + 24 * 3 + 4 * DAYSPER400YEARS;

I can imagine how this math was build but I would prefer if you add a comment where these numbers come from...

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

Quoted 6 lines
grub-core/lib/datetime.c +5 -1
-  datetime->year = 1901 + 4 * (days / DAYSPER4YEARS);
+  datetime->year = 1 + 400 * (days / DAYSPER400YEARS);
+  days %= DAYSPER400YEARS;
+
+  /* On 31st December of bissextile years 365 days from the beginning
+     of the year elapsed but year isn't finished yet */

I think the comment is not precise. It happens once per 400 years. Right? I think it should be clarified.

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

Quoted 4 lines
grub-core/lib/datetime.c +4 -0
+  if (days / DAYSPER100YEARS == 4)
+    {
+      datetime->year += 396;
+      days -= 396*DAYSPERYEAR + 96;

Where do these numbers come from? The math begs for comment.

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

Quoted 5 lines
grub-core/lib/datetime.c +5 -0
+    }
+  else
+    {
+      datetime->year += 100 * (days / DAYSPER100YEARS);
+      days %= DAYSPER100YEARS;

Ditto.

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

Quoted 11 lines
grub-core/lib/datetime.c +4 -0
+    }
+
+  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 */
@@ -103,11 +121,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);

Could you use bool instead? And define variable at the beginning of function?

Quoted 41 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]);
   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;

Again, please comment this math...

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

Quoted 3 lines
include/grub/datetime.h +1 -1
   ret += monthssum[datetime->month - 1] * SECPERDAY;
-  if (ay == 3 && datetime->month >= 3)
+  int isbisextile = ay == 3 && (datetime->year % 100 != 0 || datetime->year % 400 == 0);

Ditto.

And could you use bool instead of int?

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

Quoted 7 lines
include/grub/datetime.h +2 -1
+  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))

It would be nice to add a comment here too.

Daniel

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

#6 lore raw

Just checking if this was still on the radar, seems mostly comment and type changes so maybe a relatively minor update?

If any help is needed to make the adjustments let me know and I can help.

Thanks,
Andrew

Quoted 154 lines · Daniel Kiper

grub-core/lib/datetime.c grub_unixtime2datetime (grub_int64_t nix, struct

Quoted 20 lines · Vladimir Serbinenko
---
 grub-core/lib/datetime.c | 31 ++++++++++++++++++++++++-------
 include/grub/datetime.h  | 15 +++++++--------
 2 files changed, 31 insertions(+), 15 deletions(-)
grub-core/lib/datetime.c +2 -0
index 9120128ca..e4bdc5c4f 100644
@@ -59,6 +59,8 @@ grub_get_weekday_name (struct grub_datetime *datetime)
 #define SECPERDAY (24*SECPERHOUR)
 #define DAYSPERYEAR 365
 #define DAYSPER4YEARS (4*DAYSPERYEAR+1)
+#define DAYSPER100YEARS (100*DAYSPERYEAR+24)
+#define DAYSPER400YEARS (400*DAYSPERYEAR+97)
 
 
 void
@@ -71,7 +73,7 @@ grub_unixtime2datetime (grub_int64_t nix, struct

grub_datetime *datetime)

grub-core/lib/datetime.c grub_unixtime2datetime (grub_int64_t nix, struct

Quoted 9 lines
grub-core/lib/datetime.c +1 -1
   /* 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;
@@ -87,9 +89,25 @@ grub_unixtime2datetime (grub_int64_t nix, struct

grub_datetime *datetime)

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

Quoted 5 lines
grub-core/lib/datetime.c +1 -1
     days_epoch = grub_divmod64 (nix, SECPERDAY, NULL);
 
   secs_in_day = nix - days_epoch * SECPERDAY;
-  days = days_epoch + 69 * DAYSPERYEAR + 17;
+  days = days_epoch + 369 * DAYSPERYEAR + 17 + 24 * 3 + 4 *

DAYSPER400YEARS;

I can imagine how this math was build but I would prefer if you add a comment where these numbers come from...

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

Quoted 6 lines
grub-core/lib/datetime.c +5 -1
-  datetime->year = 1901 + 4 * (days / DAYSPER4YEARS);
+  datetime->year = 1 + 400 * (days / DAYSPER400YEARS);
+  days %= DAYSPER400YEARS;
+
+  /* On 31st December of bissextile years 365 days from the beginning
+     of the year elapsed but year isn't finished yet */

I think the comment is not precise. It happens once per 400 years. Right? I think it should be clarified.

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

Quoted 4 lines
grub-core/lib/datetime.c +4 -0
+  if (days / DAYSPER100YEARS == 4)
+    {
+      datetime->year += 396;
+      days -= 396*DAYSPERYEAR + 96;

Where do these numbers come from? The math begs for comment.

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

Quoted 5 lines
grub-core/lib/datetime.c +5 -0
+    }
+  else
+    {
+      datetime->year += 100 * (days / DAYSPER100YEARS);
+      days %= DAYSPER100YEARS;

Ditto.

grub-core/lib/datetime.c grub_unixtime2datetime (grub_int64_t nix, struct

Quoted 7 lines
grub-core/lib/datetime.c +3 -0
+    }
+
+  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 */
@@ -103,11 +121,10 @@ grub_unixtime2datetime (grub_int64_t nix, struct

grub_datetime *datetime)

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

Quoted 4 lines
grub-core/lib/datetime.c +1 -0
       datetime->year += days / DAYSPERYEAR;
       days %= DAYSPERYEAR;
     }
+  int isbisextile = datetime->year % 4 == 0 && (datetime->year % 100 !=

0 || datetime->year % 400 == 0);

Could you use bool instead? And define variable at the beginning of function?

Quoted 15 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]);
   datetime->month = i + 1;
   datetime->day = 1 + days;
   datetime->hour = (secs_in_day / SECPERHOUR);
include/grub/datetime.h +0 -0
index bcec636f0..9289b0d00 100644

include/grub/datetime.h void grub_unixtime2datetime (grub_int64_t nix, · line 55

Quoted 2 lines
include/grub/datetime.h +0 -0
@@ -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)

include/grub/datetime.h grub_datetime2unixtime (const struct grub_datetime

Quoted 7 lines
include/grub/datetime.h +1 -1
 {
-  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)

Quoted 5 lines
include/grub/datetime.h +1 -1
   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;

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

Quoted 10 lines
include/grub/datetime.h +0 -4
 
-  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

*/

include/grub/datetime.h grub_datetime2unixtime (const struct grub_datetime

Quoted 2 lines
include/grub/datetime.h +0 -0
   ret = 3 * SECPERYEAR + SECPERDAY;
@@ -94,13 +90,16 @@ grub_datetime2unixtime (const struct grub_datetime

*datetime, grub_int64_t *nix)

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

Quoted 4 lines
include/grub/datetime.h +1 -0
   ret += y4 * SECPER4YEARS;
   ret += ay * SECPERYEAR;
 
+  ret -= ((datetime->year - 1) / 100 - (datetime->year - 1) / 400 - 15)

* SECPERDAY;

Again, please comment this math...

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

Quoted 3 lines
include/grub/datetime.h +1 -1
   ret += monthssum[datetime->month - 1] * SECPERDAY;
-  if (ay == 3 && datetime->month >= 3)
+  int isbisextile = ay == 3 && (datetime->year % 100 != 0 ||

datetime->year % 400 == 0);

Ditto.

And could you use bool instead of int?

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

Quoted 7 lines
include/grub/datetime.h +2 -1
+  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))
Quoted 9 lines

It would be nice to add a comment here too.

Daniel

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

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

#7 lore raw

Just checking on this again. I know people are busy so understand.

Thanks,
Andrew

Quoted 175 lines · Andrew Hamilton

Just checking if this was still on the radar, seems mostly comment and type changes so maybe a relatively minor update?

If any help is needed to make the adjustments let me know and I can help.

Thanks,
Andrew

Quoted 164 lines · Daniel Kiper

grub-core/lib/datetime.c grub_unixtime2datetime (grub_int64_t nix, struct

Quoted 20 lines · Vladimir Serbinenko
---
 grub-core/lib/datetime.c | 31 ++++++++++++++++++++++++-------
 include/grub/datetime.h  | 15 +++++++--------
 2 files changed, 31 insertions(+), 15 deletions(-)
grub-core/lib/datetime.c +2 -0
index 9120128ca..e4bdc5c4f 100644
@@ -59,6 +59,8 @@ grub_get_weekday_name (struct grub_datetime *datetime)
 #define SECPERDAY (24*SECPERHOUR)
 #define DAYSPERYEAR 365
 #define DAYSPER4YEARS (4*DAYSPERYEAR+1)
+#define DAYSPER100YEARS (100*DAYSPERYEAR+24)
+#define DAYSPER400YEARS (400*DAYSPERYEAR+97)
 
 
 void
@@ -71,7 +73,7 @@ grub_unixtime2datetime (grub_int64_t nix, struct

grub_datetime *datetime)

grub-core/lib/datetime.c grub_unixtime2datetime (grub_int64_t nix, struct

Quoted 9 lines
grub-core/lib/datetime.c +1 -1
   /* 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;
@@ -87,9 +89,25 @@ grub_unixtime2datetime (grub_int64_t nix, struct

grub_datetime *datetime)

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

Quoted 5 lines
grub-core/lib/datetime.c +1 -1
     days_epoch = grub_divmod64 (nix, SECPERDAY, NULL);
 
   secs_in_day = nix - days_epoch * SECPERDAY;
-  days = days_epoch + 69 * DAYSPERYEAR + 17;
+  days = days_epoch + 369 * DAYSPERYEAR + 17 + 24 * 3 + 4 *

DAYSPER400YEARS;

I can imagine how this math was build but I would prefer if you add a comment where these numbers come from...

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

Quoted 6 lines
grub-core/lib/datetime.c +5 -1
-  datetime->year = 1901 + 4 * (days / DAYSPER4YEARS);
+  datetime->year = 1 + 400 * (days / DAYSPER400YEARS);
+  days %= DAYSPER400YEARS;
+
+  /* On 31st December of bissextile years 365 days from the beginning
+     of the year elapsed but year isn't finished yet */

I think the comment is not precise. It happens once per 400 years. Right? I think it should be clarified.

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

Quoted 4 lines
grub-core/lib/datetime.c +4 -0
+  if (days / DAYSPER100YEARS == 4)
+    {
+      datetime->year += 396;
+      days -= 396*DAYSPERYEAR + 96;

Where do these numbers come from? The math begs for comment.

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

Quoted 5 lines
grub-core/lib/datetime.c +5 -0
+    }
+  else
+    {
+      datetime->year += 100 * (days / DAYSPER100YEARS);
+      days %= DAYSPER100YEARS;

Ditto.

grub-core/lib/datetime.c grub_unixtime2datetime (grub_int64_t nix, struct

Quoted 7 lines
grub-core/lib/datetime.c +3 -0
+    }
+
+  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 */
@@ -103,11 +121,10 @@ grub_unixtime2datetime (grub_int64_t nix, struct

grub_datetime *datetime)

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

Quoted 4 lines
grub-core/lib/datetime.c +1 -0
       datetime->year += days / DAYSPERYEAR;
       days %= DAYSPERYEAR;
     }
+  int isbisextile = datetime->year % 4 == 0 && (datetime->year % 100

!= 0 || datetime->year % 400 == 0);

Could you use bool instead? And define variable at the beginning of function?

Quoted 15 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]);
   datetime->month = i + 1;
   datetime->day = 1 + days;
   datetime->hour = (secs_in_day / SECPERHOUR);
include/grub/datetime.h +0 -0
index bcec636f0..9289b0d00 100644

include/grub/datetime.h void grub_unixtime2datetime (grub_int64_t nix, · line 55

Quoted 2 lines
include/grub/datetime.h +0 -0
@@ -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)

include/grub/datetime.h grub_datetime2unixtime (const struct grub_datetime

Quoted 7 lines
include/grub/datetime.h +1 -1
 {
-  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)

Quoted 5 lines
include/grub/datetime.h +1 -1
   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;

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

Quoted 10 lines
include/grub/datetime.h +0 -4
 
-  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

*/

include/grub/datetime.h grub_datetime2unixtime (const struct grub_datetime

Quoted 2 lines
include/grub/datetime.h +0 -0
   ret = 3 * SECPERYEAR + SECPERDAY;
@@ -94,13 +90,16 @@ grub_datetime2unixtime (const struct grub_datetime

*datetime, grub_int64_t *nix)

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

Quoted 4 lines
include/grub/datetime.h +1 -0
   ret += y4 * SECPER4YEARS;
   ret += ay * SECPERYEAR;
 
+  ret -= ((datetime->year - 1) / 100 - (datetime->year - 1) / 400 -

15) * SECPERDAY;

Again, please comment this math...

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

Quoted 3 lines
include/grub/datetime.h +1 -1
   ret += monthssum[datetime->month - 1] * SECPERDAY;
-  if (ay == 3 && datetime->month >= 3)
+  int isbisextile = ay == 3 && (datetime->year % 100 != 0 ||

datetime->year % 400 == 0);

Ditto.

And could you use bool instead of int?

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

Quoted 7 lines
include/grub/datetime.h +2 -1
+  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))

It would be nice to add a comment here too.

Daniel

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

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

#8 lore raw

Hi Vladimir,

I can make an attempt at addressing Daniel's comments if that's okay - any objections?

Thanks,
Andrew

Quoted 184 lines · Andrew Hamilton

Just checking on this again. I know people are busy so understand.

Thanks,
Andrew

Quoted 176 lines · Andrew Hamilton

Just checking if this was still on the radar, seems mostly comment and type changes so maybe a relatively minor update?

If any help is needed to make the adjustments let me know and I can help.

Thanks,
Andrew

Quoted 165 lines · Daniel Kiper

grub-core/lib/datetime.c grub_get_weekday_name (struct grub_datetime

Quoted 11 lines · Vladimir Serbinenko
---
 grub-core/lib/datetime.c | 31 ++++++++++++++++++++++++-------
 include/grub/datetime.h  | 15 +++++++--------
 2 files changed, 31 insertions(+), 15 deletions(-)
grub-core/lib/datetime.c +0 -0
index 9120128ca..e4bdc5c4f 100644
@@ -59,6 +59,8 @@ grub_get_weekday_name (struct grub_datetime

*datetime)

grub-core/lib/datetime.c grub_unixtime2datetime (grub_int64_t nix, struct

Quoted 9 lines
grub-core/lib/datetime.c +2 -0
 #define SECPERDAY (24*SECPERHOUR)
 #define DAYSPERYEAR 365
 #define DAYSPER4YEARS (4*DAYSPERYEAR+1)
+#define DAYSPER100YEARS (100*DAYSPERYEAR+24)
+#define DAYSPER400YEARS (400*DAYSPERYEAR+97)
 
 
 void
@@ -71,7 +73,7 @@ grub_unixtime2datetime (grub_int64_t nix, struct

grub_datetime *datetime)

grub-core/lib/datetime.c grub_unixtime2datetime (grub_int64_t nix, struct

Quoted 9 lines
grub-core/lib/datetime.c +1 -1
   /* 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;
@@ -87,9 +89,25 @@ grub_unixtime2datetime (grub_int64_t nix, struct

grub_datetime *datetime)

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

Quoted 5 lines
grub-core/lib/datetime.c +1 -1
     days_epoch = grub_divmod64 (nix, SECPERDAY, NULL);
 
   secs_in_day = nix - days_epoch * SECPERDAY;
-  days = days_epoch + 69 * DAYSPERYEAR + 17;
+  days = days_epoch + 369 * DAYSPERYEAR + 17 + 24 * 3 + 4 *

DAYSPER400YEARS;

I can imagine how this math was build but I would prefer if you add a comment where these numbers come from...

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

Quoted 6 lines
grub-core/lib/datetime.c +5 -1
-  datetime->year = 1901 + 4 * (days / DAYSPER4YEARS);
+  datetime->year = 1 + 400 * (days / DAYSPER400YEARS);
+  days %= DAYSPER400YEARS;
+
+  /* On 31st December of bissextile years 365 days from the beginning
+     of the year elapsed but year isn't finished yet */

I think the comment is not precise. It happens once per 400 years. Right? I think it should be clarified.

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

Quoted 4 lines
grub-core/lib/datetime.c +4 -0
+  if (days / DAYSPER100YEARS == 4)
+    {
+      datetime->year += 396;
+      days -= 396*DAYSPERYEAR + 96;

Where do these numbers come from? The math begs for comment.

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

Quoted 5 lines
grub-core/lib/datetime.c +5 -0
+    }
+  else
+    {
+      datetime->year += 100 * (days / DAYSPER100YEARS);
+      days %= DAYSPER100YEARS;

Ditto.

grub-core/lib/datetime.c grub_unixtime2datetime (grub_int64_t nix, struct

Quoted 7 lines
grub-core/lib/datetime.c +3 -0
+    }
+
+  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 */
@@ -103,11 +121,10 @@ grub_unixtime2datetime (grub_int64_t nix, struct

grub_datetime *datetime)

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

Quoted 4 lines
grub-core/lib/datetime.c +1 -0
       datetime->year += days / DAYSPERYEAR;
       days %= DAYSPERYEAR;
     }
+  int isbisextile = datetime->year % 4 == 0 && (datetime->year % 100

!= 0 || datetime->year % 400 == 0);

Could you use bool instead? And define variable at the beginning of function?

Quoted 15 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]);
   datetime->month = i + 1;
   datetime->day = 1 + days;
   datetime->hour = (secs_in_day / SECPERHOUR);
include/grub/datetime.h +0 -0
index bcec636f0..9289b0d00 100644

include/grub/datetime.h void grub_unixtime2datetime (grub_int64_t nix, · line 55

Quoted 2 lines
include/grub/datetime.h +0 -0
@@ -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)

include/grub/datetime.h grub_datetime2unixtime (const struct grub_datetime

Quoted 7 lines
include/grub/datetime.h +1 -1
 {
-  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)

Quoted 5 lines
include/grub/datetime.h +1 -1
   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;

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

Quoted 10 lines
include/grub/datetime.h +0 -4
 
-  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 */

include/grub/datetime.h grub_datetime2unixtime (const struct grub_datetime

Quoted 2 lines
include/grub/datetime.h +0 -0
   ret = 3 * SECPERYEAR + SECPERDAY;
@@ -94,13 +90,16 @@ grub_datetime2unixtime (const struct grub_datetime

*datetime, grub_int64_t *nix)

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

Quoted 4 lines
include/grub/datetime.h +1 -0
   ret += y4 * SECPER4YEARS;
   ret += ay * SECPERYEAR;
 
+  ret -= ((datetime->year - 1) / 100 - (datetime->year - 1) / 400 -

15) * SECPERDAY;

Again, please comment this math...

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

Quoted 3 lines
include/grub/datetime.h +1 -1
   ret += monthssum[datetime->month - 1] * SECPERDAY;
-  if (ay == 3 && datetime->month >= 3)
+  int isbisextile = ay == 3 && (datetime->year % 100 != 0 ||

datetime->year % 400 == 0);

Ditto.

And could you use bool instead of int?

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

Quoted 7 lines
include/grub/datetime.h +2 -1
+  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))

It would be nice to add a comment here too.

Daniel

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

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