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