2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2008 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#ifndef KERNEL_DATETIME_HEADER
2N/A#define KERNEL_DATETIME_HEADER 1
2N/A
2N/A#include <grub/types.h>
2N/A#include <grub/err.h>
2N/A
2N/Astruct grub_datetime
2N/A{
2N/A grub_uint16_t year;
2N/A grub_uint8_t month;
2N/A grub_uint8_t day;
2N/A grub_uint8_t hour;
2N/A grub_uint8_t minute;
2N/A grub_uint8_t second;
2N/A};
2N/A
2N/A/* Return date and time. */
2N/A#ifdef GRUB_MACHINE_EMU
2N/Agrub_err_t EXPORT_FUNC(grub_get_datetime) (struct grub_datetime *datetime);
2N/A
2N/A/* Set date and time. */
2N/Agrub_err_t EXPORT_FUNC(grub_set_datetime) (struct grub_datetime *datetime);
2N/A#else
2N/Agrub_err_t grub_get_datetime (struct grub_datetime *datetime);
2N/A
2N/A/* Set date and time. */
2N/Agrub_err_t grub_set_datetime (struct grub_datetime *datetime);
2N/A#endif
2N/A
2N/Aint grub_get_weekday (struct grub_datetime *datetime);
2N/Aconst char *grub_get_weekday_name (struct grub_datetime *datetime);
2N/A
2N/Avoid grub_unixtime2datetime (grub_int32_t nix,
2N/A struct grub_datetime *datetime);
2N/A
2N/Astatic inline int
2N/Agrub_datetime2unixtime (const struct grub_datetime *datetime, grub_int32_t *nix)
2N/A{
2N/A grub_int32_t ret;
2N/A int y4, ay;
2N/A const grub_uint16_t monthssum[12]
2N/A = { 0,
2N/A 31,
2N/A 31 + 28,
2N/A 31 + 28 + 31,
2N/A 31 + 28 + 31 + 30,
2N/A 31 + 28 + 31 + 30 + 31,
2N/A 31 + 28 + 31 + 30 + 31 + 30,
2N/A 31 + 28 + 31 + 30 + 31 + 30 + 31,
2N/A 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
2N/A 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
2N/A 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
2N/A 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30};
2N/A const grub_uint8_t months[12] = {31, 28, 31, 30, 31, 30,
2N/A 31, 31, 30, 31, 30, 31};
2N/A const int SECPERMIN = 60;
2N/A const int SECPERHOUR = 60 * SECPERMIN;
2N/A const int SECPERDAY = 24 * SECPERHOUR;
2N/A const int SECPERYEAR = 365 * SECPERDAY;
2N/A const int SECPER4YEARS = 4 * SECPERYEAR + SECPERDAY;
2N/A
2N/A if (datetime->year > 2038 || datetime->year < 1901)
2N/A return 0;
2N/A if (datetime->month > 12 || datetime->month < 1)
2N/A return 0;
2N/A
2N/A /* In the period of validity of unixtime all years divisible by 4
2N/A are bissextile*/
2N/A /* Convenience: let's have 3 consecutive non-bissextile years
2N/A at the beginning of the epoch. So count from 1973 instead of 1970 */
2N/A ret = 3 * SECPERYEAR + SECPERDAY;
2N/A
2N/A /* Transform C divisions and modulos to mathematical ones */
2N/A y4 = (datetime->year - 1973) / 4;
2N/A if (datetime->year < 1973)
2N/A y4--;
2N/A ay = datetime->year - 1973 - 4 * y4;
2N/A ret += y4 * SECPER4YEARS;
2N/A ret += ay * SECPERYEAR;
2N/A
2N/A ret += monthssum[datetime->month - 1] * SECPERDAY;
2N/A if (ay == 0 && datetime->month >= 3)
2N/A ret += SECPERDAY;
2N/A
2N/A ret += (datetime->day - 1) * SECPERDAY;
2N/A if ((datetime->day > months[datetime->month - 1]
2N/A && (!ay || datetime->month != 2 || datetime->day != 29))
2N/A || datetime->day < 1)
2N/A return 0;
2N/A
2N/A ret += datetime->hour * SECPERHOUR;
2N/A if (datetime->hour > 23)
2N/A return 0;
2N/A ret += datetime->minute * 60;
2N/A if (datetime->minute > 59)
2N/A return 0;
2N/A
2N/A ret += datetime->second;
2N/A /* Accept leap seconds. */
2N/A if (datetime->second > 60)
2N/A return 0;
2N/A
2N/A if ((datetime->year > 1980 && ret < 0)
2N/A || (datetime->year < 1960 && ret > 0))
2N/A return 0;
2N/A *nix = ret;
2N/A return 1;
2N/A}
2N/A
2N/A#if (defined (__powerpc__) || defined (__sparc__)) && !defined (GRUB_UTIL)
2N/Agrub_err_t
2N/Agrub_get_datetime_cmos (struct grub_datetime *datetime);
2N/Agrub_err_t
2N/Agrub_set_datetime_cmos (struct grub_datetime *datetime);
2N/A#endif
2N/A
2N/A#endif /* ! KERNEL_DATETIME_HEADER */