Jump to content
  • Advertisement
Sign in to follow this  
Lenox

Time and Date Questions

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

If you intended to correct an error in the post then please contact us.

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 this post


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

Share this post


Link to post
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 this post


Link to post
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)




Adjust the format string in strftime to your liking.

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

Share this post


Link to post
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 this post


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

Share this post


Link to post
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. ^_^)

Thank you for your help,



The newb,




-Lenox

Share this post


Link to post
Share on other sites
Quote:
Original post by Lenox
What 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 this post


Link to post
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 this post


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


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!