/*
 * Code taken from NSD 3.2.5, which is
 * code adapted from Python 2.4.1 sources (Lib/calendar.py).
 */
static time_t
mktime_from_utc(const struct tm *tm)
{
    int year = 1900 + tm->tm_year;
    time_t days = 365 * ((time_t) (year - 1970)) +
        ((time_t) leap_days(1970, year));
    time_t hours;
    time_t minutes;
    time_t seconds;
    int i;

    for (i = 0; i < tm->tm_mon; ++i) {
        days += mdays[i];
    }
    if (tm->tm_mon > 1 && is_leap_year(year)) {
        ++days;
    }
    days += tm->tm_mday - 1;

    hours = days * 24 + tm->tm_hour;
    minutes = hours * 60 + tm->tm_min;
    seconds = minutes * 60 + tm->tm_sec;

    return seconds;
}
Esempio n. 2
0
/*
 * Code adapted from Python 2.4.1 sources (Lib/calendar.py).
 */
time_t nlnet_timegm(const struct tm *tm) {
	int year;
	time_t days;
	time_t hours;
	time_t minutes;
	time_t seconds;

	year = 1900 + tm->tm_year;
	days = 365 * (year - 1970) + leap_days(1970, year);
	days += monoff[tm->tm_mon];

	if (tm->tm_mon > 1 && is_leap_year(year))
		++days;
	days += tm->tm_mday - 1;

	hours = days * 24 + tm->tm_hour;
	minutes = hours * 60 + tm->tm_min;
	seconds = minutes * 60 + tm->tm_sec;

	return seconds;
}
Esempio n. 3
0
/*+++------------------------------------------------------------------------
NAME
  epoch2isotime --- convert IsotimeEpoch to isotime string

SYNOPSIS

  const char * epoch2isotime( char buffer[], size_t buflen, IsotimeEpoch epoch )

DESCRIPTION

  (not implemented)

RETURN VALUE

  pointer to isotime string in buffer

----------------------------------------------------------------------------*/
const char * epoch2isotime( char buffer[], size_t buflen, IsotimeEpoch epoch )
{ char epobuf[EPOLEN]; 
  char osign_c;
  long year=0, month=0, day=0;
  long hh=0, mm=0, ss=0;
  double fract=0.0;
  long Hh=0, Mm=0, Ss=0;

  long days, yd, ts, tz, leap;

  long base;

  if (ISOTIME_debug>0) fprintf(stderr,"epoch2isotime %s BEGIN\n",
     epoch2string( epobuf, EPOLEN, epoch ) );

  if ( (!buffer)||(buflen<ISOLEN) ) {
    if (ISOTIME_debug>0) fprintf(stderr,"epoch2isotime END\n");
    return ( ( const char *) NULL );
  }

  buffer[0] ='\0';

  if (!epoch.status) {
    // normalize fract and sec
    base = floor(epoch.fract);
    epoch.fract -= (double) base;
    epoch.sec   += base;

    // convert2time
    fract = epoch.fract;

    if (epoch.offset<0) osign_c='-'; else osign_c='+';
    tz    = epoch.offset; 
    Hh    = floor(tz/3600);
    tz   -= Hh*3600;
    Mm    = floor(tz/60);
    tz   -= Mm*60;
    Ss    = tz;

    Hh=labs(Hh); Mm=labs(Mm); Ss = labs(Ss);

    if (ISOTIME_debug>3)
      fprintf(stderr," osign=%c1, Hh=%ld, Mm=%ld, Ss=%ld\n",osign_c,Hh,Mm,Ss);

    ts    = epoch.sec + epoch.offset; // add time zone offset

    days  = floor( ts/3600/24 );

    ts   -= days*3600*24;

    if (ts<0) { days-=1; ts+=3600*24; }

    hh    = floor(ts/3600);
    ts   -= hh*3600;
    mm    = floor(ts/60);
    ts   -= mm*60;
    ss    = ts;

    days += DAYS_19700101; // == year*365+leap_days

    year  = floor( days/365 );
    yd    = days - year*365 - leap_days( year ); // day in the year

    if (ISOTIME_debug>3)
      fprintf(stderr," hh=%ld, hm=%ld, ss=%ld\n",hh,mm,ss);

    // days contains all leap_days => year is correct or too high 
    while ( yd <= 0 ) {
      year--;
      yd    = days - year*365 - leap_days( year ); // day in the year
    }

    if (ISOTIME_debug>3)
      fprintf(stderr," year=%ld, yd=%ld\n",year,yd);

    // get number of leap days in the year
    if ( leap_year ( year ) ) leap=1; else leap=0;

         if (yd<=31)       { month= 1; day=yd; }          // Jan
    else if (yd<=59+leap)  { month= 2; day=yd-31; }       // Feb 
    else if (yd<=90+leap)  { month= 3; day=yd-59-leap; }  // Mar
    else if (yd<=120+leap) { month= 4; day=yd-90-leap; }  // Apr
    else if (yd<=151+leap) { month= 5; day=yd-120-leap; } // May
    else if (yd<=181+leap) { month= 6; day=yd-151-leap; } // Jun
    else if (yd<=212+leap) { month= 7; day=yd-181-leap; } // Jul
    else if (yd<=243+leap) { month= 8; day=yd-212-leap; } // Aug
    else if (yd<=273+leap) { month= 9; day=yd-243-leap; } // Sep
    else if (yd<=304+leap) { month=10; day=yd-273-leap; } // Oct
    else if (yd<=334+leap) { month=11; day=yd-304-leap; } // Nov
    else                   { month=12; day=yd-334-leap; } // Dec

    // print
    switch (ISOTIME_mode) {
      case IsotimeModeSpace:
        sprintf(buffer,"%04ld-%02ld-%02ld %02ld:%02ld:%02ld.%06ld %c%02ld%02ld",
                year,month,day,hh,mm,ss,(long) floor(epoch.fract*1e6+0.5),
                osign_c,labs(Hh),labs(Mm) );
        break;
      default: // IsotimeModeNoSpace
        sprintf(buffer,"%04ld-%02ld-%02ldT%02ld:%02ld:%02ld.%06ld%c%02ld%02ld",
             year,month,day,hh,mm,ss,(long) floor(epoch.fract*1e6+0.5),
             osign_c,labs(Hh),labs(Mm) );
    }
  }

  if (ISOTIME_debug>0) fprintf(stderr,"epoch2isotime %s END\n",buffer);

  return( buffer );

} // epoch2isotime
Esempio n. 4
0
/*--------------------------------------------------------------------------*/
IsotimeEpoch _convert2epoch(long year, long month, long day, 
                            long hh, long mm, long ss, double uuuuuu, 
                            long osign, long Hh, long Mm, long Ss) 
{ long days, base;
  IsotimeEpoch epoch;

  if (ISOTIME_debug>0) fprintf(stderr,"_convert2epoch BEGIN\n");

  epoch.offset = (long int) 0;
  epoch.fract  = (double) 0.0;
  epoch.sec    = (long int) 0;

  epoch.status = -1;

  days = year*365+leap_days(year) - DAYS_19700101;

  switch (month) {
    case  1: days+=day; break;
    case  2: days+=day+31; break;
    case  3: days+=day+59; break;
    case  4: days+=day+90; break;
    case  5: days+=day+120; break;
    case  6: days+=day+151; break;
    case  7: days+=day+181; break;
    case  8: days+=day+212; break;
    case  9: days+=day+243; break;
    case 10: days+=day+273; break;
    case 11: days+=day+304; break;
    case 12: days+=day+334; break;
    default : month=0; // invalid
  }

  if ( month ) {

    // correct march to december leap years
    if ( leap_year ( year ) ) {
      if (month>2) days+=1;
    }

    epoch.offset = osign*( (Hh*60+Mm)*60+Ss );

    base = floor(uuuuuu);
    epoch.fract = uuuuuu - (double) base;
    epoch.sec   = ((days*24+hh)*60+mm)*60+ss+base-epoch.offset;

    // normalize fract and sec
    base = floor(epoch.fract);
    epoch.fract -= (double) base;
    epoch.sec   += base;

    epoch.status = 0; // success
  } 

  if (ISOTIME_debug>2)
    fprintf(stderr,"  %ld (DAYS_%04ld%02ld%02ld) - %ld (DAYS_19700101) = %ld\n",
      days+DAYS_19700101,year,month,day,DAYS_19700101,days);

  if (ISOTIME_debug>0) fprintf(stderr,"_convert2epoch END\n");

  return ( epoch );

} // _convert2epoch