Link to home
Start Free TrialLog in
Avatar of rodney_w_morris
rodney_w_morris

asked on

C Date/Time Functions

Hello to all you C gurus.

I need a date/time function that offers precision down to the microsecond as well as providing standard dd/mm/yyyy:HH:MM:SS formating.   In other words, I need a time format of the following form:

           HH:MM:SS.microseconds

I have been able to find functions that allow me to do one or the other (microseconds or standard formats) but not both.

Any suggestions?

Avatar of Jase-Coder
Jase-Coder

I wrote the following:

#include<stdio.h>
#include<time.h>
int main(void)


    {
          char days[][10] = {"Sunday",
                             "Monday",
                             "Tuesday",
                             "Wednesday",
                             "Thursday",
                             "Friday",
                             "Saturday"
                            };
          char months[][20] = {"January",
                             "February",
                             "March",
                             "April",
                             "May",
                             "June",
                             "July",
                             "August",
                             "September",
                             "October",
                             "November",
                             "December"
                            };
          time_t tme;
          struct tm *system_time;
          tme = time(NULL); /* get the sys time in system calender format */
          system_time = localtime(&tme); /* system_time will point to a tm structure created by
                                  * the compiler
                                  */
          printf("The current time is: %.2d:%.2d:%.2d\n", system_time->tm_hour, system_time->tm_min, system_time->tm_sec);
          printf("The current date is: %s %d %s %d \n", days[system_time->tm_wday], system_time->tm_mday, months[system_time->tm_mon], (system_time->tm_year + 1900));      
          return 0;
}

your welcome to use ma code and functionise it
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Check this code..

   struct date d;
   struct time t;
   DWORD packedtime;
   WORD timebytes;
   WORD datebytes;

   getdate(&d);
   gettime(&t);

   printf("Date and time %d/%d/%d   %d:%d:%d\n",  d.da_mon,   d.da_day,    d.da_year,        t.ti_hour,        t.ti_min,        t.ti_sec       );

<dos.h> must be included for my above comment
Hi venkateshwarr,

The problem with using two calls to get the time is that the second counter could have ticked between calls.  If so, then subsequent calls to the routine for timing purposes could actually calculate to a negative time.



Kent

I guess you can use only gettime(&t) and take the time and calculate date and time, relative to the previous recorded date and time.

If your application is time-critical , then it is better to use interrupts to retrieve the time and date. This would be fast and lot better.



Avatar of rodney_w_morris

ASKER

Hello everyone,

I realize that I can get the time in microseconds (i.e. gettimeofday) and get the time formated down to the level of precision of seconds (i.e. localtime).  What I need is a way to get the following format:

          dd/mm/yyyy : HH:MM:SS.micro

          12/03/2004 : 03:45:19.654765

In other words, the only time formatting functions  that I have seen truncate the microsecond precision.  I would like to keep this precision without having to write my own conversion function.

Rod
Hi Kent,

My bad.  Somehow I skipped over your solution the first time I read through the page.  That looks perfect.  Thanks.

Rod