datetime.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. static const char *const grub_weekday_names[] =
  22. {
  23. N_("Sunday"),
  24. N_("Monday"),
  25. N_("Tuesday"),
  26. N_("Wednesday"),
  27. N_("Thursday"),
  28. N_("Friday"),
  29. N_("Saturday"),
  30. };
  31. int
  32. grub_get_weekday (struct grub_datetime *datetime)
  33. {
  34. unsigned a, y, m;
  35. if (datetime->month <= 2)
  36. a = 1;
  37. else
  38. a = 0;
  39. y = datetime->year - a;
  40. m = datetime->month + 12 * a - 2;
  41. return (datetime->day + y + y / 4 - y / 100 + y / 400 + (31 * m / 12)) % 7;
  42. }
  43. const char *
  44. grub_get_weekday_name (struct grub_datetime *datetime)
  45. {
  46. return _ (grub_weekday_names[grub_get_weekday (datetime)]);
  47. }
  48. #define SECPERMIN 60
  49. #define SECPERHOUR (60*SECPERMIN)
  50. #define SECPERDAY (24*SECPERHOUR)
  51. #define DAYSPERYEAR 365
  52. #define DAYSPER4YEARS (4*DAYSPERYEAR+1)
  53. void
  54. grub_unixtime2datetime (grub_int32_t nix, struct grub_datetime *datetime)
  55. {
  56. int i;
  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 counting date. So count from 1901. */
  62. int days_epoch;
  63. /* Number of days since 1st Januar, 1901. */
  64. unsigned days;
  65. /* Seconds into current day. */
  66. unsigned secs_in_day;
  67. /* Transform C divisions and modulos to mathematical ones */
  68. if (nix < 0)
  69. days_epoch = -(((unsigned) (SECPERDAY-nix-1)) / SECPERDAY);
  70. else
  71. days_epoch = ((unsigned) nix) / SECPERDAY;
  72. secs_in_day = nix - days_epoch * SECPERDAY;
  73. days = days_epoch + 69 * DAYSPERYEAR + 17;
  74. datetime->year = 1901 + 4 * (days / DAYSPER4YEARS);
  75. days %= DAYSPER4YEARS;
  76. /* On 31st December of bissextile years 365 days from the beginning
  77. of the year elapsed but year isn't finished yet */
  78. if (days / DAYSPERYEAR == 4)
  79. {
  80. datetime->year += 3;
  81. days -= 3*DAYSPERYEAR;
  82. }
  83. else
  84. {
  85. datetime->year += days / DAYSPERYEAR;
  86. days %= DAYSPERYEAR;
  87. }
  88. for (i = 0; i < 12
  89. && days >= (i==1 && datetime->year % 4 == 0
  90. ? 29 : months[i]); i++)
  91. days -= (i==1 && datetime->year % 4 == 0
  92. ? 29 : months[i]);
  93. datetime->month = i + 1;
  94. datetime->day = 1 + days;
  95. datetime->hour = (secs_in_day / SECPERHOUR);
  96. secs_in_day %= SECPERHOUR;
  97. datetime->minute = secs_in_day / SECPERMIN;
  98. datetime->second = secs_in_day % SECPERMIN;
  99. }