| 1 |
/*=========================================================================== |
| 2 |
Copyright (C) 1987-2009 European Southern Observatory (ESO) |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or |
| 5 |
modify it under the terms of the GNU General Public License as |
| 6 |
published by the Free Software Foundation; either version 2 of |
| 7 |
the License, or (at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public |
| 15 |
License along with this program; if not, write to the Free |
| 16 |
Software Foundation, Inc., 675 Massachusetss Ave, Cambridge, |
| 17 |
MA 02139, USA. |
| 18 |
|
| 19 |
Corresponding concerning ESO-MIDAS should be addressed as follows: |
| 20 |
Internet e-mail: [email protected] |
| 21 |
Postal address: European Southern Observatory |
| 22 |
Data Management Division |
| 23 |
Karl-Schwarzschild-Strasse 2 |
| 24 |
D 85748 Garching bei Muenchen |
| 25 |
GERMANY |
| 26 |
===========================================================================*/ |
| 27 |
|
| 28 |
/*++++++++++++++ |
| 29 |
.TYPE Module |
| 30 |
.IDENTIFICATION trtime.c |
| 31 |
.LANGUAGE C |
| 32 |
.AUTHOR Francois Ochsenbein [ESO-IPG] |
| 33 |
.CATEGORY Time expression |
| 34 |
.COMMENTS Times may be expressed in three forms: |
| 35 |
\begin{TeX} |
| 36 |
\begin{itemize} |
| 37 |
\item integer (mnemonic {\tt i}): the number of seconds elapsed |
| 38 |
since Jan 1, 1970, at 00:00:00. |
| 39 |
The available period is roughly 1902-2037. |
| 40 |
\item double (mnemonic {\tt jd}): the Julian Date |
| 41 |
\item structure (mnemonic {\tt tm}): a structure containing, as integers: |
| 42 |
\begin{itemize} |
| 43 |
\item {\tt year} number |
| 44 |
\item {\tt mon} the month number in range [0-11] |
| 45 |
\item {\tt mday} the day number within month, in range [1-31] |
| 46 |
\item {\tt yday} the day number within year, in range [0-355] |
| 47 |
\item {\tt wday} the day number within week, in range [0-6] |
| 48 |
(0 is Sunday) |
| 49 |
\item {\tt hour} in range [0-23] |
| 50 |
\item {\tt min} in range [0-59] |
| 51 |
\item {\tt sec} in range [0-59] |
| 52 |
\end{itemize} |
| 53 |
\end{itemize} |
| 54 |
This routine does only include transformations between structure tm and |
| 55 |
integer. |
| 56 |
\end{TeX} |
| 57 |
|
| 58 |
.VERSION 1.0 04-Mar-1987: Creation |
| 59 |
.VERSION 1.1 21-Oct-1988: Cosmetic modifications |
| 60 |
.VERSION 1.2 14-Mar-1989: Removed bug in tr_tmi |
| 61 |
.VERSION 1.3 13-Jul-1989: Corrected bug in tr_tmi (returned status) |
| 62 |
Also checked if day is correct. |
| 63 |
090831 last modif |
| 64 |
--------------------------*/ |
| 65 |
|
| 66 |
#include <osdefos.h> |
| 67 |
#include <trtime.h> |
| 68 |
#include <tra.h> |
| 69 |
|
| 70 |
#define DEBUG 0 |
| 71 |
#define FINISH goto FIN |
| 72 |
#define RETURN FIN: return (trerror->errno ? -1 : 0) |
| 73 |
|
| 74 |
MID_EXTERN struct trerr_s *trerror; |
| 75 |
|
| 76 |
MID_STATIC unsigned char month[12]={31,28,31,30,31,30,31,31,30,31,30,31}; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
extern int tr_error(); |
| 81 |
|
| 82 |
int tr_tm(); |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
/*========================================================================*/ |
| 87 |
int tr_itm(t, T) |
| 88 |
/*+++ |
| 89 |
.PURPOSE Transform integer time to structure |
| 90 |
.RETURNS 0 |
| 91 |
.REMARKS |
| 92 |
---*/ |
| 93 |
long int t; /* IN: Time in seconds since Jan 1, 1970 */ |
| 94 |
struct tm *T; /* OUT: Time in the structure */ |
| 95 |
{ |
| 96 |
long int days, year; |
| 97 |
|
| 98 |
#if DEBUG |
| 99 |
printf("Input time is %ld sec\n", t); |
| 100 |
#endif |
| 101 |
days = t/86400L; |
| 102 |
if (t < 0) days--; |
| 103 |
t -= (days*86400L); |
| 104 |
#if DEBUG |
| 105 |
printf("Time of day is %ld sec\n", t); |
| 106 |
#endif |
| 107 |
T->tm_hour = t/3600; |
| 108 |
T->tm_min = 0; |
| 109 |
T->tm_sec = (t%3600); |
| 110 |
#if DEBUG |
| 111 |
printf("Days since 1970: %ld\n", days); |
| 112 |
printf("Time %dh %dmn %ds\n", T->tm_hour, T->tm_min, T->tm_sec); |
| 113 |
#endif |
| 114 |
|
| 115 |
days += (70L*365L + 18L); /* Offset 1900 */ |
| 116 |
year = (4L*days)/1461L; |
| 117 |
T->tm_year = year; |
| 118 |
T->tm_mon = 0; |
| 119 |
T->tm_mday = 0; |
| 120 |
T->tm_yday = days - 365L*year - (year+3)/4; |
| 121 |
T->tm_year+= 1900; |
| 122 |
|
| 123 |
tr_tm(T); |
| 124 |
|
| 125 |
return(0); |
| 126 |
} |
| 127 |
|
| 128 |
/*========================================================================*/ |
| 129 |
int tr_tmi(T, t) |
| 130 |
/*+++ |
| 131 |
.PURPOSE Transform structure time to time in seconds from Jan 1, 1970 |
| 132 |
.RETURNS 0 / -1 (time not in limits 1902-2037). Error message logged, |
| 133 |
.REMARKS |
| 134 |
---*/ |
| 135 |
struct tm *T; /* IN: Time as a structure */ |
| 136 |
long int *t; /* OUT: Time in seconds */ |
| 137 |
{ |
| 138 |
register long int n, y; |
| 139 |
|
| 140 |
trerror->errno = 0; |
| 141 |
trerror->len = 0; |
| 142 |
*t = 0x80000000; /* Default returned time... */ |
| 143 |
|
| 144 |
if (tr_tm(T)) /* Compute missing information... */ |
| 145 |
FINISH; |
| 146 |
|
| 147 |
if ((T->tm_year < 1902) || (T->tm_year >=2038)) |
| 148 |
{ trerror->errno = -1; |
| 149 |
trerror->msg = "Year outside limits [1902, 2038]"; |
| 150 |
tr_error(); |
| 151 |
FINISH; |
| 152 |
} |
| 153 |
|
| 154 |
n = T->tm_year - 1970; |
| 155 |
y = (n >= 0 ? n+1 : n-2); /* Leap year counter */ |
| 156 |
n = 365*n + y/4; |
| 157 |
|
| 158 |
*t = (n + T->tm_yday) * 86400L + T->tm_hour * 3600L |
| 159 |
+ T->tm_min * 60L + T->tm_sec; |
| 160 |
|
| 161 |
RETURN; |
| 162 |
} |
| 163 |
|
| 164 |
/*========================================================================*/ |
| 165 |
int tr_tm(T) |
| 166 |
/*+++ |
| 167 |
.PURPOSE Compute missing info in T structure |
| 168 |
.RETURNS 0 (good date) / -1 (bad data: invalid month or day number ...) |
| 169 |
.REMARKS |
| 170 |
---*/ |
| 171 |
struct tm *T; /* MOD: Time as a structure */ |
| 172 |
{ |
| 173 |
long int y, j, t; |
| 174 |
|
| 175 |
trerror->errno = 0; |
| 176 |
trerror->len = 0; |
| 177 |
trerror->msg = (char *)0; |
| 178 |
month[1] = 29; |
| 179 |
|
| 180 |
if (T->tm_mday) /* Compute Day of 400 yr cycle */ |
| 181 |
{ if ( (T->tm_mon > 11) || (T->tm_mon < 0) ) |
| 182 |
trerror->msg = "bad month"; |
| 183 |
if ( (T->tm_mday <= 0) || (T->tm_mday > (int)month[T->tm_mon])) |
| 184 |
trerror->msg = "bad day_in_month"; |
| 185 |
if (trerror->msg) |
| 186 |
{ trerror->errno = -1; |
| 187 |
tr_error(); |
| 188 |
FINISH; |
| 189 |
} |
| 190 |
} |
| 191 |
/* Convert time */ |
| 192 |
|
| 193 |
j = 0; |
| 194 |
t = ((long)(T->tm_hour*60) + (long)(T->tm_min))*60 + T->tm_sec; |
| 195 |
while (t < 0) { j--; t += 86400L; } |
| 196 |
while (t >= 86400L) { j++; t -= 86400L; } |
| 197 |
#if DEBUG |
| 198 |
printf("tr_tm: Time of day is %ld sec\n", t); |
| 199 |
#endif |
| 200 |
T->tm_sec = t%60; |
| 201 |
t /= 60; |
| 202 |
T->tm_min = t%60; |
| 203 |
T->tm_hour= t/60; |
| 204 |
t = j; /* Days */ |
| 205 |
|
| 206 |
y = T->tm_year % 400; |
| 207 |
T->tm_year /= 400; |
| 208 |
if (y < 0) { y += 400; T->tm_year -= 1;} |
| 209 |
T->tm_year *= 400; |
| 210 |
if (T->tm_mday) /* Compute Day of 400 yr cycle */ |
| 211 |
{ y += 400; /* Positive interval */ |
| 212 |
j = y - (11 - T->tm_mon)/10; |
| 213 |
t += ((1461L*j)/4 + (306L * ((T->tm_mon+10)%12) + 5)/10 |
| 214 |
- (3* ((j + 100L)/100L))/4 + T->tm_mday + 59L) % 146097L; |
| 215 |
y -= 400; |
| 216 |
} |
| 217 |
else t += 365*y + (y+3)/4 - (y-1)/100 + T->tm_yday; |
| 218 |
|
| 219 |
while (t < 0) { T->tm_year -= 400; t += 146097L;} |
| 220 |
|
| 221 |
/* Compute Week day */ |
| 222 |
|
| 223 |
T->tm_wday = (t+6L)%7L; |
| 224 |
|
| 225 |
/* Compute Year */ |
| 226 |
|
| 227 |
T->tm_year += 400L*(t/146097L); |
| 228 |
t %= 146097L; |
| 229 |
y = 4 *(t + (t-60)/36524L) / 1461L; |
| 230 |
T->tm_year += y; |
| 231 |
T->tm_yday = t - y*365L - (y+3)/4 + (y-1)/100; |
| 232 |
|
| 233 |
/* Compute mday + mon from yday */ |
| 234 |
|
| 235 |
if (y&3) month[1] = 28; |
| 236 |
if ( ((y%100) == 0) && (y)) month[1] = 28; |
| 237 |
|
| 238 |
for (y=T->tm_yday, j=0; y>=(int)month[j]; y -= month[j], j++) ; |
| 239 |
T->tm_mon = j; |
| 240 |
T->tm_mday = y+1; |
| 241 |
#if DEBUG |
| 242 |
printf("Time %dh %dmn %ds\n", T->tm_hour, T->tm_min, T->tm_sec); |
| 243 |
#endif |
| 244 |
|
| 245 |
RETURN; |
| 246 |
} |
| 247 |
|