Home | History | Annotate | Line # | Download | only in ski
      1 /*	$NetBSD: time.c,v 1.5 2026/01/29 17:51:58 nia Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000
      5  * Intel Corporation.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  *
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  *
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * 3. Neither the name of Intel Corporation or its contributors may be
     20  *    used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS''
     24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL INTEL CORPORATION OR CONTRIBUTORS BE
     27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     33  * THE POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 /* __FBSDID("$FreeBSD: src/sys/boot/ia64/libski/time.c,v 1.4 2003/09/08 09:11:32 obrien Exp $"); */
     38 
     39 //#include <time.h>
     40 #include <sys/time.h>
     41 #include <lib/libsa/stand.h>
     42 #include <lib/libsa/loadfile.h>
     43 
     44 #include "bootstrap.h"
     45 #include "libski.h"
     46 
     47 /*
     48 // Accurate only for the past couple of centuries;
     49 // that will probably do.
     50 //
     51 // (#defines From FreeBSD 3.2 lib/libc/stdtime/tzfile.h)
     52 */
     53 
     54 #define isleap(y)	(((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
     55 #define SECSPERHOUR ( 60*60 )
     56 #define SECSPERDAY	(24 * SECSPERHOUR)
     57 
     58 struct ssc_time {
     59 	int	Year;
     60 	int	Month;
     61 	int	Day;
     62 	int	Hour;
     63 	int	Minute;
     64 	int	Second;
     65 	int	Msec;
     66 	int	Wday;
     67 };
     68 
     69 time_t
     70 EfiTimeToUnixTime(struct ssc_time *ETime)
     71 {
     72     /*
     73     //  These arrays give the cumulative number of days up to the first of the
     74     //  month number used as the index (1 -> 12) for regular and leap years.
     75     //  The value at index 13 is for the whole year.
     76     */
     77     static time_t CumulativeDays[2][14] = {
     78     {0,
     79      0,
     80      31,
     81      31 + 28,
     82      31 + 28 + 31,
     83      31 + 28 + 31 + 30,
     84      31 + 28 + 31 + 30 + 31,
     85      31 + 28 + 31 + 30 + 31 + 30,
     86      31 + 28 + 31 + 30 + 31 + 30 + 31,
     87      31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
     88      31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
     89      31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
     90      31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
     91      31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 },
     92     {0,
     93      0,
     94      31,
     95      31 + 29,
     96      31 + 29 + 31,
     97      31 + 29 + 31 + 30,
     98      31 + 29 + 31 + 30 + 31,
     99      31 + 29 + 31 + 30 + 31 + 30,
    100      31 + 29 + 31 + 30 + 31 + 30 + 31,
    101      31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,
    102      31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
    103      31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
    104      31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
    105      31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 }};
    106 
    107     time_t  UTime;
    108     int     Year;
    109 
    110     ETime->Year += 1900;
    111 
    112     /*
    113     //  Do a santity check
    114     */
    115     if ( ETime->Year  <  1998 || ETime->Year   > 2099 ||
    116     	 ETime->Month ==    0 || ETime->Month  >   12 ||
    117     	 ETime->Day   ==    0 || ETime->Month  >   31 ||
    118     	                         ETime->Hour   >   23 ||
    119     	                         ETime->Minute >   59 ||
    120     	                         ETime->Second >   59 ) {
    121     	return (0);
    122     }
    123 
    124     /*
    125     // Years
    126     */
    127     UTime = 0;
    128     for (Year = 1970; Year != ETime->Year; ++Year) {
    129         UTime += (CumulativeDays[isleap(Year)][13] * SECSPERDAY);
    130     }
    131 
    132     /*
    133     // UTime should now be set to 00:00:00 on Jan 1 of the file's year.
    134     //
    135     // Months
    136     */
    137     UTime += (CumulativeDays[isleap(ETime->Year)][ETime->Month] * SECSPERDAY);
    138 
    139     /*
    140     // UTime should now be set to 00:00:00 on the first of the file's month and year
    141     //
    142     // Days -- Don't count the file's day
    143     */
    144     UTime += (((ETime->Day > 0) ? ETime->Day-1:0) * SECSPERDAY);
    145 
    146     /*
    147     // Hours
    148     */
    149     UTime += (ETime->Hour * SECSPERHOUR);
    150 
    151     /*
    152     // Minutes
    153     */
    154     UTime += (ETime->Minute * 60);
    155 
    156     /*
    157     // Seconds
    158     */
    159     UTime += ETime->Second;
    160 
    161     return UTime;
    162 }
    163 
    164 time_t
    165 time(time_t *tloc)
    166 {
    167 	struct ssc_time time;
    168 
    169 	ssc((u_int64_t) &time, 0, 0, 0, SSC_GET_RTC);
    170 
    171 	return *tloc = EfiTimeToUnixTime(&time);
    172 }
    173 
    174 time_t
    175 getsecs(void)
    176 {
    177     return time(0);
    178 }
    179