Linux vs. _strdate _strtime
Michael Jensen

I've been using _strdate/_strtime (I include <time.h> of course) in my program to get the date/time (it's nice for the high scores you know) ... it seems to work in windows even with the allegro timer installed. Which is awesome.

When I go to compile on debian linux however I get "_strdate was not declared in this scope" along with "_strtime was not declared in this scope" -- are there some other functions I should be using instead for linux?

Thomas Fjellstrom

Well, one thing to note, all symbols that start with an underscore are reserved symbols ::) Somehow MS missed that...

Anyhow, _foo functions in windows are typically just renamed versions of the standard libc methods. Though those functions don't even seem to exist in libc. try strftime, ctime, asctime or strptime instead.

note, the first three are c89 and c99 (and another), while the latter is only SUSv2, and POSIX.1-2001.

Kitty Cat
char str[32]; /* Will hold the string for the current date/time; must be at least 26 bytes */
ctime_r(time(NULL), str);

The normal ctime function is C89, but is not reentrant. It's returned string may be overwritten on subsequent calls. ctime_r is safer.

Thomas Fjellstrom

Personally, I'd use strftime. Much safer, and you can easily specify the format.

Michael Jensen

google code search is a badass:

1char cptime[50]; //char ptr
2string GetDate()
3{
4 time_t now;
5 now = time((time_t*)NULL);
6 //time(&now); // shouldn't be needed right?
7 struct tm *l_time = localtime(&now);
8 sprintf(cptime, "%.2d/%.2d/%.4d", l_time->tm_mon, l_time->tm_mday, l_time->tm_year + 1900);
9 string strTime = cptime;
10 return strTime;
11}
12 
13string GetTime()
14{
15 time_t now;
16 now = time((time_t*)NULL);
17 //time(&now); // shouldn't be needed right?
18 struct tm *l_time = localtime(&now);
19 sprintf(cptime, "%.2d:%.2d:%.2d", l_time->tm_hour, l_time->tm_min, l_time->tm_sec);
20 string strTime = cptime;
21 return strTime;
22}

I have no idea what this is for, but I got most of that code from analyzing this

It works in msvc/gcc on windows, I just have to upload and try it on linux.

Thanks for the input guys, it DID help.

Thomas Fjellstrom

Or:

char buf[100];
/* Print today's date e.g. "January 31, 2001".  */
time_t mytime = time(NULL);
strftime (buf, 100, "%B %d, %Y", localtime(&mytime));
string strTime = buf;

:)

Michael Jensen

but I... I don't want that format...

however: localtime(time) seems like a neat way access the time... but... dont you have to call localtime(time(NULL)) or at least localtime(time())?

Thomas Fjellstrom
Quote:

however: localtime(time) seems like a neat way access the time... but... dont you have to call localtime(time(NULL)) or at least localtime(time())?

I made a couple mistakes* ;) and the "format" can be anything, seeing as its a FORMAT :P (forinstance, see the %D and %T format items)

  • see my edit ;)

Michael Jensen

so my current code now is:

1char cptime[50]; //char ptr
2string GetDate()
3{
4 time_t now = time(NULL);
5 strftime(cptime, 50, "%b. %d, %Y", localtime(&now)); //uses short month name
6 string strTime = cptime;
7 return strTime;
8}
9 
10string GetTime()
11{
12 time_t now = time(NULL);
13 strftime(cptime, 50, "%I:%M %p", localtime(&now));
14 string strTime = cptime;
15 return strTime;
16}

BAF

You could just "return string(cptime);" to shorten it by another line of code.

CGamesPlay

Or just return cptime and let the compiler convert.

Michael Jensen

dunno what cptime is or how it's formatted, but this is working in msvc/mingw/linux-g++ so I'm happy. ;D

Thomas Fjellstrom

cptime is your variable :P sheesh man.

Michael Jensen

oh, so it is. ::) ;D

Thread #593088. Printed from Allegro.cc