datetime.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. GRUB_EXPORT(grub_get_weekday);
  21. GRUB_EXPORT(grub_get_weekday_name);
  22. GRUB_EXPORT(grub_unixtime2datetime);
  23. static char *grub_weekday_names[] =
  24. {
  25. "Sunday",
  26. "Monday",
  27. "Tuesday",
  28. "Wednesday",
  29. "Thursday",
  30. "Friday",
  31. "Saturday",
  32. };
  33. int
  34. grub_get_weekday (struct grub_datetime *datetime)
  35. {
  36. int a, y, m;
  37. a = (14 - datetime->month) / 12;
  38. y = datetime->year - a;
  39. m = datetime->month + 12 * a - 2;
  40. return (datetime->day + y + y / 4 - y / 100 + y / 400 + (31 * m / 12)) % 7;
  41. }
  42. char *
  43. grub_get_weekday_name (struct grub_datetime *datetime)
  44. {
  45. return grub_weekday_names[grub_get_weekday (datetime)];
  46. }
  47. #define SECPERMIN 60
  48. #define SECPERHOUR (60*SECPERMIN)
  49. #define SECPERDAY (24*SECPERHOUR)
  50. #define SECPERYEAR (365*SECPERDAY)
  51. #define SECPER4YEARS (4*SECPERYEAR+SECPERDAY)
  52. void
  53. grub_unixtime2datetime (grub_int32_t nix, struct grub_datetime *datetime)
  54. {
  55. int i;
  56. int div;
  57. grub_uint8_t months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  58. /* In the period of validity of unixtime all years divisible by 4
  59. are bissextile*/
  60. /* Convenience: let's have 3 consecutive non-bissextile years
  61. at the beginning of the epoch. So count from 1973 instead of 1970 */
  62. nix -= 3*SECPERYEAR + SECPERDAY;
  63. /* Transform C divisions and modulos to mathematical ones */
  64. div = nix / SECPER4YEARS;
  65. if (nix < 0)
  66. div--;
  67. datetime->year = 1973 + 4 * div;
  68. nix -= div * SECPER4YEARS;
  69. /* On 31st December of bissextile years 365 days from the beginning
  70. of the year elapsed but year isn't finished yet */
  71. if (nix / SECPERYEAR == 4)
  72. {
  73. datetime->year += 3;
  74. nix -= 3*SECPERYEAR;
  75. }
  76. else
  77. {
  78. datetime->year += nix / SECPERYEAR;
  79. nix %= SECPERYEAR;
  80. }
  81. for (i = 0; i < 12
  82. && nix >= ((grub_int32_t) (i==1 && datetime->year % 4 == 0
  83. ? 29 : months[i]))*SECPERDAY; i++)
  84. nix -= ((grub_int32_t) (i==1 && datetime->year % 4 == 0
  85. ? 29 : months[i]))*SECPERDAY;
  86. datetime->month = i + 1;
  87. datetime->day = 1 + (nix / SECPERDAY);
  88. nix %= SECPERDAY;
  89. datetime->hour = (nix / SECPERHOUR);
  90. nix %= SECPERHOUR;
  91. datetime->minute = nix / SECPERMIN;
  92. datetime->second = nix % SECPERMIN;
  93. }