00001
00002
00003
00004
00005
00006
00133
00134 #include <stdlib.h>
00135 #include <time.h>
00136 #include <errno.h>
00137 #include <stdio.h>
00138 #include "timeio.h"
00139 #include "jsoc.h"
00140 #include "cmdparams.h"
00141
00142 ModuleArgs_t module_args[] =
00143 {
00144 {ARG_STRING, "s", "NOT SPECIFIED", "<DSDS/JSOC time in seconds>"},
00145 {ARG_STRING, "sdo", "NOT SPECIFIED", "<SDO time in seconds>"},
00146 {ARG_STRING, "egse", "NOT SPECIFIED", "<EGSE time in seconds>"},
00147 {ARG_STRING, "time", "NOT SPECIFIED", "<Time as yyyy.mm...>"},
00148 {ARG_STRING, "ord", "NOT SPECIFIED", "<Time as yyyy.ddd...>"},
00149 {ARG_STRING, "zone", "UTC", "<Time zone>"},
00150 {ARG_STRING, "o", "NOT SPECIFIED", "format of time output"},
00151 {ARG_INT, "p", "0", "precision of seconds for time output"},
00152 {ARG_FLAG, "h", "0", "help message"},
00153 {ARG_END}
00154 };
00155
00156 ModuleArgs_t *gModArgs = module_args;
00157
00158
00159 CmdParams_t cmdparams;
00160
00161 #define SEC1970TO2004 1072828800
00162 #define SECSPERDAY 86400.0
00163
00164 typedef enum
00165 {
00166 kTimeFormat_None = 0,
00167 kTimeFormat_Internal,
00168 kTimeFormat_SDO,
00169 kTimeFormat_EGSE,
00170 kTimeFormat_Ordinal,
00171 kTimeFormat_Calendar
00172 } TimeFormat_t;
00173
00174 char *TimeFormatStr[] =
00175 {
00176 "None",
00177 "Internal/JSOC",
00178 "SDO",
00179 "EGSE",
00180 "Ordinal",
00181 "Calendar"
00182 };
00183
00184 static void PrintTime(TIME t, TimeFormat_t f, int precision);
00185 static TIME ZoneAdjustment(TIME t, char *zone, int precision);
00186
00187 int nice_intro ()
00188 {
00189 int usage = cmdparams_get_int (&cmdparams, "h", NULL);
00190 if (usage)
00191 {
00192 printf ("Usage:\ntime_convert -h\n"
00193 "time_convert s=<secondsJSOC>|sdo=<secondsSDO>|egse=<secondsEGSE>|time=<calender time>|ord=<ordinal date> [o=jsoc | o=sdo | o=egse | o=ord | o=cal] [zone=<zone>]\n"
00194 "<secondsJSOC> = JSOC standard internal time, i.e. secs since 1977.01.01_TAI \n"
00195 "<secondsSDO> = seconds since January 1, 1958 TAI, i.e. SDO onboard time\n"
00196 "<secondsEGSE> = seconds since APPROXIMATELY January 1, 2004 \n"
00197 "<calender time> = yyyy.mm.dd_hh:mm:ss<zone>\n"
00198 "<ordinal date> = yyyy.ddd<zone>\n"
00199 "<zone> = time zone as UT, TAI, PST, etc. - default is UTC\n"
00200 "h - show usage message\n"
00201 "o - output time in format specified\n"
00202 "p - precision of seconds printed\n");
00203 return(1);
00204 }
00205 return (0);
00206 }
00207
00208 int main(int argc, char **argv)
00209 {
00210 int status;
00211 TIME sscan_time(char *s);
00212
00213 char *s, *sdo, *time;
00214 char *ord;
00215 char *egse = NULL;
00216 char *oArg = NULL;
00217 int precision = 0;
00218
00219 TIME t;
00220
00221
00222 status = cmdparams_parse (&cmdparams, argc, argv);
00223 if (status == CMDPARAMS_QUERYMODE)
00224 {
00225 cmdparams_usage (argv[0]);
00226 return 0;
00227 }
00228
00229 if (nice_intro ())
00230 return (0);
00231
00232
00233 s = cmdparams_get_str(&cmdparams, "s", NULL);
00234 sdo = cmdparams_get_str(&cmdparams, "sdo", NULL);
00235 time = cmdparams_get_str(&cmdparams, "time", NULL);
00236 ord = cmdparams_get_str(&cmdparams, "ord", NULL);
00237 egse = cmdparams_get_str(&cmdparams, "egse", NULL);
00238
00239
00240 precision = cmdparams_get_int(&cmdparams, "p", NULL);
00241
00242 int err = 0;
00243
00244 TIME timeToPrint;
00245 TimeFormat_t inputFormat = kTimeFormat_None;
00246 TimeFormat_t outputFormat = kTimeFormat_None;
00247 TimeFormat_t outputOverride = kTimeFormat_None;
00248 int mFormats = 0;
00249
00250
00251 oArg = cmdparams_get_str(&cmdparams, "o", NULL);
00252 if (strcmp(oArg, "NOT SPECIFIED") != 0)
00253 {
00254 if (strcmp(oArg, "jsoc") == 0)
00255 {
00256 outputOverride = kTimeFormat_Internal;
00257 }
00258 else if (strcmp(oArg, "sdo") == 0)
00259 {
00260 outputOverride = kTimeFormat_SDO;
00261 }
00262 else if (strcmp(oArg, "egse") == 0)
00263 {
00264 outputOverride = kTimeFormat_EGSE;
00265 }
00266 else if (strcmp(oArg, "ord") == 0)
00267 {
00268 outputOverride = kTimeFormat_Ordinal;
00269 }
00270 else if (strcmp(oArg, "cal") == 0)
00271 {
00272 outputOverride = kTimeFormat_Calendar;
00273 }
00274 else
00275 {
00276 fprintf(stderr, "Output format %s is not recognized. Using default.\n", oArg);
00277 }
00278 }
00279
00280 if (strcmp(s, "NOT SPECIFIED")!= 0)
00281 {
00282
00283 if (inputFormat == kTimeFormat_None)
00284 {
00285 sscanf(s, "%lf", &t);
00286 timeToPrint = t;
00287 outputFormat = kTimeFormat_Calendar;
00288 inputFormat = kTimeFormat_Internal;
00289 }
00290 else
00291 {
00292 mFormats = 1;
00293 }
00294 }
00295 if (strcmp(sdo, "NOT SPECIFIED")!= 0)
00296 {
00297
00298 if (inputFormat == kTimeFormat_None)
00299 {
00300 sscanf(sdo, "%lf", &t);
00301 timeToPrint = t + sscan_time("1958.01.01_00:00:00_TAI");
00302 outputFormat = kTimeFormat_Calendar;
00303 inputFormat = kTimeFormat_SDO;
00304 }
00305 else
00306 {
00307 mFormats = 1;
00308 }
00309 }
00310 if (strcmp(egse, "NOT SPECIFIED") != 0)
00311 {
00312
00313 if (inputFormat == kTimeFormat_None)
00314 {
00315 sscanf(egse, "%lf", &t);
00316 timeToPrint = t + SEC1970TO2004 + sscan_time("1970.01.01_00:00_UTC");
00317 outputFormat = kTimeFormat_Calendar;
00318 inputFormat = kTimeFormat_EGSE;
00319 }
00320 else
00321 {
00322 mFormats = 1;
00323 }
00324 }
00325 if (strcmp(time, "NOT SPECIFIED")!= 0)
00326 {
00327
00328 if (inputFormat == kTimeFormat_None)
00329 {
00330 t = sscan_time(time);
00331 timeToPrint = t;
00332 outputFormat = kTimeFormat_Internal;
00333 inputFormat = kTimeFormat_Calendar;
00334 }
00335 else
00336 {
00337 mFormats = 1;
00338 }
00339 }
00340 if (ord != NULL && *ord != NULL && strcmp(ord, "NOT SPECIFIED") != 0)
00341 {
00342
00343 if (inputFormat == kTimeFormat_None)
00344 {
00345 err = 1;
00346
00347 long year = 0;
00348 int day = 0;
00349 char *tFormat = NULL;
00350
00351 char *ordDate = strdup(ord);
00352 int timeStrLen = strlen(ordDate);
00353
00354 if (ordDate != NULL)
00355 {
00356 char *loc = strchr(ordDate, '.');
00357 char *loc2 = strchr(ordDate, '_');
00358
00359 if (loc != NULL)
00360 {
00361 *loc = '\0';
00362
00363 if (sscanf(ordDate, "%ld", &year) != 0)
00364 {
00365 if (loc2 != NULL && loc2 > loc)
00366 {
00367 *loc2 = '\0';
00368 sscanf(loc + 1, "%d", &day);
00369
00370 if (loc2 + 1 <= &ordDate[timeStrLen - 1])
00371 {
00372 tFormat = (char *)malloc(sizeof(char) * strlen(loc2 + 1) + 1);
00373 if (tFormat)
00374 {
00375 sscanf(loc2 + 1, "%s", tFormat);
00376 }
00377 }
00378 }
00379 else if (loc + 1 <= &ordDate[timeStrLen - 1])
00380 {
00381 sscanf((loc + 1), "%d", &day);
00382 }
00383
00384 err = (day < 1 || day > 366);
00385
00386 if (!err)
00387 {
00388
00389 char timeBuf[64];
00390 snprintf(timeBuf, sizeof(timeBuf), "%ld.01.%d_00:00_%s", year, day, tFormat ? tFormat : "UT");
00391
00392
00393
00394 t = sscan_time(timeBuf);
00395 timeToPrint = t;
00396 outputFormat = kTimeFormat_Internal;
00397 inputFormat = kTimeFormat_Ordinal;
00398 }
00399 else
00400 {
00401 fprintf(stderr, "invalid day of year\n");
00402 }
00403 }
00404 else
00405 {
00406 fprintf(stderr, "invalid year\n");
00407 }
00408 }
00409 else
00410 {
00411 fprintf(stderr, "missing day of year\n");
00412 }
00413
00414 free(ordDate);
00415 }
00416
00417 if (tFormat)
00418 {
00419 free(tFormat);
00420 }
00421 }
00422 else
00423 {
00424 mFormats = 1;
00425 }
00426 }
00427
00428 if (inputFormat == kTimeFormat_None)
00429 {
00430 fprintf(stderr, "No input format specified.\n");
00431 err = 1;
00432 }
00433 else if (mFormats)
00434 {
00435 fprintf(stderr,
00436 "Multiple input formats specified, using %s time\n",
00437 TimeFormatStr[inputFormat]);
00438 }
00439
00440 if (outputOverride != kTimeFormat_None)
00441 {
00442 outputFormat = outputOverride;
00443 }
00444
00445 if (!err)
00446 {
00447 PrintTime(timeToPrint, outputFormat, precision);
00448 }
00449
00450 if (err)
00451 {
00452 fprintf(stderr,"time_convert call error\n");
00453 return(1);
00454 }
00455 else
00456 {
00457 return(0);
00458 }
00459 }
00460
00461
00462 void PrintTime(TIME t, TimeFormat_t f, int precision)
00463 {
00464 if (f == kTimeFormat_Calendar)
00465 {
00466 char at[128];
00467 sprint_time(at, t, cmdparams_get_str(&cmdparams, "zone", NULL), precision);
00468
00469
00470 printf("%s\n", at);
00471 }
00472 else if (f == kTimeFormat_SDO)
00473 {
00474
00475 printf("%12.*f\n", precision, t - sscan_time("1958.01.01_00:00:00_TAI"));
00476 }
00477 else if (f == kTimeFormat_EGSE)
00478 {
00479
00480 printf("%12.*f\n", precision, t - SEC1970TO2004 - sscan_time("1970.01.01_00:00_UTC"));
00481 }
00482 else if (f == kTimeFormat_Ordinal)
00483 {
00484
00485 int ordTimeDays = 0;
00486 char zoneStr[32];
00487 char at[128];
00488 TIME jan1 = 0;
00489 TIME secsSinceJan1 = 0;
00490
00491 char *zone = cmdparams_get_str(&cmdparams, "zone", NULL);
00492
00493
00494 sprint_time(at, t, zone, precision);
00495 char *tz = strrchr(at, '_');
00496
00497 if (tz)
00498 {
00499 snprintf(zoneStr, sizeof(zoneStr), "_%s", tz + 1);
00500 }
00501
00502 char *dot = strchr(at, '.');
00503 if (dot != NULL)
00504 {
00505 *dot = '\0';
00506 char timeStr[64];
00507 snprintf(timeStr, sizeof(timeStr), "%s.01.01_00:00:00%s", at, zoneStr);
00508 jan1 = sscan_time(timeStr);
00509 }
00510
00511 secsSinceJan1 = t - jan1;
00512
00513
00514
00515 TIME adjJan1 = ZoneAdjustment(jan1, zone, precision);
00516 TIME adjT = ZoneAdjustment(t, zone, precision);
00517 TIME ordTimeSecs = secsSinceJan1 - (adjT - adjJan1);
00518
00519
00520 ordTimeDays = 1 + ordTimeSecs / SECSPERDAY;
00521
00522
00523 printf("%s.%03d%s\n", at, ordTimeDays, zoneStr);
00524 }
00525 else
00526 {
00527 if (f != kTimeFormat_Internal)
00528 {
00529
00530 fprintf(stderr, "Invalid output format, defaulting to JSOC time.\n");
00531 }
00532 printf("%0.*f\n", precision, t);
00533 }
00534 }
00535
00536
00537 TIME ZoneAdjustment(TIME t, char *zone, int precision)
00538 {
00539 char timestr[128];
00540 sprint_time(timestr, t, zone, precision);
00541
00542 char *tz = strrchr(timestr, '_');
00543 tz[1] = 'T';
00544 tz[2] = 'A';
00545 tz[3] = 'I';
00546 tz[4] = '\0';
00547
00548 TIME taiDateSecs = sscan_time(timestr);
00549 return t - taiDateSecs;
00550 }