2011-06-10 148 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); 
}