# Time and Date Questions

This topic is 4792 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.

##### Share on other sites
Well, here's what I have now. (I'm using Microsoft Visual C++ 6.0).

char Timestamp(){	time_t now;	time(&now);	struct tm* ptm = localtime(&now);	char text[64];	int n = strftime(text, 64, "%H:%M:%S %p - %m/%d/%Y", ptm);	if(n == 64)		text[63] = '\0';	return text;}

But, with text[64], it'll produce the weird symbol.

[Edited by - Lenox on January 8, 2005 12:58:58 AM]

##### Share on other sites
a) You're returning a single char, while text is an array. The character returned is the low order byte of the array address.
b) Even if you were returning a char*, you would be in trouble, since text ceases to exist when the function return. Returning a pointer to a local variable is always a bad idea.
c) text[64] is not part of the array (that was my earlier error). Only indices from 0 to 63.

If you are using C, consider doing something like this:
size_t Timestamp(char* buffer, size_t length){  struct time_t now;  time(&now);  struct tm* ptm = localtime(&now);  size_t n = strftime(buffer, length, "%H:%M:%S %p - %m/%d/%Y", ptm);  if(n == length) buffer[length-1] = '\0';  return n;}

And pass it an appropriate buffer.

##### Share on other sites
Aiight, thanks Fruny. I just had to alter the code you posted just now a bit, because I saw something that I didn't think was right. Well, you put text[length - 1], when text didn't exist at all. And, where I had "Text" before, you put buffer, so, I thought you meant buffer[length - 1], so I changed that..Please correct me if i'm wrong though.

Thank you much,

--Lenox.

##### Share on other sites
Copy-paste error. Fixed now.

##### Share on other sites
Alright, thank you for your help, and I am very sorry for my errors, but I am still a newbie, and like the saying goes, "Not everyone is perfect." :P. I will learn one of these days.

--Lenox