Thread: date-time related problem

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    39

    Question date-time related problem

    Hi all,

    I'm writing a function which extracts the day, month and year value from a pre-defined timestamp format passed by the caller. This format is: NDDD
    where N is the decade number after 2000 and DDD is the day of the year. so if the timestamp is: 1001. the year would be: 2001, month would be: January and day would 1 (corresponding to 1 Jan 2001).

    I've written this code for this:

    Code:
    int main( int argc, char **argv )
    {
       long       julian_day_num   = 1001;
       int        year_val         = 0;
       int        decade_indicator = 0;
       time_t     time_epoch       = 0;
    
       struct tm  tm_stru;
    
       memset( &tm_stru, 0, sizeof( tm_stru ) );
       errno = 0;
    
       decade_indicator = ( julian_day_num / 1000 );
       year_val = 2000 + decade_indicator;
    
       tm_stru.tm_year = year_val - 1900;
       tm_stru.tm_yday = julian_day_num - ( decade_indicator * 1000 );
       printf( "initial tm_yday  is : %d\n", tm_stru.tm_yday );
    
       time_epoch = mktime( &tm_stru );
    
       printf( "year_val is         : %d\n", year_val );
       printf( "decade_indicator is : %d\n", decade_indicator );
       printf( "tm_stru values after mktime() ....\n" );
       printf( "tm_yday    : %d\n", tm_stru.tm_yday );
       printf( "tm_year    : %d\n", tm_stru.tm_year );
       printf( "tm_mon     : %d\n", tm_stru.tm_mon  );
       printf( "tm_day     : %d\n", tm_stru.tm_mday );
    
       printf( "errno is: %d\n", errno );
    
    
       return 0;
    
    } /* end of main() */
    But this is giving me this result:
    Code:
    initial tm_yday  is : 1
    year_val is         : 2001
    decade_indicator is : 1
    tm_stru values after mktime() ....
    tm_yday    : 365
    tm_year    : 100
    tm_mon     : 11
    tm_day     : 31              
    errno is: 0
    Which is not what I was expecting. What am I missing here???

    Any help would be appreciated. Thanks.
    <Signature
    name="Ruchikar"
    quote="discussions are forgotten, only code remains"/>

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This format is: NDDD
    where N is the decade number after 2000 and DDD is the day of the year. so if the timestamp is: 1001. the year would be: 2001, month would be: January and day would 1 (corresponding to 1 Jan 2001).
    Um... no it's not. According to the above description, where N is the decade, the year should be: 2010.

    Decade = 10 years.
    N = 1, thus the year would be 2010.

    According to the formulae, DDD is not enough to hold the potential for 10 years worth of days. You need to correct your formulae:

    NNDDD

    or

    NDDDD

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. time synchronization problem
    By freeindy in forum C Programming
    Replies: 1
    Last Post: 04-19-2007, 06:25 AM
  3. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  4. Assingnment problem
    By Jason in forum C Programming
    Replies: 1
    Last Post: 08-31-2001, 01:20 PM