Time stamp for log file

This is a discussion on Time stamp for log file within the C++ Programming forums, part of the General Programming Boards category; I need to add timestamp on my log file that tells the time when the log file is written. I'm ...

  1. #1
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696

    Time stamp for log file

    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

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    44
    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:
    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;
    }
    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.

    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 02:38 AM.
    Check out all my dimensions:
    Height, width, and for a limited time only... Depth!
    -sb

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    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

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    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

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    44
    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 11:24 AM.
    Check out all my dimensions:
    Height, width, and for a limited time only... Depth!
    -sb

  6. #6
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    you're guys awesome!!!

  7. #7
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    After reading, I got confused, though. Will these codes do it?
    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;
    }
    1) char fmt = %B%d + ', ' + %Y + " " + %H%M%S;?????
    I don't think so, or I'm not sure
    2) the syntax says that "now" is type of "struct tm"
    I'm lost!!!

  8. #8
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 08:51 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  5. file & linked list run time error
    By Micko in forum C Programming
    Replies: 9
    Last Post: 03-06-2004, 02:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21