datetime.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2008 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef KERNEL_DATETIME_HEADER
  19. #define KERNEL_DATETIME_HEADER 1
  20. #include <grub/types.h>
  21. #include <grub/err.h>
  22. struct grub_datetime
  23. {
  24. grub_uint16_t year;
  25. grub_uint8_t month;
  26. grub_uint8_t day;
  27. grub_uint8_t hour;
  28. grub_uint8_t minute;
  29. grub_uint8_t second;
  30. };
  31. /* Return date and time. */
  32. #ifdef GRUB_MACHINE_EMU
  33. grub_err_t EXPORT_FUNC(grub_get_datetime) (struct grub_datetime *datetime);
  34. /* Set date and time. */
  35. grub_err_t EXPORT_FUNC(grub_set_datetime) (struct grub_datetime *datetime);
  36. #else
  37. grub_err_t grub_get_datetime (struct grub_datetime *datetime);
  38. /* Set date and time. */
  39. grub_err_t grub_set_datetime (struct grub_datetime *datetime);
  40. #endif
  41. int grub_get_weekday (struct grub_datetime *datetime);
  42. const char *grub_get_weekday_name (struct grub_datetime *datetime);
  43. void grub_unixtime2datetime (grub_int64_t nix,
  44. struct grub_datetime *datetime);
  45. static inline int
  46. grub_datetime2unixtime (const struct grub_datetime *datetime, grub_int64_t *nix)
  47. {
  48. grub_int32_t ret;
  49. int y4, ay;
  50. const grub_uint16_t monthssum[12]
  51. = { 0,
  52. 31,
  53. 31 + 28,
  54. 31 + 28 + 31,
  55. 31 + 28 + 31 + 30,
  56. 31 + 28 + 31 + 30 + 31,
  57. 31 + 28 + 31 + 30 + 31 + 30,
  58. 31 + 28 + 31 + 30 + 31 + 30 + 31,
  59. 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
  60. 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
  61. 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
  62. 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30};
  63. const grub_uint8_t months[12] = {31, 28, 31, 30, 31, 30,
  64. 31, 31, 30, 31, 30, 31};
  65. const int SECPERMIN = 60;
  66. const int SECPERHOUR = 60 * SECPERMIN;
  67. const int SECPERDAY = 24 * SECPERHOUR;
  68. const int SECPERYEAR = 365 * SECPERDAY;
  69. const int SECPER4YEARS = 4 * SECPERYEAR + SECPERDAY;
  70. if (datetime->year > 2038 || datetime->year < 1901)
  71. return 0;
  72. if (datetime->month > 12 || datetime->month < 1)
  73. return 0;
  74. /* In the period of validity of unixtime all years divisible by 4
  75. are bissextile*/
  76. /* Convenience: let's have 3 consecutive non-bissextile years
  77. at the beginning of the epoch. So count from 1973 instead of 1970 */
  78. ret = 3 * SECPERYEAR + SECPERDAY;
  79. /* Transform C divisions and modulos to mathematical ones */
  80. y4 = ((datetime->year - 1) >> 2) - (1973 / 4);
  81. ay = datetime->year - 1973 - 4 * y4;
  82. ret += y4 * SECPER4YEARS;
  83. ret += ay * SECPERYEAR;
  84. ret += monthssum[datetime->month - 1] * SECPERDAY;
  85. if (ay == 3 && datetime->month >= 3)
  86. ret += SECPERDAY;
  87. ret += (datetime->day - 1) * SECPERDAY;
  88. if ((datetime->day > months[datetime->month - 1]
  89. && (!ay || datetime->month != 2 || datetime->day != 29))
  90. || datetime->day < 1)
  91. return 0;
  92. ret += datetime->hour * SECPERHOUR;
  93. if (datetime->hour > 23)
  94. return 0;
  95. ret += datetime->minute * 60;
  96. if (datetime->minute > 59)
  97. return 0;
  98. ret += datetime->second;
  99. /* Accept leap seconds. */
  100. if (datetime->second > 60)
  101. return 0;
  102. if ((datetime->year > 1980 && ret < 0)
  103. || (datetime->year < 1960 && ret > 0))
  104. return 0;
  105. *nix = ret;
  106. return 1;
  107. }
  108. #if (defined (__powerpc__) || defined (__sparc__)) && !defined (GRUB_UTIL)
  109. grub_err_t
  110. grub_get_datetime_cmos (struct grub_datetime *datetime);
  111. grub_err_t
  112. grub_set_datetime_cmos (struct grub_datetime *datetime);
  113. #endif
  114. #endif /* ! KERNEL_DATETIME_HEADER */