/** @file  tst_psTime_01.c
 *
 *  @brief Test driver for psTime functions
 *
 *  This test driver contains the following tests for psTime:
 *     A) Get current TAI time
 *     B) Print test time
 *     C) Convert psTime to ISO time
 *     D) Convert ISO time to psTime
 *     E) Convert psTime to UTC time
 *     F) Convert UTC time to psTime
 *     G) Convert psTime to MJD time
 *     H) Convert MJD time to psTime
 *     I) Convert psTime to JD time
 *     J) Convert JD time to psTime
 *     K) Convert psTime to timeval time
 *     L) Convert timeval time to psTime
 *     M) Convert psTime to tm time
 *     N) Convert tm time to psTime
 *     O) Convert psTime time to LST
 *     P) Free psMetadata
 *
 *  @author  Ross Harman, MHPCC
 *
 *  @version $Revision: 1.12 $  $Name: not supported by cvs2svn $
 *  @date  $Date: 2004-10-12 01:34:09 $
 *
 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
 *
 */

#include "pslib.h"
#include "psTest.h"
#include <string.h>

int main(int argc, char* argv[])
{
    psTime *testTime;
    char *testString = "2004-07-21T18:22:24.272Z";

    // Test time was taken at July 21, 2004 at 18:22:24.272044
    testTime = (psTime*)psAlloc(sizeof(psTime));
    testTime->sec = 1090434144;
    testTime->usec = 272044;
    testTime->type = PS_TIME_TAI;

    // Test A - Get current TAI time
    printPositiveTestHeader(stdout,"psTime", "Get current TAI time");
    psTime *timeA = NULL;
    timeA = psTimeGetTime(PS_TIME_TAI);
    psFree(timeA);
    printFooter(stdout, "psTime", "Get current TAI time", true);


    // Test B - Print test time
    printPositiveTestHeader(stdout,"psTime", "Print test time");
    printf("Test time: Seconds = %ld Microseconds = %ld\n", (long)testTime->sec, (long)testTime->usec);
    printFooter(stdout, "psTime", "Print test time", true);


    // Test C - Convert psTime to ISO time
    printPositiveTestHeader(stdout,"psTime", "Convert psTime to ISO time");
    char *isoString;
    isoString = psTimeToISOTime(testTime);
    printf("%s\n", isoString);
    if(strncmp(isoString, testString, 256)) {
        printf("ERROR - ISO string incorrect\n");
        printf("Returned: %s\n", isoString);
    }
    printFooter(stdout, "psTime", "Convert psTime to ISO time", true);


    // Test D - Convert ISO time to psTime
    printPositiveTestHeader(stdout,"psTime", "Convert ISO time to psTime");
    psTime *timeD = NULL;
    timeD = psTimeFromISOTime(isoString);
    printf("psTime: Seconds = %ld Microseconds = %d\n", (long)timeD->sec, (psU32)timeD->usec);
    psFree(isoString);
    psFree(timeD);
    printFooter(stdout, "psTime", "Convert ISO time to psTime", true);


    // Test E - Convert TAI to UTC time
    printPositiveTestHeader(stdout,"psTime", "Convert psTime time to UTC time");
    psTime *timeE = NULL;
    timeE = (psTime*)psAlloc(sizeof(psTime));
    timeE->sec = 1090434144;
    timeE->usec = 272044;
    timeE->type = PS_TIME_TAI;
    timeE = psTimeConvert(timeE, PS_TIME_UTC);
    psFree(timeE);
    printf("psTime: Seconds = %ld Microseconds = %d\n", (long)timeE->sec, (psU32)timeE->usec);
    printFooter(stdout, "psTime", "Convert psTime time to UTC time", true);


    // Test F - Convert UTC time to psTime
    printPositiveTestHeader(stdout,"psTime", "Convert UTC time to psTime");
    psTime *timeF = NULL;
    timeF = (psTime*)psAlloc(sizeof(psTime));
    timeF->sec = 1090434112;
    timeF->usec = 272044;
    timeF->type = PS_TIME_UTC;
    timeF = psTimeConvert(timeF, PS_TIME_TAI);
    psFree(timeF);
    printf("psTime: Seconds = %ld Microseconds = %d\n", (long)timeF->sec, (psU32)timeF->usec);
    printFooter(stdout, "psTime", "Convert UTC time to psTime", true);


    // Test G - Convert psTime to MJD time
    printPositiveTestHeader(stdout,"psTime", "Convert psTime to MJD time");
    double mjdTime = 0.0;
    mjdTime = psTimeToMJD(testTime);
    printf("MJD = %f\n", mjdTime);
    printFooter(stdout, "psTime", "Convert psTime to MJD time", true);


    // Test H - Convert MJD time to psTime
    printPositiveTestHeader(stdout,"psTime", "Convert MJD time to psTime");
    psTime *timeH = NULL;
    timeH = psTimeFromMJD(mjdTime);
    printf("psTime: Seconds = %ld Microseconds = %d\n", (long)timeH->sec, (psU32)timeH->usec);
    psFree(timeH);
    printFooter(stdout, "psTime", "Convert MJD time to psTime", true);


    // Test I - Convert psTime to JD time
    printPositiveTestHeader(stdout,"psTime", "Convert psTime to JD time");
    double jdTime = 0.0;
    jdTime = psTimeToJD(testTime);
    printf("JD = %f\n", jdTime);
    printFooter(stdout, "psTime", "Convert psTime to JD time", true);


    // Test J - Convert JD time to psTime
    printPositiveTestHeader(stdout,"psTime", "Convert JD time to psTime");
    psTime *timeJ = NULL;
    timeJ = psTimeFromJD(jdTime);
    printf("psTime: Seconds = %ld Microseconds = %d\n", (long)timeJ->sec, (psU32)timeJ->usec);
    psFree(timeJ);
    printFooter(stdout, "psTime", "Convert JD time to psTime", true);


    // Test K - Convert psTime to timeval time
    printPositiveTestHeader(stdout,"psTime", "Convert psTime to timeval time");
    struct timeval timevalTime;
    timevalTime = psTimeToTimeval(testTime);
    printf("timevalTime: Seconds = %ld Microseconds = %ld\n", (long)timevalTime.tv_sec, (long)timevalTime.tv_usec);
    printFooter(stdout, "psTime", "Convert psTime to timeval time", true);


    // Test L - Convert timeval time to psTime
    printPositiveTestHeader(stdout,"psTime", "Convert timeval time to psTime");
    psTime *timeL = NULL;
    timeL = psTimeFromTimeval(&timevalTime);
    printf("psTime: Seconds = %ld Microseconds = %d\n", (long)timeL->sec, (psU32)timeL->usec);
    psFree(timeL);
    printFooter(stdout, "psTime", "Convert timeval time to psTime", true);


    // Test M - Convert psTime to tm time
    printPositiveTestHeader(stdout,"psTime", "Convert psTime to tm time");
    struct tm *tmTime;
    tmTime = psTimeToTM(testTime);
    printf("tmTime:\n");
    printf("tmTime->tm_year = %d\n", tmTime->tm_year);
    printf("tmTime->tm_mon = %d\n", tmTime->tm_mon);
    printf("tmTime->tm_mday = %d\n", tmTime->tm_mday);
    printf("tmTime->tm_hour = %d\n", tmTime->tm_hour);
    printf("tmTime->tm_min = %d\n", tmTime->tm_min);
    printf("tmTime->tm_sec = %d\n", tmTime->tm_sec);
    printFooter(stdout, "psTime", "Convert psTime to tm time", true);


    // Test N - Convert tm time to psTime
    printPositiveTestHeader(stdout, "psTime", "Convert tm time to psTime");
    psTime *timeN = NULL;
    timeN = psTimeFromTM(tmTime);
    printf("psTime: Seconds = %ld Microseconds = %d\n", (long)timeN->sec, (psU32)timeN->usec);
    psFree(timeN);
    psFree(tmTime);
    printFooter(stdout, "psTime", "Convert tm time to psTime", true);


    // Test O - psTime to LST
    printPositiveTestHeader(stdout, "psTime", "Convert psTime time to LST");
    char *testString2 = "2004-09-10T1:00:00.00Z";
    psTime* testTime2 = NULL;
    testTime2 = psTimeFromISOTime(testString2);
    double dblTime = psTimeToLST(testTime2, 1);
    printf("LST (rad): %lf\n", dblTime);
    psFree(testTime2);
    printFooter(stdout, "psTime", "Convert psTime time to LST", true);


    // Test P - Free psMetadata
    printPositiveTestHeader(stdout, "psMetadata", "Test P - Free psMetadata");
    psFree(testTime);
    psMemCheckLeaks(0, NULL, stdout);
    psMemCheckCorruption(0);
    int nBad = psMemCheckCorruption(0);
    if(nBad) {
        printf("ERROR: Found %d bad memory blocks\n", nBad);
    }
    printFooter(stdout, "psMetadata", "Test P - Free psMetadata", true);

    return 0;
}
