dcsimg
Results 1 to 3 of 3

Thread: Getting the elapsed number of days

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Getting the elapsed number of days

    Hello,

    Code:
    gcc 4.7.2
    c89
    I have developed a sample program that should get the difference between a start time and an end time. I then use the seconds to calculate how many days has elapsed between those 2 times.

    This is for a logging application that after a user specified number of days. The logs will be rolled over and a new start time will be set. The user can decide how many days the log will be replaced.

    I added a small sleep to get the general idea, but normally this will be logging for days.

    Is this the best and most portable (linux/windows) way to do this? Any potential pitfalls in this design?

    Many thanks for any suggestions,

    Code:
    #include <time.h>
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
        time_t start_timestamp;
        time_t end_timestamp;
        struct tm time_days;
        double seconds = 0;
        size_t cumulative_days = 0;
    #define FORMAT_TIME_SIZE 64
        char format_time[FORMAT_TIME_SIZE];
    
    #define DAY 3600     /* Seconds in one day */
    #define MAX_DAYS 10  /* Roll over log after 10 days */
    
        /* Current time of starting application */
        time(&start_timestamp);
        time_days = *localtime(&start_timestamp);
    
        /* Print the current time at the start */
        memset(format_time, 0, sizeof format_time);
        strftime(format_time, sizeof format_time, "%c", &time_days);
        printf("timestamp start [ %s ]\n", format_time);
    
        /* Simulate a simple sleep. This could be logging for many days */
        sleep(10);
    
        /* Current time after delay print the results */
        time(&end_timestamp);
        time_days = *localtime(&end_timestamp);
        memset(format_time, 0, sizeof format_time);
        strftime(format_time, sizeof format_time, "%c", &time_days);
        printf("timestamp end [ %s ]\n", format_time);
    
        /* Get the current difference in seconds */
        seconds = difftime(end_timestamp, start_timestamp);
        printf("Seconds elapsed [ %f ]\n", seconds);
    
        /* Calculate how many days have elapsed */
        cumulative_days = DAY * seconds;
    
        /* What are we going to do */
        if(cumulative_days <= MAX_DAYS) {
            printf("Delete log and start over\n");
        }
        else {
            printf("Not there yet, keep appending the current log\n");
        }
    
        return 0;
    }
    steve

  2. #2
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Getting the elapsed number of days

    some bits don't look right...
    Code:
    #define HOUR 3600     /* Seconds in one hour */
    #define DAY 86400     /* Seconds in one day */
    
        /* Get the current difference in seconds */
        seconds = difftime(end_timestamp, start_timestamp);
        printf("Seconds elapsed [ %f ]\n", seconds);
    
        /* Calculate how many days have elapsed */
        cumulative_days = seconds / DAY; //number of days rounded down
        cumulative_days = (seconds + DAY - 1) / DAY; //number of days rounded up
    W o t . S i g

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: Getting the elapsed number of days

    Thanks for your reply.

    I will give that a try and let you know how things turn out.
    steve

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width