Lalchand
Lalchand

Reputation: 7827

How to read Bios date and time?

Is there any way to read the bios date and time using c.

There is a header file bios.h has a _bios_timeofday method for getting current time how to get current date.

Upvotes: 0

Views: 5230

Answers (2)

Edwin Buck
Edwin Buck

Reputation: 70939

Gratuitously expanded from the example you posted in your own link.

/*  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);
}

Upvotes: 0

Pankaj Kumar
Pankaj Kumar

Reputation: 83018

I don't know about any predefined method in bios.h which returns current date of bios. For this purpose you can use time.h

Like these ..

way 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 );
}

way 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;
}

And here is a good tutorial about bios.h and time.h methods with good examples.

Upvotes: 1

Related Questions