/*--------------------------------------------------------------------------*/ /* util.c */ /*--------------------------------------------------------------------------*/ /* 3/27/95 util.c sub-routines for kw2asc.c */ #include "kw2asc.h" //....................... // index // void CK2Time(unsigned long rawTime) // int IsLeapYear(int year) // void Seconds2Time(unsigned long time, int *iyear, int *iday_of_year, int *ihour, // int *imin, int *isec) // void Day2Month(int year, int day_of_year, int *month, int *day_of_month) // int swap2(int); // long swap4i(long); // float swap4r(float); // float ad_val(long); //....................... /*---------------------------------------------------------*/ void CK2Time(unsigned long rawTime) { Seconds2Time(rawTime, &m_nYear, &m_nDayOfYear, &m_nHour, &m_nMin, &m_nSec); Day2Month(m_nYear, m_nDayOfYear, &m_nMonth, &m_nDayOfMonth); } /*---------------------------------------------------------*/ // Algorithms below were taken from K2 code, file TIME.C /*---------------------------------------------------------*/ static const int mon[] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; /* January = 1, take out __far */ int IsLeapYear(int year) { /* Returns 1 if year is a leap year Leap years are year divisible by 4 except century years. Leap years are century years divisible by 400. */ return (((year%100 == 0) && (year%400 == 0)) || // century & /400 ((year%100 != 0) && (year%4 == 0))); // not century & /4 } /*---------------------------------------------------------*/ void Seconds2Time(unsigned long time, int *iyear, int *iday_of_year, int *ihour, int *imin, int *isec) { /* Converts seconds since Jan. 1, 1980 to calendar date and time This algorithm is good for years between 1980 and 2100. (Year 2100 is not a leap year and requires special handling.) */ *isec = (int)(time % 60L); time /= 60L; /* time in minutes */ *imin = (int)(time % 60L); time /= 60L; /* time in hours */ *ihour = (int)(time % 24L); time /= 24L; /* time in days */ *iyear = 1980 + (int)((time/1461L) << 2); /* 1461 days in 4 years */ time %= 1461L; if (time >= 366) { time -= 366; (*iyear)++; *iyear += (int)(time / 365L); time %= 365L; } *iday_of_year = (int)(time+1); /* add 1 since day 1 == Jan. 1 */ } /*---------------------------------------------------------*/ void Day2Month(int year, int day_of_year, int *month, int *day_of_month) { /* * Given year and day of year, calculates month and day of month * day_of_year = 1 for January 1 * *month = 1, *day_of_month = 1 for January 1 */ int i; for ( ; ; (year)++) { if (IsLeapYear(year)) { if (day_of_year > 60) day_of_year--; else if (day_of_year == 60) { *month = 2; *day_of_month = 29; return; } } for (i=1; i <= 12; i++) { if (day_of_year <= mon[i]) { *day_of_month = day_of_year; *month = i; return; } day_of_year -= mon[i]; } } } /*-----------------------------------------*/ // ................................... // // int swap2(int w) // // swaps order of bytes: 12 -> 21 // // ................................... int swap2(int w) { union { unsigned char t[2]; int w; } tempi; unsigned char tempb; tempi.w = w; tempb = tempi.t[0]; tempi.t[0] = tempi.t[1]; tempi.t[1] = tempb; return tempi.w; } // swap2() /*------------------------------------*/ // ................................... // // long swap4i(long li) // // swaps order of 4 byte long 1234 -> 4321 // // ................................... long swap4i(long li) { union { unsigned char t[4]; long l; } templ; unsigned char tempb; templ.l = li; tempb = templ.t[0]; templ.t[0] = templ.t[3]; templ.t[3] = tempb; tempb = templ.t[1]; templ.t[1] = templ.t[2]; templ.t[2] = tempb; return templ.l; } // swap4i() /*--------------------------------------*/ // ................................... // // float swap4r(float re) // // swaps order of 4 byte float 1234 -> 4321 // // ................................... float swap4r(float re) { union { unsigned char t[4]; float f; } tempf; unsigned char tempb; tempf.f = re; tempb = tempf.t[0]; tempf.t[0] = tempf.t[3]; tempf.t[3] = tempb; tempb = tempf.t[1]; tempf.t[1] = tempf.t[2]; tempf.t[2] = tempb; return tempf.f; } // swap4r() /*-------------------------------------*/ // ..................................... // // float ad_val(long) // // converts sample to voltage // //....................................... float ad_val(long data) { float x; x = ((data*1.0)/8388608.0) * 2.5; // fullscale = +/- 2.5V return x; } // ad_val() // 2^23 = 8388608.0 /*--------------------------------------*/ /*--- end util.c -------------------------------------------------------*/