getting time/date 
Author Message
 getting time/date

Hi there,
Is there a way I can get the date and time in c?
I know how to get the proceessor time with time_t(&now);
But I want to get something so that I can have the date, and the time in
hours, mins, secs.
Any suggestions?
Thanks
Allan


Fri, 28 Oct 2005 02:24:47 GMT  
 getting time/date
On Sun, 11 May 2003 19:24:47 +0100, "Allan Bruce"

Quote:

>Hi there,
>Is there a way I can get the date and time in c?
>I know how to get the proceessor time with time_t(&now);
>But I want to get something so that I can have the date, and the time in
>hours, mins, secs.
>Any suggestions?

The library funtions localtime() and gmtime() will convert a time_t
value into a structure with the various parts.

Libary functions asctime() and strftime() are convenient ways to turn
the stuct tm into something printable.

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

int main ()
{
     time_t now;
     struct tm *now_tm;
     char *now_text;

     time (&now);
     now_tm = localtime (&now);
     now_text = asctime (now_tm);
     fputs (now_text, stdout);

     return 0;

Quote:
}



Fri, 28 Oct 2005 02:38:41 GMT  
 getting time/date

Quote:
> Is there a way I can get the date and time in c?
> I know how to get the proceessor time with time_t(&now);
> But I want to get something so that I can have the date, and the time in
> hours, mins, secs.

struct tm
mktime()
gmtime()
localtime()
asctime()
strftime()

--
-ed- emdel at noos.fr
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-library: http://www.dinkumware.com/htm_cl/index.html
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
                          -- Jerry Coffin in a.l.c.c++



Fri, 28 Oct 2005 04:58:10 GMT  
 getting time/date
[snip]

Allan try with this one..

#include <time.h>

void main( void )
{
        struct tm *newtime;
        char am_pm[] = "AM";
        time_t long_time;

        time( &long_time );                /* Get time as long integer. */
        newtime = localtime( &long_time ); /* Convert to local time. */

        if( newtime->tm_hour > 12 )        /* Set up extension. */
                strcpy( am_pm, "PM" );
        if( newtime->tm_hour > 12 )        /* Convert from 24-hour */
                newtime->tm_hour -= 12;    /*   to 12-hour clock.  */
        if( newtime->tm_hour == 0 )        /*Set hour to 12 if midnight. */
                newtime->tm_hour = 12;

        printf( "%.19s %s\n", asctime( newtime ), am_pm );

Quote:
}



Fri, 28 Oct 2005 15:38:43 GMT  
 getting time/date

Quote:

> [snip]

> Allan try with this one..

> #include <time.h>

> void main( void )

Don't you dare to presume to offer advice until you cease using this
illiterate construction.


Fri, 28 Oct 2005 22:48:50 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. C time/date to Visual Basic time/date

2. Date Time Picker - Date AND Time

3. Getting date and time from two 32 bit numbers

4. Getting Sql database server date and time??

5. Getting a file list based on creation date/time

6. Getting the Date and Time from the Server

7. Getting Date/Time from the Server

8. Getting directory creation date and time???

9. Getting Date/Time in C

10. Getting system date/time at midnight?

11. Getting at the spin control in a date/time picker

12. Getting the current date/time

 

 
Powered by phpBB® Forum Software