datetime.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* datetime.c - Module for common datetime function. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2008 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/datetime.h>
  20. #include <grub/i18n.h>
  21. #include <grub/misc.h>
  22. #include <grub/mm.h>
  23. static const char *const grub_weekday_names[] =
  24. {
  25. N_("Sunday"),
  26. N_("Monday"),
  27. N_("Tuesday"),
  28. N_("Wednesday"),
  29. N_("Thursday"),
  30. N_("Friday"),
  31. N_("Saturday"),
  32. };
  33. int
  34. grub_get_weekday (struct grub_datetime *datetime)
  35. {
  36. unsigned a, y, m;
  37. if (datetime->month <= 2)
  38. a = 1;
  39. else
  40. a = 0;
  41. y = datetime->year - a;
  42. m = datetime->month + 12 * a - 2;
  43. return (datetime->day + y + y / 4 - y / 100 + y / 400 + (31 * m / 12)) % 7;
  44. }
  45. const char *
  46. grub_get_weekday_name (struct grub_datetime *datetime)
  47. {
  48. return _ (grub_weekday_names[grub_get_weekday (datetime)]);
  49. }
  50. #define SECPERMIN 60
  51. #define SECPERHOUR (60*SECPERMIN)
  52. #define SECPERDAY (24*SECPERHOUR)
  53. #define DAYSPERYEAR 365
  54. #define DAYSPER4YEARS (4*DAYSPERYEAR+1)
  55. void
  56. grub_unixtime2datetime (grub_int64_t nix, struct grub_datetime *datetime)
  57. {
  58. int i;
  59. grub_uint8_t months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  60. /* In the period of validity of unixtime all years divisible by 4
  61. are bissextile*/
  62. /* Convenience: let's have 3 consecutive non-bissextile years
  63. at the beginning of the counting date. So count from 1901. */
  64. int days_epoch;
  65. /* Number of days since 1st Januar, 1901. */
  66. unsigned days;
  67. /* Seconds into current day. */
  68. unsigned secs_in_day;
  69. /* Transform C divisions and modulos to mathematical ones */
  70. if (nix < 0)
  71. /*
  72. * The result of division here shouldn't be larger than GRUB_INT_MAX.
  73. * So, it's safe to store the result back in an int.
  74. */
  75. days_epoch = -(grub_divmod64 (((grub_int64_t) (SECPERDAY) - nix - 1), SECPERDAY, NULL));
  76. else
  77. days_epoch = grub_divmod64 (nix, SECPERDAY, NULL);
  78. secs_in_day = nix - days_epoch * SECPERDAY;
  79. days = days_epoch + 69 * DAYSPERYEAR + 17;
  80. datetime->year = 1901 + 4 * (days / DAYSPER4YEARS);
  81. days %= DAYSPER4YEARS;
  82. /* On 31st December of bissextile years 365 days from the beginning
  83. of the year elapsed but year isn't finished yet */
  84. if (days / DAYSPERYEAR == 4)
  85. {
  86. datetime->year += 3;
  87. days -= 3*DAYSPERYEAR;
  88. }
  89. else
  90. {
  91. datetime->year += days / DAYSPERYEAR;
  92. days %= DAYSPERYEAR;
  93. }
  94. for (i = 0; i < 12
  95. && days >= (i==1 && datetime->year % 4 == 0
  96. ? 29 : months[i]); i++)
  97. days -= (i==1 && datetime->year % 4 == 0
  98. ? 29 : months[i]);
  99. datetime->month = i + 1;
  100. datetime->day = 1 + days;
  101. datetime->hour = (secs_in_day / SECPERHOUR);
  102. secs_in_day %= SECPERHOUR;
  103. datetime->minute = secs_in_day / SECPERMIN;
  104. datetime->second = secs_in_day % SECPERMIN;
  105. }