Operating System - Linux
1752421 Members
5800 Online
108788 Solutions
New Discussion юеВ

Getting date and time from from time.h

 
SOLVED
Go to solution
CA1490051
Frequent Advisor

Getting date and time from from time.h

Hi All,

I want to get the system date and time i am tryong man pages of local time

Here is my code
#include
int main()
{
tm TimeNow;
char *pTime = localtime(&TimeNow);

printf("%s",*pTime);
return 0;;

}

It says " tm undefined "

Can anyone help me in getting through this problem.

thanks and regards
Vikram
8 REPLIES 8
Torsten.
Acclaimed Contributor
Solution

Re: Getting date and time from from time.h

I did no programming for at least 10 years now, but I guess it must be something like

struct tm *TimeNow;
time_t Now;

...

Now = time ( NULL );
TimeNow = localtime ( &Now );

printf ( "%s", ctime ( &Now ) );


Hope this helps!
Regards
Torsten.

__________________________________________________
There are only 10 types of people in the world -
those who understand binary, and those who don't.

__________________________________________________
No support by private messages. Please ask the forum!

If you feel this was helpful please click the KUDOS! thumb below!   
Kapil Jha
Honored Contributor

Re: Getting date and time from from time.h

Yes it should be struct tm.
You have to define TimeNow as smthing.
There is nothin like tm.

BR,
KApil
I am in this small bowl, I wane see the real world......
Dennis Handly
Acclaimed Contributor

Re: Getting date and time from from time.h

You can also do:
#include
#include
int main() {
struct tm *TimeNow;
time_t now = time(NULL);
TimeNow = localtime(&now);
printf("%s", asctime(TimeNow));
return 0;
}
Dennis Handly
Acclaimed Contributor

Re: Getting date and time from from time.h

>KApil: There is nothin like tm.

In a real language like C++ there is. :-)
struct tm == tm
Torsten.
Acclaimed Contributor

Re: Getting date and time from from time.h

Real language?
;-)

Anyway, looks like "pure" C.

Here is the struct

struct tm {
int tm_sec; /* second (0-61, allows for leap seconds) */
int tm_min; /* minute (0-59) */
int tm_hour; /* hour (0-23) */
int tm_mday; /* day of the month (1-31) */
int tm_mon; /* month (0-11) */
int tm_year; /* years since 1900 */
int tm_wday; /* day of the week (0-6) */
int tm_yday; /* day of the year (0-365) */
int tm_isdst;/* non-0 if daylight savings time is in effect */
};


Hope this helps!
Regards
Torsten.

__________________________________________________
There are only 10 types of people in the world -
those who understand binary, and those who don't.

__________________________________________________
No support by private messages. Please ask the forum!

If you feel this was helpful please click the KUDOS! thumb below!   
CA1490051
Frequent Advisor

Re: Getting date and time from from time.h

Hi All,

Yes, i missed struct there.

I am also able to achieve this by following 2 lines of code

#include
long t = time(0);
cout << asctime(localtime(&t))<
thanks and regards
Vikram
Dennis Handly
Acclaimed Contributor

Re: Getting date and time from from time.h

>I am also able to achieve this by following 2 lines of code

If you are going to use iostream, you should go whole hog: :-)

#include
#include
#include
struct my_pt : public std::time_put {} tp;
struct my_ibase : public std::ios_base {};
int main() {
std::tm *TimeNow;
std::time_t now = std::time(NULL);
TimeNow = std::localtime(&now);
// printf("%s", asctime(TimeNow));
const char format[] = "%a %b %d %H:%M:%S %Y\n";
my_pt::iter_type o_itr(std::cout);
my_ibase ibase;
tp.put(o_itr, ibase, ' ', TimeNow, format, format + (sizeof(format)-1));
}
CA1490051
Frequent Advisor

Re: Getting date and time from from time.h

Hi All,

In my previous reply there is 1 wrong sorry for that
it should be as below

#include
time_t t = time(0);
cout << asctime(localtime(&t))<

thanks and regards
Vikram