Public Group

# Time and Date Questions

This topic is 5127 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey, I'm having problems with getting the time and date for timestamping. I'm trying to have it get the day and time, and put it in (DD, MM, YY, h : m : s, U) where h = hour, m = minute, and s = seconds, DD = Day, MM = Month, YY = Year, and U = User sent (I can do that part.). I'm thinking I need to #include <ctime>, but I can't figure out where to go from there (AKA, which functions to use). So, the question is : What functions would I use to get the exact date and time? (Including Day, Month, Year, Hour, Minute, and Second?) Any help would be greatly appreciated, thank you. P.S. Sorry for this stupid question.

##### Share on other sites
time() and localtime() to get the current time.
asctime() or strftime() to format it.

##### Share on other sites
Alright, I'm looking at time.h at the localtime() function. This confuses me, lol.

localtime(const time_t *);

So..what would I put in there for accurate results? (I don't understand this const time_t * part) Also, I looked at strftime, and wow. No one here has ever been as lost as I (I don't believe).
Right now, i'm trying localtime(NULL).

strftime(char *, size_t, const char *,
const struct tm *);

Er..any help would be appreciated.

The newb,

-Lenox

##### Share on other sites
struct time_t now;time(&now);struct tm* ptm = localtime(&now);char text[64];/* Either */char* tmp = asctime(ptm);int n = strncpy(text, tmp, 64);// text[n] = '\0';     (BAD, SEE FOLLOW-UP)/* Or */int n = strftime(text, 64, "%x", ptm);// text[n] = '\0';     (BAD, SEE FOLLOW-UP)

[Edited by - Fruny on January 7, 2005 10:09:29 PM]

##### Share on other sites
Thanks, now I just have to get rid of "cannot convert parameter 1 from 'struct main::time_t ** ' to 'long *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast" and i'll be on my way. Thank you very much :P

Also, what are the valid options for the format?

##### Share on other sites
On my system, time() and localtime() both take a struct time_t*. Check your platform's documentation.

##### Share on other sites
Ah, there's the problem. It takes const time_t*. So, i'll just change this..and this..and there. Voila! Now it works. Now, one more question. What are the valid options for the format using strftime? Also, what exactly is the point for the \0? I'm not exactly clear on that. (Hey, i'm learning. ^_^)

The newb,

-Lenox

##### Share on other sites
Quote:
 Original post by LenoxWhat are the valid options for the format using strftime?

I don't want to reproduce the whole documentation here. It's easy enough to find.

Quote:
 Also, what exactly is the point for the \0? I'm not exactly clear on that.

The buffer has a limited size. strncpy() and strftime() both take length parameters to avoid buffer overflows (i.e. writing more characters than you have room for). However, if they happen to write exactly the maximum number of characters, the null byte that is required at the end of every C string is not written (at least, with strncpy(), I'd need to check for strftime()).

Given that they both return the number of characters actually written, I just write a '\0' in the array at that position, terminating the string.

... and actually, there's an error in my code, so... the correct version would be:

struct time_t now;time(&now);struct tm* ptm = localtime(&now);char text[64];/* Either */char* tmp = asctime(ptm);int n = strncpy(text, tmp, 64);if(n == 64) text[63] = '\0';/* Or */int n = strftime(text, 64, "%x", ptm);if(n == 64) text[63] = '\0';

##### Share on other sites
Not. Now it just produces an error. "cannot convert from 'char [64]' to 'char'". Yet, with the [64] it gives me a weird symbol.
The newb,

-Lenox

[Edited by - Lenox on January 7, 2005 11:00:48 PM]

##### Share on other sites
Quote:
 Original post by LenoxGah, now it just produces a weird symbol. CURSE YOU C/C++!!

Ok, show me your code, and tell me what compiler you're using.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 34
• 16
• 11
• 11
• ### Forum Statistics

• Total Topics
634122
• Total Posts
3015646
×