I need to add timestamp on my log file that tells the time when the log file is written. I'm a beginning programmer so give me the least sophisticated solution if possible. thanx
I need to add timestamp on my log file that tells the time when the log file is written. I'm a beginning programmer so give me the least sophisticated solution if possible. thanx
Ok, the way you show time in c++ is number-of-seconds.
Say you wrote a program to output that number. The output would look like: 1062833516, a string of digits.
This is the number of seconds. Since when? Don't hold me to this, but I think it's some time back around 1970.
You have to do a lot of math to figure that out and display the time in an understandable format. Here it is in a format that I'm going to try to make easy to understand:
Ok, the first thing we need to do is ensure we're dealing with number of seconds past midnight. I'll explain why later. If now == 0 was the num-of-seconds value of some midnight than all would be good, but it's not. 18000 is the first recordable midnight by the "num-of-secs started at 0 back in 1970" method, so I use that one.Code:#include <time.h> int main() { time_t now; //declares num-of-secs storage place time(&now); //puts the current num-of-secs in 'now' int clock = now - 18000; int secsPastMidnight = clock % 86400; int hours = (secsPastMidnight / 3600); int minutes = (secsPastMidnight % 3600) / 60; int seconds = secsPastMidnight % 60; return 0; }
By subtracting that from the current num-of-secs value you have num-of-secs value that you can divide by 86400 (the number of seconds in a day) and the remainder will be the number of seconds since last midnight.
You can divide that number by 3600 (the number of seconds in an hour) and get the number of hours since midnight.
You can divide the remainder of that by 60 and get the number of minutes into that hour.
Finally, the remainder of that is the number of seconds into that minute. I've simplified this step to equal the number of seconds past the last completed minute.
It's possible an hour could be misplaced because of daylight savings time, or that the midnight value I used is not the same on your machine. I haven't included any corrections for calendar related changes to the time. You may have to develop these yourself. I wrote a rather lengthy program that includes corrections for all the calendar alterations I could think of, and your welcome to it, if you want, but it sounds like you're just looking for time of day, and not an actual date. My program is very hard to understand due to a large amount of math and conditionals (that aren't commented - sorry).
Edit: Also VERY possible that the 18000 (any past midnight value will work in it's place) is dependant upon time zone. I use central standard time, so yours may be different. If you want, get a value equal to a midnight by observing the number of seconds in a test program and use it in 18000's place.
Last edited by Wick; 09-06-2003 at 01:38 AM.
Check out all my dimensions:
Height, width, and for a limited time only... Depth!
-sb
1) Actually, I need to have the date stamped also, so what addition do I need?
2) I suppose I could replace <time.h> with <time>?
3) Are "time_t" and "time" class objects from <time> library?
thnx a bunch
1) Look into the strftime function.
2) <ctime>, actually
3) time_t is actually an int or a long, and time is a function. Since they are standard C libraries, they cannot use classes.
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
haven't used strftime, but it's official. When I spend a lot of time on something, there is always an easier way.
time_t is just a datatype like he said, and time() is a function that sets the time_t value.
If you continue using my method, you'll have to find out when the start of each year is (in time_t terms). This is hard because years are not all the same length. Leapyears are every year divisible by 4 excluding years divisible by 100 but including years divisible by 400.
Finding the month is even more a headache because they are way un-uiniform. After Feb 28th everything depends on whether or not it's a leapyear.
Days aren't so bad. In fact, it's probably easier to use the number of days into the year to find the month.
Edit: looks like the strftime() code does the same math I've been doing for myself.
Last edited by Wick; 09-06-2003 at 10:24 AM.
Check out all my dimensions:
Height, width, and for a limited time only... Depth!
-sb
After reading, I got confused, though. Will these codes do it?
1) char fmt = %B%d + ', ' + %Y + " " + %H%M%S;?????Code:#include <time.h> int main() { time_t now; //declares num-of-secs storage place time(&now); //puts the current num-of-secs in 'now' const size_t maxsize = 20; char output; char fmt = //question no.1 size_t strftime(&output, maxsize, &fmt, &now); cout << output; // print to screen return 0; }
I don't think so, or I'm not sure
2) the syntax says that "now" is type of "struct tm"
I'm lost!!!
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie