Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Linux vs. _strdate _strtime

This thread is locked; no one can reply to it. rss feed Print
Linux vs. _strdate _strtime
Michael Jensen
Member #2,870
October 2002
avatar

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
Member #476
June 2000
avatar

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.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Kitty Cat
Member #2,815
October 2002
avatar

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.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

Thomas Fjellstrom
Member #476
June 2000
avatar

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

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Michael Jensen
Member #2,870
October 2002
avatar

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
Member #476
June 2000
avatar

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;

:)

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Michael Jensen
Member #2,870
October 2002
avatar

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
Member #476
June 2000
avatar

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

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Michael Jensen
Member #2,870
October 2002
avatar

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
Member #2,981
December 2002
avatar

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

CGamesPlay
Member #2,559
July 2002
avatar

Or just return cptime and let the compiler convert.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Michael Jensen
Member #2,870
October 2002
avatar

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
Member #476
June 2000
avatar

cptime is your variable :P sheesh man.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Michael Jensen
Member #2,870
October 2002
avatar

oh, so it is. ::) ;D

Go to: