PLplot  5.15.0
qsastime_test.c
Go to the documentation of this file.
1 //
2 // This software originally contributed under the LGPL in January 2009 to
3 // PLplot by the
4 // Cluster Science Centre
5 // QSAS team,
6 // Imperial College, London
7 // Copyright (C) 2009 Imperial College, London
8 //
9 // This file is part of PLplot.
10 //
11 // PLplot is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU Library General Public License as published
13 // by the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
15 //
16 // PLplot is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU Library General Public License for more details.
20 //
21 // You should have received a copy of the GNU Library General Public License
22 // along with PLplot; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 //
25 //
26 #include "qsastime_extra.h"
27 #include "qsastimeP.h"
28 #include <time.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 // Test program to run MJDtime
32 
33 int main()
34 {
35  char buf[360];
36  char copy[360];
37  int y = 2004;
38  int m = 0;
39  int d = 23;
40  int hour = 13;
41  int min = 39;
42  double sec = 2.345678901;
43  const char *ISOstring;
44  double epoch;
45  int doy, ifleapyear, y1;
46  double zeroTest;
47 
48  size_t used;
49  time_t localt;
50  struct tm *ptm;
51 
52  MJDtime MJD1;
53  MJDtime MJD2;
54 
55 
56  printf( "Start date/time components: %d-%d-%d %d:%d:%13.11g\n", y, m + 1, d, hour, min, sec );
57 
58  setFromUT( y, m, d, hour, min, sec, &MJD2, 0 );
59 
60  breakDownMJD( &y, &m, &d, &hour, &min, &sec, &MJD2, 0 );
61  printf( "date/time components: %d-%d-%d %d:%d:%13.11g\n\n", y, m + 1, d, hour, min, sec );
62 
63  printf( "MJD = %d, seconds = %17.15g\n", MJD2.base_day, MJD2.time_sec );
64  printf( " MJD = %18.10f \n", getMJD( &MJD2 ) );
65  printf( " JD = %18.10f \n\n", getJD( &MJD2 ) );
66 
67  ISOstring = getISOString( &MJD2, 0, 0 );
68  printf( "ISO string = '%s'\n\n", ISOstring );
69 
70  strfMJD( &( buf[0] ), 360, "%Y-%m-%d %H:%M:%S%.", &MJD2, 0, 0 );
71  printf( "strfMJD:ISO equiv: '%s'\n", buf );
72  strfMJD( &( buf[0] ), 360, "%Y-%m-%dT%H:%M:%S%4Z", &MJD2, 0, 0 );
73  printf( "strfMJD:ISO equiv: '%s'\n", buf );
74 
75  getYAD( &y1, &ifleapyear, &doy, &MJD2, 0 );
76  setFromDOY( y, doy, hour, min, sec, &MJD1, 0 );
77 
78  zeroTest = getDiffSecs( &MJD1, &MJD2 );
79  printf( "difference MJD (month/day) - MJD(doy) '%g'\n\n", zeroTest );
80 
81  hour += 24;
82  d -= 1;
83  setFromUT( y, m, d, hour, min, sec, &MJD2, 0 );
84  zeroTest = getDiffDays( &MJD1, &MJD2 );
85  printf( "difference MJD (d-1, h+24) - MJD(d, h) '%g'\n\n", zeroTest );
86 
87  epoch = getCDFepoch( &MJD2 );
88  printf( "CDF epoch sec %18.3f\n", epoch );
89  setFromCDFepoch( epoch, &MJD2 );
90  printf( "from CDF ISO string (CDF epoch is accurate to msec only) = '%s'\n", getISOString( &MJD2, 1, 0 ) );
91 
92  printf( "Day of week is/was %s\n\n", getDayOfWeek( &MJD2 ) );
93 
94  ISOstring = getISOString( &MJD1, 0, 0 );
95  printf( "ISO string = '%s'\n\n", ISOstring );
96  setFromISOstring( ISOstring, &MJD1, 0 );
97  printf( "for %s, MJD = %d, seconds = %17.11g\n", ISOstring, MJD1.base_day, MJD1.time_sec );
98  ISOstring = getISOString( &MJD1, 1, 0 );
99  printf( "ISO string = '%s'\n\n", ISOstring );
100  setFromISOstring( ISOstring, &MJD1, 0 );
101  printf( "for %s, MJD = %d, seconds = %17.11g\n\n", ISOstring, MJD1.base_day, MJD1.time_sec );
102 
103  // try julian/gregorian changeover
104  y = 1752;
105  m = 8;
106  d = 14;
107  hour = 0;
108 
109  setFromUT( y, m, d, hour, min, sec, &MJD1, 0 );
110  strcpy( &( copy[0] ), getISOString( &MJD1, 0, 0 ) ); // copy because getISOString() returns a pointer to a static string
111  printf( "Gregorian = '%s'\n", &( copy[0] ) );
112  //setFromUT(y, m, d, hour, min, sec, &MJD1, 1); // set from Julian date
113  printf( "%s Julian = '%s' Gregorian, (give us back our 11 days)\n", getISOString( &MJD1, 1, 1 ), &( copy[0] ) );
114 
115 
116  // Compare formatting from strftime()
117 
118 
119  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 );
120  printf( "chars %d for \n%s\n", (int) used, buf );
121 
122  // seconds since 01 Jan 1970 Gregorian for strftime use
123  localt = (int) MJD2.time_sec + ( MJD2.base_day - 40587 ) * 86400;
124  ptm = gmtime( &localt );
125 #ifndef _MSC_VER
126  // note %s not implemented in cygwin 1.5 gcc 3.x nothing printed
127  strftime( &( buf[0] ), 360,
128  " 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 );
129 #else
130  // the following format options are not defined in MSVC (2008)
131  // and are replaced as follows
132  // %e -> %d will print as 01 etc
133  // %D -> %m/%d/%y
134  // %F -> %Y-%m-%d
135  // %r -> %I:%M:%S %p
136  // %s -> %%s - turned into literal as not implemented
137  //
138  // also %c and %p are locale dependent
139  //
140  strftime( &( buf[0] ), 360,
141  " 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 );
142 #endif
143  printf( "%s\n", buf );
144 }
double getJD(MJDtime *MJD)
size_t strfMJD(char *buf, size_t len, const char *format, const MJDtime *MJD, int forceJulian, int inleap)
Definition: qsastime.c:365
int setFromUT(int year, int month, int day, int hour, int min, double sec, MJDtime *MJD, int forceJulian)
Definition: qsastime.c:71
int min(int a, int b)
double getDiffSecs(MJDtime *MJD1, MJDtime *MJD2)
double getDiffDays(MJDtime *MJD1, MJDtime *MJD2)
double getCDFepoch(MJDtime *MJD)
void setFromCDFepoch(double cdfepoch, MJDtime *MJD)
void getYAD(int *year, int *ifleapyear, int *doy, const MJDtime *MJD, int forceJulian)
Definition: qsastime.c:175
int setFromISOstring(const char *ISOstring, MJDtime *MJD, int forceJulian)
double time_sec
Definition: qsastimeP.h:20
const char * getISOString(MJDtime *MJD, int delim, int forceJulian)
void setFromDOY(int year, int doy, int hour, int min, double sec, MJDtime *MJD, int forceJulian)
static char buf[200]
Definition: tclAPI.c:873
int main()
Definition: qsastime_test.c:33
const char * getDayOfWeek(const MJDtime *MJD)
Definition: qsastime.c:334
double getMJD(MJDtime *MJD)
void breakDownMJD(int *year, int *month, int *day, int *hour, int *min, double *sec, const MJDtime *MJD, int forceJulian)
Definition: qsastime.c:289