Thread: Hi First time poster, big time problem with my program! Regarding epoch time conver

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    3

    Hi First time poster, big time problem with my program! Regarding epoch time conver

    Hi guys,

    First time poster, was hoping to get a bit of help with my code. Im wondering if this is a legal operation with a for loop.
    Code:
        edays = tv->tv_sec / 86400;                 // Turn into days since epoch
    
    
    
    
        fouryears = edays / days4years;
        remaindays = edays % days4years;            // Determines which 4 year perio
        tempdays = remaindays;
    
    
        for (monthindex = 0 ; tempdays <= 0 ; monthindex++)
        {
            daysgoneby += months[monthindex].day;   // Keeps track of days passed
    
    
            tempdays -= months[monthindex].day;
    
    
            if (monthindex > 11)                    // Resets counter for 12 months
            {
                monthindex = 0;
            }
            ++monthtracker;
        }
    Can I have a different paramter (in this case tempdays) in the for loop with the other parameter type ( monthindex) ? Also My loop is acting funny, this is just a partition of code.

    Question 2: Problem with the loop. Bascially tempdays has a positive value of 12011 or so and so it enters the forloop. This particular code/ program is called upon in a modulation setup by another program to provide the date and time. So on the first call from the main program, my month tracker value is 0, which means monthtracker gets initialized to 0, but does NOT go through the loop...as it does not get incremented properly. Meaning the loop must exit almost immediately? The next subsequent calls by the main program give my monthtracker values of -124959595 or so....basically nonsense and I can't figure it out. I've been on this for about 5 hours now and I'd really like to figure it out.

    If more clarification is needed i'll gladly post the entire program (just this one not the other calling program).

    Code:

    Code:
    #include <curses.h>
    #include <sys/time.h>
    #include <time.h>
    #include "fmttime.h"                    // Include for formattime interface
    #include <strstream>
    #include <iostream>
    #include <iomanip>
    
    
    static int monthindex;                  // Lookup table index
    static const int milli = 1000;          // Constant value for ms conversion
    static const int epochyear = 1970;      // Epoch year 1970
    static const int leapy = 4;             // Number of years for a leapyear
    using namespace std;
    
    
    struct Monthpairs                       // Fields for month & day lookup table
    {
        const char* mon;                    // Months
        int day;                            // Days
    };
    
    
    Monthpairs months[] =                   // Lookup table for months & days
    {
        {"Jan", 31},
        {"Feb", 28},
        {"Mar", 31},
        {"Apr", 30},
        {"May", 31},
        {"Jun", 30},
        {"Jul", 31},
        {"Aug", 31},
        {"Sep", 30},
        {"Oct", 31},
        {"Nov", 30},
        {"Dec", 31},
    };
    
    
    // Structure which will contain the human readable local
    // time values
    struct ExpandedTime
    {
    
    
        int et_usec;        // Number of Milliseconds local time
        int et_sec;         // Number of Seconds local time
        int et_min;         // Number of minutes local time
        int et_hour;        // Number of hours local time
        int et_day;         // Day of the month local time
        int et_mon;         // Month of the year local time
        int et_year;        // Our current year (2013 present)
    
    
    };
    
    
    // Function prototype for Expanded time function
    
    
    ExpandedTime* localTime(struct timeval* tv, ExpandedTime* etime);
    
    
    // Expanded time will take the Epoch time and convert into days,
    // months years, hours, minutes etc and then store this back into
    // the Expanded Structure
    
    
    ExpandedTime* localTime(
                            struct timeval* tv,     // Pointer to timeval struct
                            ExpandedTime* etime     // '' '' to expandedtime strct
                            )
    {
        tzset();                                    // Corrects for timezone
    
    
        int epochT = (tv->tv_sec) - timezone;       // Epoch seconds with
        int epochUT = tv->tv_usec;                  // epochtime microseconds
        int edays;                                  // Days since epochtime
        int days4years = 1461;                      // Number of days in 4 years
        int fouryears;                              // Number of 4 year periods
        int remaindays;                             // Number of days leftover
        int tempdays;                               // Holds value of remaindays
        int monthtracker;                           // Number of TOTAL months passed
                                                    // since current 4 year period
        int daysgoneby;                             // Number of days passed
                                                    // since current 4 year period
    
    
        etime->et_usec = (epochUT/milli) % milli;   // Find the milliseconds
    
    
        etime->et_sec = epochT % 60;
        epochT /= 60;                               // Turn into minutes
    
    
        etime->et_min = epochT % 60;
        epochT /= 60;                               // Turn into hours
    
    
        if (localtime(&tv->tv_sec)->tm_isdst !=0)
            etime->et_hour = (epochT % 24) + 1;     // Hours with DST correction
        else
            etime->et_hour = (epochT % 24);
    
    
        edays = tv->tv_sec / 86400;                 // Turn into days since epochT
    
    
    
    
        fouryears = edays / days4years;
        remaindays = edays % days4years;            // Determines which 4 year perio
        tempdays = remaindays;
    
    
        for (monthindex = 0 ; tempdays <= 0 ; monthindex++)
        {
            daysgoneby += months[monthindex].day;   // Keeps track of days passed
    
    
            tempdays -= months[monthindex].day;
    
    
            if (monthindex > 11)                    // Resets counter for 12 months
            {
                monthindex = 0;
            }
            ++monthtracker;
        }
        etime->et_day = tempdays + months[monthsindex];
    
    
    // This will take the number of months passed in current 4 year period
    // and add it to the epoch year of 1970 to find the current year
    
    
        etime->et_year = (monthtracker % 12)  + epochyear;
        return etime;
    
    
    }
    
    
    // Formats epoch time passed from a main function to a
    // human readable string
    
    
    char* formatTime(
                    struct timeval* tv,     // Pointer to timeval struct
                    char* buf,              // Pointer to char buf
                    size_t len              // size of buffer
                    )
    {
        struct ExpandedTime etime2;         // Struct object declaration
    
    
    
    
        // Array containing strings for the months
    
    
    /*    const char* month[] =
            {NOT USED OLD CODE BUT LEFT IN JUST IN CASE
            "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
            "Aug", "Sep", "Oct", "Nov", "Dec"
            };*/
    
    
    
    
        if (len > 0)                        // Will only print to valid sized buffer
        {
            localTime(tv, &etime2);
            ostrstream oss(buf, len);
    
    
            // Prints the time and date into a human readable string and
            // places it in the supplied buffer from plot.cc
    
    
            oss << etime2.et_year << " " <<  months[monthindex].mon << " " <<
            etime2.et_day << " " << setw(2) << etime2.et_hour << ":"
            << setfill('0') << setw(2) << etime2.et_min << ":" << setfill('0')
            << setw(2) << etime2.et_sec << ":" << setfill('0') << setw(3) <<
             etime2.et_usec << ends;
        }
    
    
        else if (len <= 0)
        {
            cout << "Sorry the length of the buffer is too small cannot write" << en
        }
    
    
        return buf;
    }
    *EDIT* Sorry I just realized I have the operator in the for loop backwards...should be > 0. My question regarding that forloop still stands however, but I need to test if this might resolve my problem. Thanks for looking. If one could check that the logic of finding the year and month using my lookup table is sound I'd be very appreciative. Thanks again!
    Last edited by RyanKim; 04-17-2013 at 12:11 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. operator!= and primary-expression error : First time poster
    By kentrenholmpei in forum C++ Programming
    Replies: 2
    Last Post: 11-04-2011, 06:24 AM
  2. Replies: 23
    Last Post: 05-22-2011, 11:20 PM
  3. time program showing time since epoch?
    By cus in forum Linux Programming
    Replies: 5
    Last Post: 01-10-2009, 01:56 PM
  4. Epoch and quantum and time slices ??
    By pritesh in forum Linux Programming
    Replies: 0
    Last Post: 02-18-2002, 08:55 AM