Logo Search packages:      
Sourcecode: freetds version File versions

TDS_INT tds_datecrack ( TDS_INT  datetype,
const void *  di,
TDSDATEREC dr 
)

Convert from db date format to a structured date format

Parameters:
datetype source date type. SYBDATETIME or SYBDATETIME4
di source date
dr destination date
Returns:
TDS_FAIL or TDS_SUCCEED

Definition at line 2749 of file convert.c.

References tdsdaterec::day, tdsdaterec::dayofyear, tdsdaterec::hour, tdsdaterec::millisecond, tdsdaterec::minute, tdsdaterec::month, tdsdaterec::second, tds_datecrack(), tdsdaterec::weekday, and tdsdaterec::year.

Referenced by tds_datecrack().

{

const TDS_DATETIME  *dt;
const TDS_DATETIME4 *dt4;

int dt_days;
unsigned int dt_time;

int years, months, days, ydays, wday, hours, mins, secs, ms;
int l,n,i,j;

      if ( datetype == SYBDATETIME ) {
            dt = (const TDS_DATETIME *) di;
            dt_time = dt->dttime;
            ms = ((dt_time % 300) * 1000) / 300 ;
            dt_time = dt_time / 300;
            secs = dt_time % 60;
            dt_time = dt_time / 60;
            dt_days = dt->dtdays;
      } 
      else if (datetype == SYBDATETIME4 ) {
            dt4 = (const TDS_DATETIME4 *) di;
            secs = 0;
            ms = 0;
            dt_days = dt4->days;
            dt_time = dt4->minutes;
      } 
      else
            return TDS_FAIL;

      /* -53690 is minimun  (1753-1-1) (Gregorian calendar start in 1732) 
       * 2958463 is maximun (9999-12-31) */
      l = dt_days + 146038;
      wday = ( l + 4 ) % 7;
      n = (4 * l ) / 146097; /* n century */
      l = l - ( 146097 * n + 3 ) / 4; /* days from xx00-02-28 (y-m-d) */
      i = ( 4000 * ( l + 1 ) ) / 1461001; /* years from xx00-02-28 */
      l = l - ( 1461 * i ) / 4; /* year days from xx00-02-28 */
      ydays = l >= 306 ? l - 305 : l + 60;
      l += 31;
      j = ( 80 * l ) / 2447;
      days = l - ( 2447 * j ) / 80;
      l = j / 11;
      months = j + 1 - 12 * l;
      years = 100 * ( n + 15 ) + i + l;
      if ( l == 0 && (years & 3) == 0 && (years % 100 != 0 || years % 400 == 0) ) 
            ++ydays;

      hours = dt_time / 60;
      mins  = dt_time % 60;

      dr->year        = years;
      dr->month       = months;
      dr->day         = days;
      dr->dayofyear   = ydays;
      dr->weekday     = wday;
      dr->hour        = hours;
      dr->minute      = mins;
      dr->second      = secs;
      dr->millisecond = ms;
      return TDS_SUCCEED;
}


Generated by  Doxygen 1.6.0   Back to index