IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 6, 2004, 2:06:06 PM (22 years ago)
Author:
desonia
Message:

another attempt to get astyle to get it right.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/astro/psTime.c

    r1406 r1407  
     1
    12/** @file  psTime.c
    23 *
     
    1213 *  @author Ross Harman, MHPCC
    1314 *
    14  *  @version $Revision: 1.13 $ $Name: not supported by cvs2svn $
    15  *  @date $Date: 2004-08-06 22:34:05 $
     15 *  @version $Revision: 1.14 $ $Name: not supported by cvs2svn $
     16 *  @date $Date: 2004-08-07 00:06:06 $
    1617 *
    1718 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
    1819 */
    1920
    20 
    2121/******************************************************************************/
     22
    2223/*  INCLUDE FILES                                                             */
     24
    2325/******************************************************************************/
    2426
     
    3234
    3335/******************************************************************************/
     36
    3437/*  DEFINE STATEMENTS                                                         */
     38
    3539/******************************************************************************/
    3640
     
    7377}                                                                                                            \
    7478
     79
    7580/******************************************************************************/
     81
    7682/*  TYPE DEFINITIONS                                                          */
     83
    7784/******************************************************************************/
    7885
     
    8087
    8188/*****************************************************************************/
     89
    8290/*  GLOBAL VARIABLES                                                         */
     91
    8392/*****************************************************************************/
    8493
     
    8695
    8796/*****************************************************************************/
     97
    8898/*  FILE STATIC VARIABLES                                                    */
     99
    89100/*****************************************************************************/
    90101
     
    119130
    120131// Table for Julian date of leapsecond update and current total number of leapseconds at that date
    121 static double leapseconds[NUM_LEAPSECOND_UPDATES][2] =
    122     {
    123         {
    124             2441317.5, 10.0
    125         },
    126         {
    127             2441499.5, 11.0
    128         },
    129         {
    130             2441683.5, 12.0
    131         },
    132         {
    133             2442048.5, 13.0
    134         },
    135         {
    136             2442413.5, 14.0
    137         },
    138         {
    139             2442778.5, 15.0
    140         },
    141         {
    142             2443144.5, 16.0
    143         },
    144         {
    145             2443509.5, 17.0
    146         },
    147         {
    148             2443874.5, 18.0
    149         },
    150         {
    151             2444239.5, 19.0
    152         },
    153         {
    154             2444786.5, 20.0
    155         },
    156         {
    157             2445151.5, 21.0
    158         },
    159         {
    160             2445516.5, 22.0
    161         },
    162         {
    163             2446247.5, 23.0
    164         },
    165         {
    166             2447161.5, 24.0
    167         },
    168         {
    169             2447892.5, 25.0
    170         },
    171         {
    172             2448257.5, 26.0
    173         },
    174         {
    175             2448804.5, 27.0
    176         },
    177         {
    178             2449169.5, 28.0
    179         },
    180         {
    181             2449534.5, 29.0
    182         },
    183         {
    184             2450083.5, 30.0
    185         },
    186         {
    187             2450630.5, 31.0
    188         },
    189         {
    190             2451179.5, 32.0
    191         }
    192     };
    193 
    194 /*****************************************************************************/
     132static double leapseconds[NUM_LEAPSECOND_UPDATES][2] = {
     133            {
     134                2441317.5, 10.0},
     135            {
     136                2441499.5, 11.0},
     137            {
     138                2441683.5, 12.0},
     139            {
     140                2442048.5, 13.0},
     141            {
     142                2442413.5, 14.0},
     143            {
     144                2442778.5, 15.0},
     145            {
     146                2443144.5, 16.0},
     147            {
     148                2443509.5, 17.0},
     149            {
     150                2443874.5, 18.0},
     151            {
     152                2444239.5, 19.0},
     153            {
     154                2444786.5, 20.0},
     155            {
     156                2445151.5, 21.0},
     157            {
     158                2445516.5, 22.0},
     159            {
     160                2446247.5, 23.0},
     161            {
     162                2447161.5, 24.0},
     163            {
     164                2447892.5, 25.0},
     165            {
     166                2448257.5, 26.0},
     167            {
     168                2448804.5, 27.0},
     169            {
     170                2449169.5, 28.0},
     171            {
     172                2449534.5, 29.0},
     173            {
     174                2450083.5, 30.0},
     175            {
     176                2450630.5, 31.0},
     177            {
     178                2451179.5, 32.0}
     179        };
     180
     181/*****************************************************************************/
     182
    195183/*  FUNCTION IMPLEMENTATION - LOCAL                                          */
     184
    196185/*****************************************************************************/
    197186
     
    199188
    200189/*****************************************************************************/
     190
    201191/* FUNCTION IMPLEMENTATION - PUBLIC                                          */
     192
    202193/*****************************************************************************/
    203194
     
    210201    time.tv_usec = 0;
    211202
    212     if(gettimeofday(&now,(struct timezone *) 0) == -1) {
     203    if (gettimeofday(&now, (struct timezone *)0) == -1) {
    213204        psError(__func__, " : Line %d - Failed to get time", __LINE__);
    214205        return time;
    215206    }
    216 
    217207    // Convert timeval time to psTime
    218208    time.tv_sec = now.tv_sec;
     
    220210
    221211    // Add most current leapseconds value to UTC time to get TAI time
    222     time.tv_sec += leapseconds[NUM_LEAPSECOND_UPDATES-1][1];
     212    time.tv_sec += leapseconds[NUM_LEAPSECOND_UPDATES - 1][1];
    223213
    224214    return time;
    225215}
    226216
    227 char* psTimeToISO(psTime time)
     217char *psTimeToISO(psTime time)
    228218{
    229219    int ms = 0;
     
    232222    struct tm *tmTime = NULL;
    233223
    234     CHECK_NEGATIVE_TIME_STRUCT(time,NULL);
     224    CHECK_NEGATIVE_TIME_STRUCT(time, NULL);
    235225
    236226    tempString = psAlloc(MAX_TIME_STRING_LENGTH);
     
    238228
    239229    // Converts psTime to YYYY/MM/DD,HH:MM:SS.SSS in string form
    240     ms = time.tv_usec/1000;
     230    ms = time.tv_usec / 1000;
    241231
    242232    // tmTime variable is statically allocated, no need to free
    243233    tmTime = gmtime(&time.tv_sec);
    244     if(!strftime(tempString, MAX_TIME_STRING_LENGTH, "%Y/%m/%d,%H:%M:%S", tmTime)) {
     234    if (!strftime(tempString, MAX_TIME_STRING_LENGTH, "%Y/%m/%d,%H:%M:%S", tmTime)) {
    245235        psError(__func__, " : Line %d - Failed strftime conversion", __LINE__);
    246236    }
    247237
    248     if(snprintf(timeString,MAX_TIME_STRING_LENGTH,"%s.%3.3d", tempString, ms) < 0) {
     238    if (snprintf(timeString, MAX_TIME_STRING_LENGTH, "%s.%3.3d", tempString, ms) < 0) {
    249239        psError(__func__, " : Line %d - Failed snprintf conversion", __LINE__);
    250240    }
     
    262252    psTime outTime;
    263253
    264     CHECK_NEGATIVE_TIME_STRUCT(time,outTime);
     254    CHECK_NEGATIVE_TIME_STRUCT(time, outTime);
    265255
    266256    // Find leapseconds to subtract from psTime to get UTC time
    267257    jd = psTimeToJD(time);
    268258    jdTable = leapseconds[0];
    269     for(i=0; i<NUM_LEAPSECOND_UPDATES; i++, jdTable+=2) {
    270         if(jd > *jdTable) {
     259    for (i = 0; i < NUM_LEAPSECOND_UPDATES; i++, jdTable += 2) {
     260        if (jd > *jdTable) {
    271261            ls = *(jdTable + 1);
    272262        }
     
    276266    outTime.tv_usec = time.tv_usec;
    277267
    278     CHECK_NEGATIVE_TIME_STRUCT(outTime,outTime);
     268    CHECK_NEGATIVE_TIME_STRUCT(outTime, outTime);
    279269
    280270    return outTime;
     
    285275    double mjd = 0.0;
    286276
    287     CHECK_NEGATIVE_TIME_STRUCT(time,mjd);
     277    CHECK_NEGATIVE_TIME_STRUCT(time, mjd);
    288278
    289279    // Modified Julian date conversion courtesy of Eugene Magnier
    290     mjd = time.tv_sec/SEC_PER_DAY + time.tv_usec/USEC_PER_DAY + 40587.0;
     280    mjd = time.tv_sec / SEC_PER_DAY + time.tv_usec / USEC_PER_DAY + 40587.0;
    291281
    292282    return mjd;
     
    297287    double jd = 0.0;
    298288
    299     CHECK_NEGATIVE_TIME_STRUCT(time,jd);
     289    CHECK_NEGATIVE_TIME_STRUCT(time, jd);
    300290
    301291    // Julian date conversion courtesy of Eugene Magnier
    302     jd = time.tv_sec/SEC_PER_DAY + time.tv_usec/USEC_PER_DAY + 2440587.5;
     292    jd = time.tv_sec / SEC_PER_DAY + time.tv_usec / USEC_PER_DAY + 2440587.5;
    303293
    304294    return jd;
     
    309299    struct timeval timevalTime;
    310300
    311     CHECK_NEGATIVE_TIME_STRUCT(time,timevalTime);
     301    CHECK_NEGATIVE_TIME_STRUCT(time, timevalTime);
    312302    timevalTime.tv_sec = time.tv_sec;
    313303    timevalTime.tv_usec = time.tv_usec;
     
    316306}
    317307
    318 struct tm* psTimeToTM(psTime time)
     308struct tm *psTimeToTM(psTime time)
    319309{
    320310    struct tm *tmTime = NULL;
    321311
    322     CHECK_NEGATIVE_TIME_STRUCT(time,tmTime);
     312    CHECK_NEGATIVE_TIME_STRUCT(time, tmTime);
    323313    tmTime = gmtime(&time.tv_sec);
    324314
     
    343333    // Convert YYYY/MM/DD,HH:MM:SS.SSS in string form to tm time
    344334    year = atoi(strtok(tempString, "/"));
    345     if(year < 1900) {
    346         psError(__func__,"Years less than 1900 not allowed. Value: %d", year);
     335    if (year < 1900) {
     336        psError(__func__, "Years less than 1900 not allowed. Value: %d", year);
    347337        return outTime;
    348338    }
    349339
    350340    month = atoi(strtok(NULL, "/"));
    351     if(month<1 || month>12) {
    352         psError(__func__,"Month must have a value from 1 to 12. Value: %d", month);
     341    if (month < 1 || month > 12) {
     342        psError(__func__, "Month must have a value from 1 to 12. Value: %d", month);
    353343        return outTime;
    354344    }
    355345
    356346    day = atoi(strtok(NULL, ","));
    357     if(day<1 || day>31) {
    358         psError(__func__,"Day must have a value from 1 to 31. Value: %d", day);
     347    if (day < 1 || day > 31) {
     348        psError(__func__, "Day must have a value from 1 to 31. Value: %d", day);
    359349        return outTime;
    360350    }
    361351
    362352    hour = atoi(strtok(NULL, ":"));
    363     if(hour<0 || hour>23) {
    364         psError(__func__,"Hour must have a value from 0 to 23. Value: %d", hour);
     353    if (hour < 0 || hour > 23) {
     354        psError(__func__, "Hour must have a value from 0 to 23. Value: %d", hour);
    365355        return outTime;
    366356    }
    367357
    368358    minute = atoi(strtok(NULL, ":"));
    369     if(minute<0 || minute>59) {
    370         psError(__func__,"Minute must have a value from 0 to 59. Value: %d", minute);
     359    if (minute < 0 || minute > 59) {
     360        psError(__func__, "Minute must have a value from 0 to 59. Value: %d", minute);
    371361        return outTime;
    372362    }
    373363
    374364    second = atoi(strtok(NULL, "."));
    375     if(second<0 || second>59) {
    376         psError(__func__,"Second must have a value from 0 to 59. Value: %d", second);
     365    if (second < 0 || second > 59) {
     366        psError(__func__, "Second must have a value from 0 to 59. Value: %d", second);
    377367        return outTime;
    378368    }
    379369
    380370    millisecond = atoi(strtok(NULL, "X"));
    381     if(millisecond<0 || millisecond>1000) {
    382         psError(__func__,"Millisecond must have a value from 0 to 999. Value: %d", millisecond);
     371    if (millisecond < 0 || millisecond > 1000) {
     372        psError(__func__, "Millisecond must have a value from 0 to 999. Value: %d", millisecond);
    383373        return outTime;
    384374    }
     
    394384    // Convert tm time to psTime
    395385    outTime = psTMToTime(&tmTime);
    396     outTime.tv_usec = millisecond*1000;
     386    outTime.tv_usec = millisecond * 1000;
    397387
    398388    return outTime;
     
    403393    psTime outTime;
    404394
    405     CHECK_NEGATIVE_TIME_STRUCT(time,outTime);
     395    CHECK_NEGATIVE_TIME_STRUCT(time, outTime);
    406396
    407397    // Convert UTC time to psTime/TAI
    408     outTime.tv_sec = time.tv_sec + leapseconds[NUM_LEAPSECOND_UPDATES-1][1];
     398    outTime.tv_sec = time.tv_sec + leapseconds[NUM_LEAPSECOND_UPDATES - 1][1];
    409399    outTime.tv_usec = time.tv_usec;
    410     CHECK_NEGATIVE_TIME_STRUCT(outTime,outTime);
     400    CHECK_NEGATIVE_TIME_STRUCT(outTime, outTime);
    411401
    412402    return outTime;
     
    419409    double seconds = 0.0;
    420410
    421     CHECK_NEGATIVE_TIME(time,outTime);
     411    CHECK_NEGATIVE_TIME(time, outTime);
    422412
    423413    // Modified Julian date conversion courtesy of Eugene Magnier
     
    425415
    426416    // Convert to psTime/TAI
    427     seconds = days*SEC_PER_DAY;
    428     outTime.tv_usec = (seconds -(long)seconds)*1000000.0;
     417    seconds = days * SEC_PER_DAY;
     418    outTime.tv_usec = (seconds - (long)seconds) * 1000000.0;
    429419    outTime.tv_sec = seconds;
    430420
    431     CHECK_NEGATIVE_TIME_STRUCT(outTime,outTime);
     421    CHECK_NEGATIVE_TIME_STRUCT(outTime, outTime);
    432422
    433423    return outTime;
     
    440430    psTime outTime;
    441431
    442     CHECK_NEGATIVE_TIME(time,outTime);
     432    CHECK_NEGATIVE_TIME(time, outTime);
    443433
    444434    // Julian date conversion courtesy of Eugene Magnier
     
    446436
    447437    // Convert to psTime/TAI
    448     seconds = days*SEC_PER_DAY;
     438    seconds = days * SEC_PER_DAY;
    449439    outTime.tv_sec = seconds;
    450     outTime.tv_usec = (seconds -(long)seconds)*1000000.0;
    451 
    452     CHECK_NEGATIVE_TIME_STRUCT(outTime,outTime);
    453 
    454     return outTime;
    455 }
    456 
    457 psTime psTimevalToTime(struct timeval *time)
    458 {
    459     psTime outTime;
    460     if(time == NULL)
    461     {
    462         psError(__func__,"Null value for timeval arg not allowed");
    463         return outTime;
    464     } else
    465         if(time->tv_sec < 0)
    466         {
    467             psError(__func__,"Negative seconds are not allowed: %ld", time->tv_sec);
    468             return outTime;
    469         } else
    470             if(time->tv_usec<0)
    471             {
    472                 psError(__func__,"Negative microseconds are not allowed: %ld", time->tv_usec);
    473                 return outTime;
    474             }
    475 
     440    outTime.tv_usec = (seconds - (long)seconds) * 1000000.0;
     441
     442    CHECK_NEGATIVE_TIME_STRUCT(outTime, outTime);
     443
     444    return outTime;
     445}
     446
     447psTime psTimevalToTime(struct timeval * time)
     448{
     449    psTime outTime;
     450
     451    if (time == NULL)
     452    {
     453        psError(__func__, "Null value for timeval arg not allowed");
     454        return outTime;
     455    } else if (time->tv_sec < 0)
     456    {
     457        psError(__func__, "Negative seconds are not allowed: %ld", time->tv_sec);
     458        return outTime;
     459    } else if (time->tv_usec < 0)
     460    {
     461        psError(__func__, "Negative microseconds are not allowed: %ld", time->tv_usec);
     462        return outTime;
     463    }
    476464    // Convert to psTime/TAI
    477465    outTime.tv_sec = time->tv_sec;
    478466    outTime.tv_usec = time->tv_usec;
    479467
    480     CHECK_NEGATIVE_TIME_STRUCT(outTime,outTime);
    481 
    482     return outTime;
    483 }
    484 
    485 
    486 psTime psTMToTime(struct tm *time)
     468    CHECK_NEGATIVE_TIME_STRUCT(outTime, outTime);
     469
     470    return outTime;
     471}
     472
     473psTime psTMToTime(struct tm * time)
    487474{
    488475    int i;
    489476    int n;
    490477    int y;
    491     int mon [] =
    492         {
    493             31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
    494         };
     478    int mon[] = {
     479                    31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
     480                };
    495481    long epoch;
    496482    psTime outTime;
    497483
    498     i= 0;
     484    i = 0;
    499485    n = 0;
    500486    y = 0;
    501487    epoch = 0;
    502488
    503     if(time == NULL)
    504     {
    505         psError(__func__,"Null value for tm arg not allowed");
    506         return outTime;
    507     } else
    508         if(time->tm_year < 70)
    509         {
    510             psError(__func__,"Input times earlier than 1970 not allowed. Value: %d", time->tm_year+1900);
    511             return outTime;
    512         } else
    513             if(time->tm_mon<0 || time->tm_mon>11)
    514             {
    515                 psError(__func__,"Month must have a value from 0 to 11. Value: %d", time->tm_mon);
    516                 return outTime;
    517             } else
    518                 if(time->tm_mday<1 || time->tm_mday>31)
    519                 {
    520                     psError(__func__,"Day must have a value from 1 to 31. Value: %d", time->tm_mday);
    521                     return outTime;
    522                 } else
    523                     if(time->tm_hour<0 || time->tm_hour>23)
    524                     {
    525                         psError(__func__,"Hour must have a value from 0 to 23. Value: %d", time->tm_hour);
    526                         return outTime;
    527                     } else
    528                         if(time->tm_min<0 || time->tm_min>59)
    529                         {
    530                             psError(__func__,"Minute must have a value from 0 to 59. Value: %d", time->tm_min);
    531                             return outTime;
    532                         } else
    533                             if(time->tm_sec<0 || time->tm_sec>59)
    534                             {
    535                                 psError(__func__,"Second must have a value from 0 to 59. Value: %d", time->tm_sec);
    536                                 return outTime;
    537                             }
     489    if (time == NULL)
     490    {
     491        psError(__func__, "Null value for tm arg not allowed");
     492        return outTime;
     493    } else if (time->tm_year < 70)
     494    {
     495        psError(__func__, "Input times earlier than 1970 not allowed. Value: %d", time->tm_year + 1900);
     496        return outTime;
     497    } else if (time->tm_mon < 0 || time->tm_mon > 11)
     498    {
     499        psError(__func__, "Month must have a value from 0 to 11. Value: %d", time->tm_mon);
     500        return outTime;
     501    } else if (time->tm_mday < 1 || time->tm_mday > 31)
     502    {
     503        psError(__func__, "Day must have a value from 1 to 31. Value: %d", time->tm_mday);
     504        return outTime;
     505    } else if (time->tm_hour < 0 || time->tm_hour > 23)
     506    {
     507        psError(__func__, "Hour must have a value from 0 to 23. Value: %d", time->tm_hour);
     508        return outTime;
     509    } else if (time->tm_min < 0 || time->tm_min > 59)
     510    {
     511        psError(__func__, "Minute must have a value from 0 to 59. Value: %d", time->tm_min);
     512        return outTime;
     513    } else if (time->tm_sec < 0 || time->tm_sec > 59)
     514    {
     515        psError(__func__, "Second must have a value from 0 to 59. Value: %d", time->tm_sec);
     516        return outTime;
     517    }
    538518
    539519    n = time->tm_year + 1900 - 1;
    540     epoch = (time->tm_year - 70) * SEC_PER_YEAR + ((n/4 - n/100 + n/400) -
    541             (1969/4 - 1969/100 + 1969/400)) * SEC_PER_DAY;
     520    epoch = (time->tm_year - 70) * SEC_PER_YEAR + ((n / 4 - n / 100 + n / 400) -
     521            (1969 / 4 - 1969 / 100 + 1969 / 400)) * SEC_PER_DAY;
    542522
    543523    y = time->tm_year + 1900;
    544524
    545525    // Adjust for leap years
    546     for(i = 0; i<time->tm_mon; i++)
    547     {
    548         epoch += mon [i] * SEC_PER_DAY;
    549         if(i == 1 && y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)) {
     526    for (i = 0; i < time->tm_mon; i++)
     527    {
     528        epoch += mon[i] * SEC_PER_DAY;
     529        if (i == 1 && y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)) {
    550530            epoch += SEC_PER_DAY;
    551531        }
     
    554534    // Add everything
    555535    epoch += (time->tm_mday - 1) * SEC_PER_DAY;
    556     epoch += time->tm_hour *SEC_PER_HOUR + time->tm_min * SEC_PER_MINUTE + time->tm_sec;
     536    epoch += time->tm_hour * SEC_PER_HOUR + time->tm_min * SEC_PER_MINUTE + time->tm_sec;
    557537
    558538    // Create psTime
     
    560540    outTime.tv_sec = epoch;
    561541
    562     CHECK_NEGATIVE_TIME_STRUCT(outTime,outTime);
    563 
    564     return outTime;
    565 }
     542    CHECK_NEGATIVE_TIME_STRUCT(outTime, outTime);
     543
     544    return outTime;
     545}
Note: See TracChangeset for help on using the changeset viewer.