| 1 |
/*=========================================================================== |
| 2 |
Copyright (C) 1986-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 |
|
| 30 |
.TYPE Module |
| 31 |
.IDENTIFICATION edtime.c |
| 32 |
.AUTHOR Francois Ochsenbein [ESO-IPG] |
| 33 |
.LANGUAGE C |
| 34 |
.KEYWORDS Date & Time edition |
| 35 |
.ENVIRONMENT Any |
| 36 |
.COMMENTS The following conventions in pic: |
| 37 |
\begin{TeX} |
| 38 |
\begin{itemize} |
| 39 |
\item {\tt YY} or {\tt YYYY}: year |
| 40 |
\item {\tt MM}: month number |
| 41 |
\item {\tt MMM}\dots: month name (alphabetic) |
| 42 |
\item {\tt DD}: day of the month (number), edited with leading zeroes |
| 43 |
\item {\tt dd}: day of the month (number), edited with leading blanks |
| 44 |
\item {\tt DDD}: day of the year (number), edited with leading zeroes |
| 45 |
\item {\tt ddd}: day of the year (number), edited with leading blanks |
| 46 |
\item {\tt WW\dots}: weekday name (alphabetic) |
| 47 |
\item {\tt ww\dots}: weekday name followed by a comma |
| 48 |
\item {\tt hh}: hour of the day (00\dots23) |
| 49 |
\item {\tt mm}: minutes (00\dots59) |
| 50 |
\item {\tt ss}: seconds (00\dots59) |
| 51 |
\end{itemize} |
| 52 |
The {\bf tm} structure is defined in {\tt time.h} and has the structure: |
| 53 |
\begin{itemize} |
| 54 |
\item int tm\_year number |
| 55 |
\item int tm\_mon number [0-11] \item int tm\_mday [1-31] |
| 56 |
\item int tm\_yday [0-355] \item int tm\_wday [0-6] (Sunday = 0) |
| 57 |
\item int tm\_hour [0-23] \item int tm\_min [0-59] |
| 58 |
\item int tm\_sec [0-59] \item int tm\_isdst |
| 59 |
\end{itemize} |
| 60 |
\end{TeX} |
| 61 |
|
| 62 |
.VERSION 1.0 31-Jul-1986: Creation. |
| 63 |
.VERSION 1.1 04-Mar-1987: Usage of DCL_T structure; the |
| 64 |
date may be any valid Gregorian date. |
| 65 |
.VERSION 1.2 21-Oct-1988: Use new include files |
| 66 |
|
| 67 |
090831 last modif |
| 68 |
----------------------------------------------------------------------------*/ |
| 69 |
|
| 70 |
#include <midas_def.h> |
| 71 |
#include <macrogen.h> |
| 72 |
#include <trtime.h> |
| 73 |
|
| 74 |
|
| 75 |
extern int tr_tm(), tr_itm(), ed_pic(); |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
#if SW_LEVEL |
| 82 |
MID_RSTATIC char Month_list[] = |
| 83 |
"JanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember\ |
| 84 |
MarzM\\\"arzMaiJuniJuliOktoberDezember\ |
| 85 |
JanvierFevrierF\\\'evrierMarsAvrilJuinJuilletAoutAo\\^utSeptembreOctobre\ |
| 86 |
NovembreDecembreD\\\'ecembre"; |
| 87 |
|
| 88 |
MID_RSTATIC char Day_list[]= |
| 89 |
"SundayMondayTuesdayWednesdayThursdayFridaySaturday\ |
| 90 |
SonntagMontagDienstagMittwochDonnerstagFreitagSamstag\ |
| 91 |
DimancheLundiMardiMercrediJeudiVendrediSamedi"; |
| 92 |
|
| 93 |
MID_RSTATIC unsigned char Month_name[3][12][2] = { |
| 94 |
/* English */ { {7,0}, {8,7}, {5,15}, {5,20}, {3,25}, {4,28}, |
| 95 |
{4,32}, {6,36}, {9,42}, {7,51}, {8,58}, {8,66}}, |
| 96 |
/* German */ { {6,0}, {7,7}, {4,75}, {5,20}, {3,85}, {4,88}, |
| 97 |
{4,92}, {6,36}, {9,42}, {7,96}, {8,58}, {8,103}}, |
| 98 |
/* French */ { {7,111},{7,118},{4,134},{5,138},{3,85}, {4,143}, |
| 99 |
{7,147},{4,154},{9,164},{7,173},{8,180},{8,188}} |
| 100 |
}; |
| 101 |
MID_RSTATIC unsigned char Month_TeX[3][12][2] = { |
| 102 |
/* English */ { {7,0}, {8,7}, {5,15}, {5,20}, {3,25}, {4,28}, |
| 103 |
{4,32}, {6,36}, {9,42}, {7,51}, {8,58}, {8,66}}, |
| 104 |
/* German */ { {6,0}, {7,7}, {6,79}, {5,20}, {3,85}, {4,88}, |
| 105 |
{4,92}, {6,36}, {9,42}, {7,96}, {8,58}, {8,103}}, |
| 106 |
/* French */ { {7,111},{9,125},{4,134},{5,138},{3,85}, {4,143}, |
| 107 |
{7,147},{6,158},{9,164},{7,173},{8,180},{10,196}} |
| 108 |
}; |
| 109 |
MID_RSTATIC unsigned char Day_name[3][7][2] = { |
| 110 |
/* English */ { {6,0}, {6,6}, {7,12}, {9,19}, {8,28}, {6,36}, {8,42}}, |
| 111 |
/* German */ { {7,50}, {6,57}, {8,63}, {8,71}, {10,79},{7,89}, {7,96}}, |
| 112 |
/* French */ { {8,103},{5,111},{5,116},{8,121},{5,129},{8,134},{6,142}} |
| 113 |
}; |
| 114 |
MID_STATIC unsigned char language = 0; |
| 115 |
MID_STATIC unsigned char TeX = 0; |
| 116 |
|
| 117 |
#else /* Minimal Implementation */ |
| 118 |
|
| 119 |
MID_RSTATIC char Month_list[] = |
| 120 |
"JanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember"; |
| 121 |
MID_RSTATIC char Day_list[]= "SundayMondayTuesdayWednesdayThursdayFridaySaturday"; |
| 122 |
MID_RSTATIC unsigned char Month_name[12][2] = { |
| 123 |
/* English */ {7,0}, {8,7}, {5,15}, {5,20}, {3,25}, {4,28}, |
| 124 |
{4,32}, {6,36}, {9,42}, {7,51}, {8,58}, {8,66} |
| 125 |
}; |
| 126 |
MID_RSTATIC unsigned char Day_name[7][2] = { |
| 127 |
/* English */ {6,0}, {6,6}, {7,12}, {9,19}, {8,28}, {6,36}, {8,42} |
| 128 |
}; |
| 129 |
#endif |
| 130 |
|
| 131 |
MID_STATIC struct tm TM; |
| 132 |
|
| 133 |
/*========================================================================== |
| 134 |
* ed |
| 135 |
*==========================================================================*/ |
| 136 |
static int ed(buffer, pic, T) |
| 137 |
/*+++ |
| 138 |
.PURPOSE Edit the structure describing time & date (localtime) |
| 139 |
according to a picture. |
| 140 |
.RETURNS Length of edited date / time |
| 141 |
---------*/ |
| 142 |
char *buffer; /* OUT: Output buffer */ |
| 143 |
char *pic; /* IN: Used picture */ |
| 144 |
struct tm *T; /* IN: Decomposed time */ |
| 145 |
{ |
| 146 |
char *q, *p; |
| 147 |
int i, l; |
| 148 |
unsigned char oc, lc; |
| 149 |
static char ypic[] = "--9999"; |
| 150 |
#define zpic (ypic+2) |
| 151 |
static char dpic[] = "0009"; |
| 152 |
|
| 153 |
p = buffer; |
| 154 |
while(*pic) |
| 155 |
{ l = 1; /* Count identical characters */ |
| 156 |
while (*(pic+l) == *pic) l++; |
| 157 |
|
| 158 |
switch(*pic) |
| 159 |
{ default: DEFAULT: /* Simply copy characters to out */ |
| 160 |
while (--l >= 0) *(p++) = *(pic++); |
| 161 |
continue; |
| 162 |
|
| 163 |
case 'Y': if (l < 2) goto DEFAULT; |
| 164 |
i = T->tm_year; |
| 165 |
if (l<=2) i -= 1900; |
| 166 |
if (l<6) ypic[l] = EOS; |
| 167 |
p += ed_pic(p, ypic, (long)i); |
| 168 |
if (l<6) ypic[l] = '9'; |
| 169 |
break; |
| 170 |
|
| 171 |
case 'M': /* Month number or name */ |
| 172 |
i = T->tm_mon; |
| 173 |
if ((i < 0) || (i >= 12)) |
| 174 |
{ p += oscfill(p, l, '~'); break; } |
| 175 |
if (l == 1) goto DEFAULT; |
| 176 |
if (l == 2) { p += ed_pic(p, "99", (long)(i+1)); break;} |
| 177 |
# if SW_LEVEL |
| 178 |
if (TeX) |
| 179 |
{ oc = Month_TeX[language][i][1]; |
| 180 |
lc = Month_TeX[language][i][0]; |
| 181 |
} else |
| 182 |
{ oc = Month_name[language][i][1]; |
| 183 |
lc = Month_name[language][i][0]; |
| 184 |
} |
| 185 |
# else |
| 186 |
{ oc = Month_name[i][1]; |
| 187 |
lc = Month_name[i][0]; |
| 188 |
} |
| 189 |
# endif |
| 190 |
i = MIN(l,(int)lc); |
| 191 |
pic += i; |
| 192 |
l -= i; |
| 193 |
for (q = &Month_list[oc]; --i >= 0; ) *(p++) = *(q++); |
| 194 |
pic += l; |
| 195 |
while (--l >= 0) *(p++) = ' '; |
| 196 |
continue; |
| 197 |
|
| 198 |
case 'D': case 'd': /* Day number */ |
| 199 |
if (l == 1) goto DEFAULT; |
| 200 |
q = (*pic == 'D' ? zpic : dpic); |
| 201 |
if (l == 2) { p += ed_pic(p, q+2, (long)(T->tm_mday)); break;} |
| 202 |
l = 3; |
| 203 |
p += ed_pic(p, q+1, (long)((T->tm_yday%366) + 1)); |
| 204 |
break; |
| 205 |
|
| 206 |
case 'W': case 'w': /* Weekday name */ |
| 207 |
i = T->tm_wday; |
| 208 |
if ((i < 0) || (i >= 7)) |
| 209 |
{ p += oscfill(p, l, '~'); break; } |
| 210 |
if (l == 1) goto DEFAULT; |
| 211 |
# if SW_LEVEL |
| 212 |
oc = Day_name[language][i][1]; |
| 213 |
lc = Day_name[language][i][0]; |
| 214 |
# else |
| 215 |
oc = Day_name[i][1]; |
| 216 |
lc = Day_name[i][0]; |
| 217 |
# endif |
| 218 |
i = MIN(l,(int)lc); |
| 219 |
l -= i; |
| 220 |
lc = (*pic == 'w'); |
| 221 |
pic += i; |
| 222 |
for (q = &Day_list[oc]; --i >= 0; ) *(p++) = *(q++); |
| 223 |
pic += l; |
| 224 |
if ((lc) && (l > 0)) { *(p++) = ',', l--;} |
| 225 |
while (--l >= 0) *(p++) = ' '; |
| 226 |
continue; |
| 227 |
|
| 228 |
case 'h': /* Hour, 00...23 */ |
| 229 |
i = T->tm_hour % 24; |
| 230 |
ED2: |
| 231 |
if (l == 1) goto DEFAULT; |
| 232 |
p += ed_pic(p, zpic+2, (long)i); |
| 233 |
break; |
| 234 |
|
| 235 |
case 'm': /* Minutes, 00..59 */ |
| 236 |
i = T->tm_min % 60; |
| 237 |
goto ED2; |
| 238 |
|
| 239 |
case 's': /* Seconds, 00..59 */ |
| 240 |
i = T->tm_sec % 60; |
| 241 |
goto ED2; |
| 242 |
} |
| 243 |
pic += l; |
| 244 |
} |
| 245 |
|
| 246 |
*p = EOS; |
| 247 |
return(p-buffer); |
| 248 |
} |
| 249 |
|
| 250 |
/*========================================================================== |
| 251 |
* Callable Routines |
| 252 |
*==========================================================================*/ |
| 253 |
int ed_tm(buffer, pic, T) |
| 254 |
/*+++ |
| 255 |
.PURPOSE Edit the structure describing time & date (tm structure, see time1.h) |
| 256 |
according to a picture. |
| 257 |
.RETURNS Length of characters edited in buffer |
| 258 |
.REMARKS buffer is terminated with EOS. |
| 259 |
---------*/ |
| 260 |
char *buffer; /* OUT: Output buffer */ |
| 261 |
char *pic; /* IN: Used picture */ |
| 262 |
struct tm *T; /* IN: Decomposed time */ |
| 263 |
{ |
| 264 |
#if SW_LEVEL |
| 265 |
TeX = 0; /* No TeX option */ |
| 266 |
language = 0; /* Default is English */ |
| 267 |
#endif |
| 268 |
|
| 269 |
return(ed(buffer, pic, T)); |
| 270 |
} |
| 271 |
|
| 272 |
/*==========================================================================*/ |
| 273 |
int ed_t(buffer, pic, t) |
| 274 |
/*+++ |
| 275 |
.PURPOSE Edit the time (is in since Jan. 1, 1970) |
| 276 |
according to a picture. |
| 277 |
.RETURNS Length of characters edited in buffer / -1 if error |
| 278 |
.REMARKS buffer is terminated with EOS. |
| 279 |
---------*/ |
| 280 |
char *buffer; /* OUT: Output buffer */ |
| 281 |
char *pic; /* IN: Used picture */ |
| 282 |
long int t; /* IN: Time (s) */ |
| 283 |
{ |
| 284 |
register int ret; |
| 285 |
|
| 286 |
#if SW_LEVEL |
| 287 |
TeX = 0; /* No TeX option */ |
| 288 |
language = 0; /* French */ |
| 289 |
#endif |
| 290 |
|
| 291 |
if ((ret = tr_itm(t,&TM)) < 0) goto FIN; |
| 292 |
if ((ret = tr_tm(&TM)) < 0) goto FIN; |
| 293 |
ret = ed(buffer, pic, &TM); |
| 294 |
FIN: |
| 295 |
return(ret); |
| 296 |
} |
| 297 |
|
| 298 |
/* Other Functions Only Defined for Large SW_LEVEL */ |
| 299 |
#if SW_LEVEL |
| 300 |
/*==========================================================================*/ |
| 301 |
int ed_tm_F(buffer, pic, T) |
| 302 |
/*+++ |
| 303 |
.PURPOSE Edit the structure describing time & date (tm structure, see time1.h) |
| 304 |
according to a picture, in French |
| 305 |
.RETURNS Length of characters edited in buffer |
| 306 |
.REMARKS buffer is terminated with EOS. |
| 307 |
---------*/ |
| 308 |
char *buffer; /* OUT: Output buffer */ |
| 309 |
char *pic; /* IN: Used picture */ |
| 310 |
struct tm *T; /* IN: Decomposed time */ |
| 311 |
{ |
| 312 |
TeX = 0; /* No TeX option */ |
| 313 |
language = 2; /* French */ |
| 314 |
|
| 315 |
return(ed(buffer, pic, T)); |
| 316 |
} |
| 317 |
|
| 318 |
/*==========================================================================*/ |
| 319 |
int ed_tm_G(buffer, pic, T) |
| 320 |
/*+++ |
| 321 |
.PURPOSE Edit the structure describing time & date (tm structure, see time1.h) |
| 322 |
according to a picture, in German. |
| 323 |
.RETURNS Length of characters edited in buffer |
| 324 |
.REMARKS buffer is terminated with EOS. |
| 325 |
---------*/ |
| 326 |
char *buffer; /* OUT: Output buffer */ |
| 327 |
char *pic; /* IN: Used picture */ |
| 328 |
struct tm *T; /* IN: Decomposed time */ |
| 329 |
{ |
| 330 |
TeX = 0; /* No TeX option */ |
| 331 |
language = 1; /* German */ |
| 332 |
|
| 333 |
return(ed(buffer, pic, T)); |
| 334 |
} |
| 335 |
|
| 336 |
/*==========================================================================*/ |
| 337 |
int ed_t_F(buffer, pic, t) |
| 338 |
/*+++ |
| 339 |
.PURPOSE Edit the time (is in since Jan. 1, 1970) |
| 340 |
according to a picture, in French |
| 341 |
.RETURNS Length of characters edited in buffer / -1 if error |
| 342 |
.REMARKS buffer is terminated with EOS. |
| 343 |
---------*/ |
| 344 |
char *buffer; /* OUT: Output buffer */ |
| 345 |
char *pic; /* IN: Used picture */ |
| 346 |
long int t; /* IN: Time (s) */ |
| 347 |
{ |
| 348 |
register int ret; |
| 349 |
|
| 350 |
TeX = 0; /* No TeX option */ |
| 351 |
language = 2; /* French */ |
| 352 |
|
| 353 |
if ((ret = tr_itm(t,&TM)) < 0) goto FIN; |
| 354 |
if ((ret = tr_tm(&TM)) < 0) goto FIN; |
| 355 |
ret = ed(buffer, pic, &TM); |
| 356 |
FIN: |
| 357 |
return(ret); |
| 358 |
} |
| 359 |
|
| 360 |
/*==========================================================================*/ |
| 361 |
int ed_t_G(buffer, pic, t) |
| 362 |
/*+++ |
| 363 |
.PURPOSE Edit the time (is in since Jan. 1, 1970) |
| 364 |
according to a picture, in German |
| 365 |
.RETURNS Length of characters edited in buffer / -1 if error |
| 366 |
.REMARKS buffer is terminated with EOS. |
| 367 |
---------*/ |
| 368 |
char *buffer; /* OUT: Output buffer */ |
| 369 |
char *pic; /* IN: Used picture */ |
| 370 |
long int t; /* IN: Time (s) */ |
| 371 |
{ |
| 372 |
register int ret; |
| 373 |
|
| 374 |
TeX = 0; /* No TeX option */ |
| 375 |
language = 1; /* German */ |
| 376 |
|
| 377 |
if ((ret = tr_itm(t,&TM)) < 0) goto FIN; |
| 378 |
if ((ret = tr_tm(&TM)) < 0) goto FIN; |
| 379 |
ret = ed(buffer, pic, &TM); |
| 380 |
FIN: |
| 381 |
return(ret); |
| 382 |
} |
| 383 |
|
| 384 |
/*==========================================================================*/ |
| 385 |
int TeX_tm_F(buffer, pic, T) |
| 386 |
/*+++ |
| 387 |
.PURPOSE Edit the structure describing time & date (tm structure, see time1.h) |
| 388 |
according to a picture, in French and for TeX |
| 389 |
.RETURNS Length of characters edited in buffer |
| 390 |
.REMARKS buffer is terminated with EOS. |
| 391 |
---------*/ |
| 392 |
char *buffer; /* OUT: Output buffer */ |
| 393 |
char *pic; /* IN: Used picture */ |
| 394 |
struct tm *T; /* IN: Decomposed time */ |
| 395 |
{ |
| 396 |
TeX = 1; /* No TeX option */ |
| 397 |
language = 2; /* French */ |
| 398 |
|
| 399 |
return(ed(buffer, pic, T)); |
| 400 |
} |
| 401 |
|
| 402 |
/*==========================================================================*/ |
| 403 |
int TeX_tm_G(buffer, pic, T) |
| 404 |
/*+++ |
| 405 |
.PURPOSE Edit the structure describing time & date (tm structure, see time1.h) |
| 406 |
according to a picture, in German. |
| 407 |
.RETURNS Length of characters edited in buffer |
| 408 |
.REMARKS buffer is terminated with EOS. |
| 409 |
---------*/ |
| 410 |
char *buffer; /* OUT: Output buffer */ |
| 411 |
char *pic; /* IN: Used picture */ |
| 412 |
struct tm *T; /* IN: Decomposed time */ |
| 413 |
{ |
| 414 |
TeX = 1; /* No TeX option */ |
| 415 |
language = 1; /* German */ |
| 416 |
|
| 417 |
return(ed(buffer, pic, T)); |
| 418 |
} |
| 419 |
|
| 420 |
#endif |