2011-06-10 158 views
0

有什么方法可以使用c来读取BIOS日期和时间。如何阅读Bios日期和时间?

有一个头文件bios.h有一个_bios_timeofday方法获取当前时间如何获取当前日期。

+1

如果目标机器没有BIOS,该怎么办? – 2011-06-10 10:27:02

+1

您提供的链接中有很多示例。你有没有试过看过他们,看看你能否想出来? – 2011-06-10 10:27:50

+0

我试过_bios_timeofday返回当前时间,但对于当前日期我不知道 – Lalchand 2011-06-10 10:29:07

回答

1

我不知道任何预定义的方法在bios.h返回当前日期的BIOS。为了这个目的,你可以使用time.h

像这些..

方式1:

#include <stdio.h> 
#include <time.h> 
void main() 
{ 
    char *Day[7] = { 
        "Sunday" , "Monday", "Tuesday", "Wednesday", 
        "Thursday", "Friday", "Saturday" 
       }; 
    char *Month[12] = { 
        "January", "February", "March", "April", 
        "May",  "June",  "July",  "August", 
        "September", "October", "November", "December" 
        }; 

    char *Suffix[] = { "st", "nd", "rd", "th" }; 
    int i = 3;         
    struct tm *OurT = NULL;     
    time_t Tval = 0; 
    Tval = time(NULL); 
    OurT = localtime(&Tval); 

    switch(OurT->tm_mday) 
    { 
    case 1: case 21: case 31: 
     i= 0;     /* Select "st" */ 
     break; 
    case 2: case 22: 
     i = 1;     /* Select "nd" */ 
     break; 
    case 3: case 23: 
     i = 2;     /* Select "rd" */ 
     break; 
    default: 
     i = 3;     /* Select "th" */ 
     break; 
    } 

    printf("\nToday is %s the %d%s %s %d", Day[OurT->tm_wday], 
     OurT->tm_mday, Suffix[i], Month[OurT->tm_mon], 1900 + OurT->tm_year); 
    printf("\nThe time is %d : %d : %d", 
             OurT->tm_hour, OurT->tm_min, OurT->tm_sec); 
} 

方式2:

#include<stdio.h> 
#include<time.h> 

int main(void) 
{ 
    time_t t; 
    time(&t); 
    printf("Todays date and time is : %s",ctime(&t)); 
    return 0; 
} 

here是一个很好的关于bios.h和time.h方法的教程。

0

从您在自己的链接中发布的示例无偿扩展。

/* Example for biostime */ 

#include <stdio.h> 
#include <bios.h> 

void main() 
{ 
    long ticks; 

    ticks = biostime (0, 0L); 
    printf("Ticks since midnight is %d\n", ticks); 
    printf("The seconds since midnight is %d\n", ticks*18.2); 

    int allSeconds = ticks*18.2; 

    int hours = allSeconds/3600; 
    int minutes = allSeconds/60 - hours * 60; 
    int seconds = allSeconds % 60; 

    // I like military time, if you don't covert it and add an AM/PM indicator. 
    printf("The bios time is %02d:%02d:%02d\n", hours, minutes, seconds); 
}