* grub-core/normal/datetime.c (grub_unixtime2datetime): Fix mishandling

of first three years after start of validity of unixtime.
This commit is contained in:
Vladimir Serbinenko 2013-10-26 02:47:40 +02:00
parent b1c6d03760
commit 10bafa1c38
3 changed files with 35 additions and 23 deletions

View file

@ -1,3 +1,8 @@
2013-10-26 Vladimir Serbinenko <phcoder@gmail.com>
* grub-core/normal/datetime.c (grub_unixtime2datetime): Fix mishandling
of first three years after start of validity of unixtime.
2013-10-26 Vladimir Serbinenko <phcoder@gmail.com> 2013-10-26 Vladimir Serbinenko <phcoder@gmail.com>
* grub-core/normal/menu_entry.c (get_logical_num_lines): Use unsigned * grub-core/normal/menu_entry.c (get_logical_num_lines): Use unsigned

View file

@ -52,50 +52,55 @@ grub_get_weekday_name (struct grub_datetime *datetime)
#define SECPERMIN 60 #define SECPERMIN 60
#define SECPERHOUR (60*SECPERMIN) #define SECPERHOUR (60*SECPERMIN)
#define SECPERDAY (24*SECPERHOUR) #define SECPERDAY (24*SECPERHOUR)
#define SECPERYEAR (365*SECPERDAY) #define DAYSPERYEAR 365
#define SECPER4YEARS (4*SECPERYEAR+SECPERDAY) #define DAYSPER4YEARS (4*DAYSPERYEAR+1)
void void
grub_unixtime2datetime (grub_int32_t nix, struct grub_datetime *datetime) grub_unixtime2datetime (grub_int32_t nix, struct grub_datetime *datetime)
{ {
int i; int i;
int div;
grub_uint8_t months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; grub_uint8_t months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
/* In the period of validity of unixtime all years divisible by 4 /* In the period of validity of unixtime all years divisible by 4
are bissextile*/ are bissextile*/
/* Convenience: let's have 3 consecutive non-bissextile years /* Convenience: let's have 3 consecutive non-bissextile years
at the beginning of the epoch. So count from 1973 instead of 1970 */ at the beginning of the counting date. So count from 1901. */
nix -= 3*SECPERYEAR + SECPERDAY; int days_epoch;
/* Number of days since 1st Januar, 1901. */
unsigned days;
/* Seconds into current day. */
unsigned secs_in_day;
/* Transform C divisions and modulos to mathematical ones */ /* Transform C divisions and modulos to mathematical ones */
div = nix / SECPER4YEARS;
if (nix < 0) if (nix < 0)
div--; days_epoch = -(((unsigned) (SECPERDAY-nix-1)) / SECPERDAY);
datetime->year = 1973 + 4 * div; else
nix -= div * SECPER4YEARS; days_epoch = ((unsigned) nix) / SECPERDAY;
secs_in_day = nix - days_epoch * SECPERDAY;
days = days_epoch + 69 * DAYSPERYEAR + 17;
datetime->year = 1901 + 4 * (days / DAYSPER4YEARS);
days %= DAYSPER4YEARS;
/* On 31st December of bissextile years 365 days from the beginning /* On 31st December of bissextile years 365 days from the beginning
of the year elapsed but year isn't finished yet */ of the year elapsed but year isn't finished yet */
if (nix / SECPERYEAR == 4) if (days / DAYSPERYEAR == 4)
{ {
datetime->year += 3; datetime->year += 3;
nix -= 3*SECPERYEAR; days -= 3*DAYSPERYEAR;
} }
else else
{ {
datetime->year += nix / SECPERYEAR; datetime->year += days / DAYSPERYEAR;
nix %= SECPERYEAR; days %= DAYSPERYEAR;
} }
for (i = 0; i < 12 for (i = 0; i < 12
&& nix >= ((grub_int32_t) (i==1 && datetime->year % 4 == 0 && days >= (i==1 && datetime->year % 4 == 0
? 29 : months[i]))*SECPERDAY; i++) ? 29 : months[i]); i++)
nix -= ((grub_int32_t) (i==1 && datetime->year % 4 == 0 days -= (i==1 && datetime->year % 4 == 0
? 29 : months[i]))*SECPERDAY; ? 29 : months[i]);
datetime->month = i + 1; datetime->month = i + 1;
datetime->day = 1 + (nix / SECPERDAY); datetime->day = 1 + days;
nix %= SECPERDAY; datetime->hour = (secs_in_day / SECPERHOUR);
datetime->hour = (nix / SECPERHOUR); secs_in_day %= SECPERHOUR;
nix %= SECPERHOUR; datetime->minute = secs_in_day / SECPERMIN;
datetime->minute = nix / SECPERMIN; datetime->second = secs_in_day % SECPERMIN;
datetime->second = nix % SECPERMIN;
} }

View file

@ -129,6 +129,8 @@ typedef grub_int32_t grub_ssize_t;
#define GRUB_SHRT_MAX 0x7fff #define GRUB_SHRT_MAX 0x7fff
#define GRUB_UINT_MAX 4294967295U #define GRUB_UINT_MAX 4294967295U
#define GRUB_INT_MAX 0x7fffffff #define GRUB_INT_MAX 0x7fffffff
#define GRUB_INT32_MIN (-2147483647 - 1)
#define GRUB_INT32_MAX 2147483647
#if GRUB_CPU_SIZEOF_LONG == 8 #if GRUB_CPU_SIZEOF_LONG == 8
# define GRUB_ULONG_MAX 18446744073709551615UL # define GRUB_ULONG_MAX 18446744073709551615UL