Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
Raghunathan
09/05/12 10:03
Read: 1514 times
Chennai
India


 
#188240 - Error in conversion from Unix EPOCH
Created this code based on a pseudo code from a Dallas application note to convert an unsigned long to year-month-date-time format. Its working but instead of the month showing as Sept it shows as October. Not sure where is the bug :

unsigned long set_Epoch_s( unsigned long year, unsigned long month, unsigned long day, unsigned long hour, unsigned long min, unsigned long sec) 
 {
  unsigned long DMonth[13] = { 0,31,59,90,120,151,181,212,243,273,304,334,365};
  unsigned long iday;
  unsigned long temp;

  iday = 365 * ( year - 1970) + DMonth[month] + ( day-1); // days since 1/1/1970
  iday = iday + ( year - 1969)/4;                         // + leap days since 1/1/1970
  temp = year % 4;
  if ( ( month > 2 ) && (temp ==0))                      // if leap year & past Feb..
     {
       iday = iday + 1;
     }
  return (sec +( 60 * ( min + (60 * ( hour + (24 * iday )))))); //Compute seconds since
 }

 


When I call the function with an argument of (2012, 9, 5, 17, 45, 0) I get a value of 1349459100 which is "Oct 5th 2012 , 17:45:00 " . All except the month are correct. So what is the mistake that I am doing ( apart from creating a struct for the argument ..;-)

Thanks

Raghu

List of 22 messages in thread
TopicAuthorDate
Error in conversion from Unix EPOCH       Raghunathan      09/05/12 10:03      
   zero        J. Guy      09/05/12 10:32      
   DMonth[month-1] ????        Stefan KAnev      09/05/12 11:50      
      Stefan is correct and...      Daniel Contarino      09/05/12 14:11      
         +4 - 100 + 400      Per Westermark      09/05/12 14:46      
            right, but...      Stefan KAnev      09/05/12 14:54      
               88 years is a long time      Per Westermark      09/05/12 14:55      
                  That is what they said in the 60's      Neil Kurzman      09/06/12 13:46      
      Thanks. Problem solved      Raghunathan      09/05/12 22:26      
         Cross-checking important      Per Westermark      09/06/12 00:10      
            Just a foot note about the year      Per Westermark      09/06/12 03:00      
               ?back conversion      Stefan KAnev      09/06/12 04:56      
                  No - multiplier should not be 366      Per Westermark      09/06/12 05:21      
                     Running out of seconds > 1970      David Prentice      09/06/12 10:00      
                        signed is actually common - to support dates before 1970      Per Westermark      09/06/12 12:42      
                        Will You Now.      Neil Kurzman      09/06/12 13:53      
                           Have you considered leap seconds?      KONSTANTINOS L. ANGELIS      09/07/12 05:00      
                              Leap seconds can almost always be ignored        Per Westermark      09/07/12 05:37      
                                 Time is passing anyway or is it just an illusion?      KONSTANTINOS L. ANGELIS      09/07/12 07:00      
                                    Missiles? Leap seconds contra way larger drift...      Per Westermark      09/07/12 07:45      
         No such thing as a free lunch!      Andy Neil      09/07/12 15:28      
   Link?      Andy Neil      09/08/12 03:16      

Back to Subject List