datetime.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #ifdef GRUB_MACHINE_EMU
  24. #include <grub/dl.h>
  25. GRUB_MOD_LICENSE ("GPLv3+");
  26. #endif
  27. static const char *const grub_weekday_names[] =
  28. {
  29. N_("Sunday"),
  30. N_("Monday"),
  31. N_("Tuesday"),
  32. N_("Wednesday"),
  33. N_("Thursday"),
  34. N_("Friday"),
  35. N_("Saturday"),
  36. };
  37. int
  38. grub_get_weekday (struct grub_datetime *datetime)
  39. {
  40. unsigned a, y, m;
  41. if (datetime->month <= 2)
  42. a = 1;
  43. else
  44. a = 0;
  45. y = datetime->year - a;
  46. m = datetime->month + 12 * a - 2;
  47. return (datetime->day + y + y / 4 - y / 100 + y / 400 + (31 * m / 12)) % 7;
  48. }
  49. const char *
  50. grub_get_weekday_name (struct grub_datetime *datetime)
  51. {
  52. return _ (grub_weekday_names[grub_get_weekday (datetime)]);
  53. }
  54. #define SECPERMIN 60
  55. #define SECPERHOUR (60*SECPERMIN)
  56. #define SECPERDAY (24*SECPERHOUR)
  57. #define DAYSPERYEAR 365
  58. #define DAYSPER4YEARS (4*DAYSPERYEAR+1)
  59. void
  60. grub_unixtime2datetime (grub_int64_t nix, struct grub_datetime *datetime)
  61. {
  62. int i;
  63. grub_uint8_t months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  64. /* In the period of validity of unixtime all years divisible by 4
  65. are bissextile*/
  66. /* Convenience: let's have 3 consecutive non-bissextile years
  67. at the beginning of the counting date. So count from 1901. */
  68. int days_epoch;
  69. /* Number of days since 1st Januar, 1901. */
  70. unsigned days;
  71. /* Seconds into current day. */
  72. unsigned secs_in_day;
  73. /* Transform C divisions and modulos to mathematical ones */
  74. if (nix < 0)
  75. /*
  76. * The result of division here shouldn't be larger than GRUB_INT_MAX.
  77. * So, it's safe to store the result back in an int.
  78. */
  79. days_epoch = -(grub_divmod64 (((grub_int64_t) (SECPERDAY) - nix - 1), SECPERDAY, NULL));
  80. else
  81. days_epoch = grub_divmod64 (nix, SECPERDAY, NULL);
  82. secs_in_day = nix - days_epoch * SECPERDAY;
  83. days = days_epoch + 69 * DAYSPERYEAR + 17;
  84. datetime->year = 1901 + 4 * (days / DAYSPER4YEARS);
  85. days %= DAYSPER4YEARS;
  86. /* On 31st December of bissextile years 365 days from the beginning
  87. of the year elapsed but year isn't finished yet */
  88. if (days / DAYSPERYEAR == 4)
  89. {
  90. datetime->year += 3;
  91. days -= 3*DAYSPERYEAR;
  92. }
  93. else
  94. {
  95. datetime->year += days / DAYSPERYEAR;
  96. days %= DAYSPERYEAR;
  97. }
  98. for (i = 0; i < 12
  99. && days >= (i==1 && datetime->year % 4 == 0
  100. ? 29 : months[i]); i++)
  101. days -= (i==1 && datetime->year % 4 == 0
  102. ? 29 : months[i]);
  103. datetime->month = i + 1;
  104. datetime->day = 1 + days;
  105. datetime->hour = (secs_in_day / SECPERHOUR);
  106. secs_in_day %= SECPERHOUR;
  107. datetime->minute = secs_in_day / SECPERMIN;
  108. datetime->second = secs_in_day % SECPERMIN;
  109. }