PLplot  5.10.0
qsastime_test.c
Go to the documentation of this file.
00001 //
00002 // This software originally contributed under the LGPL in January 2009 to
00003 // PLplot by the
00004 // Cluster Science Centre
00005 // QSAS team,
00006 // Imperial College, London
00007 // Copyright (C) 2009 Imperial College, London
00008 //
00009 // This file is part of PLplot.
00010 //
00011 // PLplot is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Library General Public License as published
00013 // by the Free Software Foundation; either version 2 of the License, or
00014 // (at your option) any later version.
00015 //
00016 // PLplot is distributed in the hope that it will be useful,
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 // GNU Library General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Library General Public License
00022 // along with PLplot; if not, write to the Free Software
00023 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00024 //
00025 //
00026 #include "qsastime_extra.h"
00027 #include "qsastimeP.h"
00028 #include <time.h>
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 // Test program to run MJDtime
00032 
00033 int main()
00034 {
00035     char       buf[360];
00036     char       copy[360];
00037     int        y    = 2004;
00038     int        m    = 0;
00039     int        d    = 23;
00040     int        hour = 13;
00041     int        min  = 39;
00042     double     sec  = 2.345678901;
00043     const char *ISOstring;
00044     double     epoch;
00045     int        doy, ifleapyear, y1;
00046     double     zeroTest;
00047 
00048     size_t     used;
00049     time_t     localt;
00050     struct tm  *ptm;
00051 
00052     MJDtime    MJD1;
00053     MJDtime    MJD2;
00054 
00055 
00056     printf( "Start date/time components: %d-%d-%d %d:%d:%13.11g\n", y, m + 1, d, hour, min, sec );
00057 
00058     setFromUT( y, m, d, hour, min, sec, &MJD2, 0 );
00059 
00060     breakDownMJD( &y, &m, &d, &hour, &min, &sec, &MJD2, 0 );
00061     printf( "date/time components: %d-%d-%d %d:%d:%13.11g\n\n", y, m + 1, d, hour, min, sec );
00062 
00063     printf( "MJD = %d, seconds = %17.15g\n", MJD2.base_day, MJD2.time_sec );
00064     printf( " MJD = %18.10f \n", getMJD( &MJD2 ) );
00065     printf( " JD = %18.10f \n\n", getJD( &MJD2 ) );
00066 
00067     ISOstring = getISOString( &MJD2, 0, 0 );
00068     printf( "ISO string = '%s'\n\n", ISOstring );
00069 
00070     strfMJD( &( buf[0] ), 360, "%Y-%m-%d %H:%M:%S%.", &MJD2, 0, 0 );
00071     printf( "strfMJD:ISO equiv:   '%s'\n", buf );
00072     strfMJD( &( buf[0] ), 360, "%Y-%m-%dT%H:%M:%S%4Z", &MJD2, 0, 0 );
00073     printf( "strfMJD:ISO equiv:  '%s'\n", buf );
00074 
00075     getYAD( &y1, &ifleapyear, &doy, &MJD2, 0 );
00076     setFromDOY( y, doy, hour, min, sec, &MJD1, 0 );
00077 
00078     zeroTest = getDiffSecs( &MJD1, &MJD2 );
00079     printf( "difference MJD (month/day) - MJD(doy) '%g'\n\n", zeroTest );
00080 
00081     hour += 24;
00082     d    -= 1;
00083     setFromUT( y, m, d, hour, min, sec, &MJD2, 0 );
00084     zeroTest = getDiffDays( &MJD1, &MJD2 );
00085     printf( "difference MJD (d-1, h+24) - MJD(d, h) '%g'\n\n", zeroTest );
00086 
00087     epoch = getCDFepoch( &MJD2 );
00088     printf( "CDF epoch sec %18.3f\n", epoch );
00089     setFromCDFepoch( epoch, &MJD2 );
00090     printf( "from CDF ISO string (CDF epoch is accurate to msec only) = '%s'\n", getISOString( &MJD2, 1, 0 ) );
00091 
00092     printf( "Day of week is/was %s\n\n", getDayOfWeek( &MJD2 ) );
00093 
00094     ISOstring = getISOString( &MJD1, 0, 0 );
00095     printf( "ISO string = '%s'\n\n", ISOstring );
00096     setFromISOstring( ISOstring, &MJD1, 0 );
00097     printf( "for %s, MJD = %d, seconds = %17.11g\n", ISOstring, MJD1.base_day, MJD1.time_sec );
00098     ISOstring = getISOString( &MJD1, 1, 0 );
00099     printf( "ISO string = '%s'\n\n", ISOstring );
00100     setFromISOstring( ISOstring, &MJD1, 0 );
00101     printf( "for %s, MJD = %d, seconds = %17.11g\n\n", ISOstring, MJD1.base_day, MJD1.time_sec );
00102 
00103     // try julian/gregorian changeover
00104     y    = 1752;
00105     m    = 8;
00106     d    = 14;
00107     hour = 0;
00108 
00109     setFromUT( y, m, d, hour, min, sec, &MJD1, 0 );
00110     strcpy( &( copy[0] ), getISOString( &MJD1, 0, 0 ) ); // copy because getISOString() returns a pointer to a static string
00111     printf( "Gregorian = '%s'\n", &( copy[0] ) );
00112     //setFromUT(y, m, d, hour, min, sec, &MJD1, 1); // set from Julian date
00113     printf( "%s Julian = '%s' Gregorian, (give us back our 11 days)\n", getISOString( &MJD1, 1, 1 ), &( copy[0] ) );
00114 
00115 
00116     // Compare formatting from strftime()
00117 
00118 
00119     used = strfMJD( &( buf[0] ), 360, "   strfMJD():\n   --------\n '%+' \n %c\n %D %F \n %j \n %r \n %s \n %v\n\n", &MJD2, 0, 0 );
00120     printf( "chars %d for \n%s\n", (int) used, buf );
00121 
00122     // seconds since 01 Jan 1970 Gregorian for strftime use
00123     localt = (int) MJD2.time_sec + ( MJD2.base_day - 40587 ) * 86400;
00124     ptm    = gmtime( &localt );
00125 #ifndef _MSC_VER
00126     // note %s not implemented in cygwin 1.5 gcc 3.x nothing printed
00127     strftime( &( buf[0] ), 360,
00128         "  strftime(): (invalid before 1970 on some systems and subject to time zones on all systems)\n   ------\n '%a %b %e %H:%M:%S UTC %Y' \n %c\n %D %F \n %j \n %r \n %s \n %e-%b-%Y", ptm );
00129 #else
00130     // the following format options are not defined in MSVC (2008)
00131     // and are replaced as follows
00132     // %e -> %d     will print as 01 etc
00133     // %D -> %m/%d/%y
00134     // %F -> %Y-%m-%d
00135     // %r -> %I:%M:%S %p
00136     // %s -> %%s - turned into literal as not implemented
00137     //
00138     // also %c and %p are locale dependent
00139     //
00140     strftime( &( buf[0] ), 360,
00141         "  strftime(): (invalid before 1970)\n   ------\n '%a %b %d %H:%M:%S UTC %Y' \n %c\n %m/%d/%y %Y-%m-%d \n %j \n %I:%M:%S %p \n %%s not implemented \n %d-%b-%Y", ptm );
00142 #endif
00143     printf( "%s\n", buf );
00144 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines