LAL  7.5.0.1-08ee4f4
tconvert.c
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2007 Jolien Creighton
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (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 License
15 * along with with program; see the file COPYING. If not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301 USA
18 */
19 
20 #ifndef _GNU_SOURCE
21 #define _GNU_SOURCE /* for setenv() and putenv() */
22 #endif
23 
24 #include <stdlib.h>
25 #include <ctype.h>
26 #include <errno.h>
27 #include <math.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <time.h>
31 
32 #include <lal/LALStdlib.h>
33 #include <lal/LALgetopt.h>
34 #include <lal/LALConstants.h>
35 #include <lal/Date.h>
36 #include "getdate.h"
37 #include <LALVCSInfo.h>
38 
39 #include "config.h"
40 
41 #ifdef __GNUC__
42 #define UNUSED __attribute__ ((unused))
43 #else
44 #define UNUSED
45 #endif
46 
47 #define MAX_DATE_STRING_LENGTH 256
48 
49 /* Longitudes of LHO and LLO taken from LIGO-T980044-08 */
50 #define LHO_LONGITUDE_RAD_E (LAL_PI*(-119.0+(24.0+27.5657/60.0)/60.0)/180.0)
51 #define LLO_LONGITUDE_RAD_E (LAL_PI*(-90.0+(46.0+27.2654/60.0)/60.0)/180.0)
52 
53 static const char * skip_space( const char *s )
54 {
55  while ( s && *s && isspace( *s ) )
56  ++s;
57  return s;
58 }
59 
60 static int is_integer( const char *s )
61 {
62  if ( ! s )
63  return -1;
64  s = skip_space( s );
65  if ( ! *s ) /* it was blank, not an integer! */
66  return 0;
67  while ( *s && isdigit( *s ) )
68  ++s;
69  s = skip_space( s );
70  return *s ? 0 : 1;
71 }
72 
73 static int is_blank( const char *s )
74 {
75  s = skip_space( s );
76  return *s ? 0 : 1;
77 }
78 
79 void output( int gps_sec, int output_type );
80 void usage( const char *program, int exitcode );
81 char * parse_options( char *buf, int buflen, int argc, char **argv );
82 int unix_to_gps( time_t unix_sec );
83 time_t gps_to_unix( int gps_sec, int *leap );
84 void set_zone( const char *zone );
85 void reset_zone( void );
86 void set_zone_lho( void );
87 void set_zone_llo( void );
88 
89 /* standard formats */
90 const char *iso_8601_format = "%Y-%m-%dT%H:%M:%S%z";
91 const char *rfc_822_format = "%a, %d %b %Y %H:%M:%S %z";
92 const char *unix_date_format = "%a %b %d %H:%M:%S %Z %Y";
93 const char *utc_unix_date_format = "%a %b %d %H:%M:%S UTC %Y";
94 const char *output_date_format = NULL;
95 const char *tz = NULL;
96 
97 enum { GPS_EPOCH, UNIX_EPOCH } epoch = GPS_EPOCH; /* default is GPS epoch */
98 enum { SIDEREAL_HMS, SIDEREAL_RAD } sidereal_format = SIDEREAL_HMS; /* default isHH:MM:SS */
101 int utc_flag = 1; /* default is universal time rather than local time */
102 
103 int verbose = 0; /* default is brief output */
104 
105 FILE *fp = NULL; /* set to stdin to read dates from stdin */
106 
107 int main( int argc, char *argv[] )
108 {
109  char buf[1024] = "";
110  char * arg; /* argument after options have been parsed */
111  time_t unix_sec;
112  int gps_sec;
113  int leap;
114 
116 
117  errno = 0;
118  arg = parse_options( buf, sizeof( buf ) - 1, argc, argv );
119  if ( ! arg )
120  {
121  if ( errno )
122  perror( "parse options" );
123  usage( argv[0], 1 );
124  }
125  if ( tz )
126  set_zone( tz );
127 
128  while ( arg )
129  {
130  if ( is_integer( arg ) ) /* argument is an integer seconds since epoch */
131  {
132  if ( output_type == OUTPUT_DEFAULT && ! verbose )
134  switch ( epoch )
135  {
136  case GPS_EPOCH:
137  gps_sec = atoi( arg );
138  break;
139  case UNIX_EPOCH:
140  unix_sec = atoi( arg );
141  gps_sec = unix_to_gps( unix_sec );
142  break;
143  default:
144  fprintf( stderr, "internal error: unrecognized epoch\n" );
145  exit( 1 );
146  }
147  }
148  else /* assume argument is a date string */
149  {
150  if ( output_type == OUTPUT_DEFAULT && ! verbose )
152 
153  if ( utc_flag )
154  set_zone( "00" ); /* make zone UTC for date parsing unless local */
155 
156  if ( is_blank( arg ) )
157  unix_sec = get_date( "now", 0 );
158  else
159  unix_sec = get_date( arg, 0 );
160 
161  if ( utc_flag )
162  set_zone( tz ); /* if we set zone, set it back now */
163 
164  /* need to check to see if there was a leap second */
165  leap = 0;
166  if ( strstr( arg, ":59:60" ) ) /* aha! there was one! */
167  {
168  --unix_sec; /* to distinguish this from next day */
169  leap = 1; /* add the leap second back in later */
170  }
171 
172  if ( unix_sec < 0 )
173  {
174  fprintf( stderr, "could not parse date string: %s\n", arg );
175  exit( 1 );
176  }
177  gps_sec = unix_to_gps( unix_sec );
178  gps_sec += leap; /* the leap second */
179  }
180 
181  output( gps_sec, output_type );
182 
183  if ( fp )
184  arg = fgets( buf, sizeof( buf ) - 1, fp );
185  else
186  break;
187  }
188 
189  return 0;
190 }
191 
192 
193 static void output_secs( int gps_sec )
194 {
195  time_t unix_sec;
196  int leap;
197  switch ( epoch )
198  {
199  case GPS_EPOCH:
200  if ( verbose )
201  printf( "GPS Time = " );
202  printf( "%d\n", gps_sec );
203  break;
204  case UNIX_EPOCH:
205  if ( verbose )
206  printf( "UNIX Time = " );
207  unix_sec = gps_to_unix( gps_sec, &leap );
208  printf( "%d", (int)unix_sec );
209  if ( leap )
210  {
211  if ( verbose )
212  printf( " + 1 leap second" );
213  else
214  printf( "+1" );
215  }
216  printf( "\n" );
217  break;
218  default:
219  fprintf( stderr, "internal error: unrecognized epoch\n" );
220  exit( 1 );
221  }
222  return;
223 }
224 
225 static void output_date( int gps_sec )
226 {
227  char date_string[256];
228  struct tm *tp;
229  time_t unix_sec;
230  const char *fmt;
231  int leap;
232 
234  unix_sec = gps_to_unix( gps_sec, &leap );
235  if ( utc_flag )
236  tp = gmtime( &unix_sec );
237  else
238  tp = localtime( &unix_sec );
239  if ( leap )
240  ++tp->tm_sec;
241 
242  if ( utc_flag )
243  set_zone( "00" );
244 
245  strftime( date_string, sizeof( date_string ), fmt, tp );
246 
247  if ( utc_flag )
248  set_zone( tz );
249 
250  puts( date_string );
251 
252  return;
253 }
254 
255 static void output_date_utc( int gps_sec )
256 {
257  int utc_flag_save = utc_flag;
258  utc_flag = 1;
259  if ( verbose )
260  printf( "Coordinated Universal Time: " );
261  output_date( gps_sec );
262  utc_flag = utc_flag_save;
263  return;
264 }
265 
266 static void output_date_local( int gps_sec )
267 {
268  int utc_flag_save = utc_flag;
269  utc_flag = 0;
270  if ( verbose )
271  printf( "Local Time: " );
272  output_date( gps_sec );
273  utc_flag = utc_flag_save;
274  return;
275 }
276 
277 static void output_lho_date( int gps_sec )
278 {
279  set_zone_lho();
280  if ( verbose )
281  printf( "LHO " );
282  output_date_local( gps_sec );
283  set_zone( tz );
284 }
285 
286 static void output_llo_date( int gps_sec )
287 {
288  set_zone_llo();
289  if ( verbose )
290  printf( "LLO " );
291  output_date_local( gps_sec );
292  set_zone( tz );
293 }
294 
295 static void output_date_site( int gps_sec )
296 {
297  switch ( site )
298  {
299  case SITE_LHO:
300  output_lho_date( gps_sec );
301  break;
302  case SITE_LLO:
303  output_llo_date( gps_sec );
304  break;
305  case SITE_UNSPECIFIED:
306  output_date( gps_sec );
307  break;
308  default:
309  fprintf( stderr, "internal error: unrecognized site\n" );
310  exit( 1 );
311  }
312  return;
313 }
314 
315 static void output_jd( int gps_sec )
316 {
317  struct tm utc;
318  double jd;
319  utc = *XLALGPSToUTC( &utc, gps_sec );
320  jd = XLALConvertCivilTimeToJD( &utc );
321  if ( verbose )
322  printf( "Julian Day (UTC) = " );
323  printf( "%.6f\n", jd );
324  return;
325 }
326 
327 static void output_mjd( int gps_sec )
328 {
329  struct tm utc;
330  double mjd;
331  utc = *XLALGPSToUTC( &utc, gps_sec );
332  mjd = XLALConvertCivilTimeToMJD( &utc );
333  if ( verbose )
334  printf( "Modified Julian Day (UTC) = " );
335  printf( "%.6f\n", mjd );
336  return;
337 }
338 
339 static void output_leaps( int gps_sec )
340 {
341  int leaps;
342  leaps = XLALLeapSeconds( gps_sec );
343  if ( verbose )
344  printf( "Leap Seconds (TAI-UTC) = " );
345  printf( "%d\n", leaps );
346  return;
347 }
348 
349 static void output_sidereal_time( double sidereal_time_rad )
350 {
351  double sidereal_time_hrs;
352  double sidereal_time_min;
353  double sidereal_time_sec;
354  while ( sidereal_time_rad >= 2.0 * LAL_PI )
355  sidereal_time_rad -= 2.0 * LAL_PI;
356  while ( sidereal_time_rad < 0 )
357  sidereal_time_rad += 2.0 * LAL_PI;
358  switch ( sidereal_format )
359  {
360  case SIDEREAL_HMS:
361  sidereal_time_hrs = 12.0 * sidereal_time_rad / LAL_PI;
362  sidereal_time_min = 60.0 * modf( sidereal_time_hrs, &sidereal_time_hrs );
363  sidereal_time_sec = 60.0 * modf( sidereal_time_min, &sidereal_time_min );
364  sidereal_time_sec = floor( sidereal_time_sec + 0.5 ); /* round to nearest second */
365  if ( verbose )
366  printf( "Sidereal Time (HH:MM:SS) = " );
367  printf( "%02.0f:%02.0f:%02d\n", sidereal_time_hrs, sidereal_time_min, (int)sidereal_time_sec );
368  break;
369  case SIDEREAL_RAD:
370  if ( verbose )
371  printf( "Sidereal Time (radians) = " );
372  printf( "%.6f\n", sidereal_time_rad );
373  break;
374  default:
375  fprintf( stderr, "internal error: unrecognized sidereal time format\n" );
376  exit( 1 );
377  }
378  return;
379 }
380 
381 static void output_gmst( int gps_sec )
382 {
383  LIGOTimeGPS ligo_time;
384  double gmst_rad;
385  ligo_time.gpsSeconds = gps_sec;
386  ligo_time.gpsNanoSeconds = 0;
387  gmst_rad = XLALGreenwichMeanSiderealTime( &ligo_time );
388  if ( verbose )
389  printf( "Greenwich Mean " );
390  output_sidereal_time( gmst_rad );
391  return;
392 }
393 
394 static void output_local_sidereal_time_lho( int gps_sec )
395 {
396  LIGOTimeGPS ligo_time;
397  double sidereal_time_rad;
398  ligo_time.gpsSeconds = gps_sec;
399  ligo_time.gpsNanoSeconds = 0;
400  sidereal_time_rad = XLALGreenwichMeanSiderealTime( &ligo_time );
401  sidereal_time_rad += LHO_LONGITUDE_RAD_E;
402  if ( verbose )
403  printf( "LHO Local " );
404  output_sidereal_time( sidereal_time_rad );
405 }
406 
407 static void output_local_sidereal_time_llo( int gps_sec )
408 {
409  LIGOTimeGPS ligo_time;
410  double sidereal_time_rad;
411  ligo_time.gpsSeconds = gps_sec;
412  ligo_time.gpsNanoSeconds = 0;
413  sidereal_time_rad = XLALGreenwichMeanSiderealTime( &ligo_time );
414  sidereal_time_rad += LLO_LONGITUDE_RAD_E;
415  if ( verbose )
416  printf( "LLO Local " );
417  output_sidereal_time( sidereal_time_rad );
418 }
419 
420 static void output_sidereal_time_site( int gps_sec )
421 {
422  switch ( site )
423  {
424  case SITE_LHO:
426  break;
427  case SITE_LLO:
429  break;
430  case SITE_UNSPECIFIED:
431  output_gmst( gps_sec );
432  break;
433  default:
434  fprintf( stderr, "internal error: unrecognized site\n" );
435  exit( 1 );
436  }
437  return;
438 }
439 
440 void output( int gps_sec, int type )
441 {
442  switch ( type )
443  {
444  case OUTPUT_DATE:
445  output_date_site( gps_sec );
446  break;
447  case OUTPUT_SECS:
448  output_secs( gps_sec );
449  break;
450  case OUTPUT_JD:
451  output_jd( gps_sec );
452  break;
453  case OUTPUT_MJD:
454  output_mjd( gps_sec );
455  break;
456  case OUTPUT_LEAPS:
457  output_leaps( gps_sec );
458  break;
459  case OUTPUT_GMST:
460  output_sidereal_time_site( gps_sec );
461  break;
462  case OUTPUT_DEFAULT:
463  if ( verbose )
464  {
465  int save_epoch = epoch;
466  output_date_utc( gps_sec );
467  output_date_local( gps_sec );
468  output_lho_date( gps_sec );
469  output_llo_date( gps_sec );
470  epoch = GPS_EPOCH;
471  output_secs( gps_sec );
472  epoch = UNIX_EPOCH;
473  output_secs( gps_sec );
474  epoch = save_epoch;
475  output_leaps( gps_sec );
476  output_jd( gps_sec );
477  output_mjd( gps_sec );
478  output_gmst( gps_sec );
481  break;
482  }
483  /* fall-through */
484  default:
485  fprintf( stderr, "internal error: unrecognized output type\n" );
486  exit( 1 );
487  }
488  return;
489 }
490 
491 /*
492  * If this is a leap second, return the unix time of the previous second
493  * and set *leap to 1. Thus if you add the return value and *leap you'll
494  * have the actual unix time. This is done since the two times, say
495  * 16:59:60 of some day and 17:00:00 of the same day have the same unix
496  * time. If this routine gets a gps_sec corresponding to 16:59:60, it will
497  * return a unix_sec corresponding to 16:59:59 and will set *leap to 1.
498  * However if it gets a gps time corresponding to 17:00:00, it will return
499  * a unix_sec corresponding to 17:00:00 and will set *leap to 0.
500  */
501 time_t gps_to_unix( int gps_sec, int *leap )
502 {
503  struct tm utc;
504  time_t unix_sec;
505 
506  utc = *XLALGPSToUTC( &utc, gps_sec );
507  if ( leap ) /* worry about whether this second was a leap second */
508  {
509  *leap = 0;
510  if ( utc.tm_sec == 60 )
511  {
512  --utc.tm_sec;
513  *leap = 1;
514  }
515  }
516 
517  /* posix definition of the unix time */
518  unix_sec = XLALSecondsSinceUnixEpoch( &utc );
519 
520  return unix_sec;
521 }
522 
523 int unix_to_gps( time_t unix_sec )
524 {
525  struct tm *tp;
526  int gps_sec;
527 
528  tp = gmtime( &unix_sec );
529  gps_sec = XLALUTCToGPS( tp );
530 
531  return gps_sec;
532 }
533 
534 /* Sets TZ environment to a particular zone string, e.g., "US/Pacific".
535  * Lots of static variables here... these need to be static since they
536  * become part of the environment. The argument is copied.
537  * Pass NULL to this function to reset TZ to its original value.
538  * Note: this function is ugly and a little bit dangerous, perhaps.*/
539 #define MAX_ENV_SIZE 1024
540 void set_zone( const char *zone )
541 {
542 #ifdef HAVE_SETENV
543  static int first = 1;
544  static char *tz_env_orig = NULL;
545  if ( first )
546  {
547  first = 0;
548  tz_env_orig = getenv( "TZ" );
549  }
550 
551  if ( zone )
552  setenv( "TZ", zone, 1 );
553  else /* reset to original */
554  {
555  if ( tz_env_orig )
556  setenv( "TZ", tz_env_orig, 1 );
557  else
558  unsetenv( "TZ" );
559  }
560 
561  return;
562 #else
563  static int first = 1;
564  static char tz_equals_string[] = "TZ=";
565  static char tz_string[] = "TZ";
566  static char tz_env[MAX_ENV_SIZE];
567  static char *tz_env_orig = NULL;
568 
569  if ( first )
570  {
571  first = 0;
572  tz_env_orig = getenv( "TZ" );
573  }
574 
575  if ( zone )
576  {
577  strncpy( tz_env, "TZ=", sizeof( tz_env ) - 1 );
578  strncat( tz_env, zone, sizeof( tz_env ) - strlen( tz_env ) - 1 );
579  putenv( tz_env );
580  }
581  else /* reset to original */
582  {
583  if ( tz_env_orig ) /* TZ was originally set */
584  {
585  strncpy( tz_env, "TZ=", sizeof( tz_env ) - 1 );
586  strncat( tz_env, tz_env_orig, sizeof( tz_env ) - strlen( tz_env ) - 1 );
587  putenv( tz_env );
588  }
589  else /* try to reset */
590  {
591  putenv( tz_equals_string ); /* set TZ to blank in case can't unset */
592  putenv( tz_string ); /* this will unset TZ if possible */
593  strcpy( tz_equals_string, "" ); /* a real hack at unsetting otherwise */
594  }
595  }
596 
597  return;
598 #endif
599 }
600 
601 void reset_zone( void )
602 {
603  set_zone( NULL );
604  return;
605 }
606 
607 void set_zone_lho( void )
608 {
609  set_zone( "PST8PDT" );
610  return;
611 }
612 
613 void set_zone_llo( void )
614 {
615  set_zone( "CST6CDT" );
616  return;
617 }
618 
619 
620 void usage( const char *program, int exitcode )
621 {
622  const char *name = "Name:\n\t%s\n\n";
623  const char *synopsis = "Synopsis:\n\t%s [OPTIONS] DATE_STRING\n\t%s [OPTIONS] TIMER_SECONDS\n\n";
624  const char *description = "Description:\n\
625  Convert between various representations of time.\n\n";
626  const char *options = "Options:\n\n\
627  -d, --date\n\
628  output a date string [default when input is timer seconds]\n\
629  \n\
630  -fFORMAT, --format=FORMAT\n\
631  output format for date string [see date(1)]\n\
632  \n\
633  -G, --gps-epoch\n\
634  use GPS epoch for timer (1980-01-06 00:00:00 UTC) [default]\n\
635  \n\
636  -I, --iso-8601\n\
637  output date string in ISO 8601 format\n\
638  \n\
639  -j, --jd, --julian-day\n\
640  output Julian day (in UTC time system)\n\
641  \n\
642  -l, --leap-seconds\n\
643  output number of leap seconds (TAI-UTC)\n\
644  \n\
645  -L, --local\n\
646  date strings formatted for local time (current TZ setting)\
647  \n\
648  -m, --mjd, --modified-julian-day\n\
649  output modified Julian day (in UTC time system)\n\
650  \n\
651  -R, --rfc-2822\n\
652  output RFC-2822 compliant date string\n\
653  \n\
654  -s, --sidereal-time[= HMS|RAD]\n\
655  print Greenwich mean sidreal time [default format HH:MM:SS]\n\
656  \n\
657  -S, --stdin\n\
658  read from standard input\n\
659  \n\
660  -t, --timer\n\
661  output timer seconds [default when input is a date string]\n\
662  \n\
663  -u, --utc\n\
664  date strings formatted for Coordinated Universal Time [default]\n\
665  \n\
666  -U, --unix-epoch\n\
667  use Unix epoch for timer (1970-01-01 00:00:00 UTC)\n\
668  \n\
669  -zTZ, --zone=TZ\n\
670  set time zone to TZ (use with --local)\n\
671  \n\
672  -ZSITE, --site=SITE\n\
673  set site to SITE which is [valid sites: LHO | LLO]\n\
674  \n\
675  --help display this help and exit\n\
676  \n\
677  --version\n\
678  output version information and exit\n\n";
679  const char *environment = "Environment Variables:\n\
680  TZ The timezone to use when parsing or displaying dates.\n\n";
681  const char *examples = "Examples:\n\
682  Get current time in GPS seconds:\n\
683  \n\
684  %s now\n\
685  \n\
686  Get local time corresponding to GPS time 800000000 in RFC 2822 format:\n\
687  \n\
688  %s --rfc-2822 --local 800000000\n\
689  \n\
690  Find the local sidereal time at LHO three hours ago:\n\
691  \n\
692  %s --sidereal-time --site=LHO 3 hours ago\n\
693  \n\
694  Get the Julian day(UTC) of Y2K (UTC is default unless --local is used):\n\
695  \n\
696  %s --julian-day Jan 1, 2000\n\
697  \n\
698  Find out all about now:\n\
699  \n\
700  %s --verbose\n\
701  \n";
702  fprintf( stdout, name, program );
703  fprintf( stdout, synopsis, program, program );
704  fprintf( stdout, "%s", description );
705  fprintf( stdout, "%s", options );
706  fprintf( stdout, "%s", environment );
707  fprintf( stdout, examples, program, program, program, program, program );
708  exit( exitcode );
709 }
710 
711 
712 char * parse_options( char *buf, int buflen, int argc, char **argv )
713 {
714  struct LALoption long_options[] =
715  {
716  { "date", no_argument, 0, 'd' },
717  { "format", required_argument, 0, 'f' },
718  { "gps-epoch", no_argument, 0, 'G' },
719  { "help", no_argument, 0, 'h' },
720  { "iso-8601", no_argument, 0, 'I' },
721  { "jd", no_argument, 0, 'j' },
722  { "julian-day", no_argument, 0, 'j' },
723  { "leap-seconds", no_argument, 0, 'l' },
724  { "local", no_argument, 0, 'L' },
725  { "mjd", no_argument, 0, 'm' },
726  { "modified-julian-day", no_argument, 0, 'm' },
727  { "rfc-2822", no_argument, 0, 'R' },
728  { "sidereal-time", optional_argument, 0, 's' },
729  { "stdin", no_argument, 0, 'S' },
730  { "utc", no_argument, 0, 'u' },
731  { "universal", no_argument, 0, 'u' },
732  { "unix-epoch", no_argument, 0, 'U' },
733  { "timer", no_argument, 0, 't' },
734  { "verbose", no_argument, 0, 'v' },
735  { "version", no_argument, 0, 'V' },
736  { "zone", required_argument, 0, 'z' },
737  { "site", required_argument, 0, 'Z' },
738  { 0, 0, 0, 0 }
739  };
740  char args[] = "+df:GhIjlLmRsSuUvVW:z:Z:";
741  char *program = argv[0];
742 
743  while ( 1 )
744  {
745  int option_index = 0;
746  int c;
747 
748  c = LALgetopt_long_only( argc, argv, args, long_options, &option_index );
749  if ( c == -1 ) /* end of options */
750  break;
751 
752  switch ( c )
753  {
754 
755  case 0: /* if option set a flag, nothing else to do */
756  if ( ! long_options[option_index].flag )
757  {
758  fprintf( stderr, "error parsing option %s with argument %s\n",
759  long_options[option_index].name, LALoptarg );
760  usage( program, 1 );
761  }
762  break;
763 
764  case 'd': /* date */
766  break;
767 
768  case 'f': /* format */
770  break;
771 
772  case 'G': /* gps-epoch */
773  epoch = GPS_EPOCH;
774  break;
775 
776  case 'h': /* help */
777  usage( program, 0 );
778  break;
779 
780  case 'I': /* iso-8601 */
782  break;
783 
784  case 'j': /* julian-day */
786  break;
787 
788  case 'l': /* leap-seconds */
790  break;
791 
792  case 'L': /* local time */
793  utc_flag = 0;
794  break;
795 
796  case 'm': /* mjd */
798  break;
799 
800  case 'R': /* rfc-8286 */
802  break;
803 
804  case 's': /* sidereal-time */
806  if ( LALoptarg )
807  {
808  if ( ! strcmp( LALoptarg, "rad" ) || ! strcmp( LALoptarg, "RAD" ) )
810  else if ( ! strcmp( LALoptarg, "hms" ) || ! strcmp( LALoptarg, "HMS" ) )
812  else
813  {
814  fprintf( stderr, "unrecognized sidereal time format %s\n", LALoptarg );
815  fprintf( stderr, "expect \"HMS\" (hours:minutes:seconds)" );
816  fprintf( stderr, "or \"RAD\" (radians)\n" );
817  exit( 1 );
818  }
819  }
820  break;
821 
822  case 'S': /* stdin */
823  fp = stdin;
824  break;
825 
826  case 't': /* timer */
828  break;
829 
830  case 'u': /* utc */
831  utc_flag = 1;
832  break;
833 
834  case 'U': /* unix-epoch */
835  epoch = UNIX_EPOCH;
836  break;
837 
838  case 'v': /* verbose */
839  verbose = 1;
840  break;
841 
842  case 'V': /* version */
843  XLALOutputVCSInfo(stderr, lalVCSInfoList, 0, "%% ");
844  exit( 0 );
845  break;
846 
847  case 'z': /* zone */
848  tz = LALoptarg;
849  break;
850 
851  case 'Z': /* site */
852  if ( strcmp( LALoptarg, "LHO" ) == 0 || strcmp( LALoptarg, "lho" ) == 0 )
853  site = SITE_LHO;
854  else if ( strcmp( LALoptarg, "LLO" ) == 0 || strcmp( LALoptarg, "llo" ) == 0 )
855  site = SITE_LLO;
856  else
857  {
858  fprintf( stderr, "unrecognized detector site %s\n", LALoptarg );
859  fprintf( stderr, "expect either \"LHO\" or \"LLO\"\n" );
860  exit( 1 );
861  }
862  break;
863 
864  case '?':
865  /* fall-through */
866  default:
867  fprintf( stderr, "unknown error while parsing options\n" );
868  usage( program, 1 );
869  }
870  }
871 
872  if ( LALoptind == argc && fp )
873  {
874  char UNUSED *c;
875  c = fgets( buf, buflen, fp );
876  }
877  else
878  {
879  for ( ; LALoptind < argc; ++LALoptind )
880  {
881  int len;
882  len = strlen( buf );
883  strncat( buf + len, argv[LALoptind], buflen - len );
884  len = strlen( buf );
885  if ( len == buflen )
886  {
887  fprintf( stderr, "error: line too long\n" );
888  exit( 1 );
889  }
890  strncat( buf + len, " ", buflen - len );
891  if ( len == buflen )
892  {
893  fprintf( stderr, "error: line too long\n" );
894  exit( 1 );
895  }
896  }
897  }
898 
899  return buf;
900 }
const char * program
const LALVCSInfoList lalVCSInfoList
NULL-terminated list of VCS and build information for LAL and its dependencies
int LALgetopt_long_only(int argc, char *const *argv, const char *options, const struct LALoption *long_options, int *opt_index)
Definition: LALgetopt.c:190
int LALoptind
Definition: LALgetopt.c:79
char * LALoptarg
Definition: LALgetopt.c:64
#define no_argument
Definition: LALgetopt.h:100
#define required_argument
Definition: LALgetopt.h:101
#define optional_argument
Definition: LALgetopt.h:102
#define fprintf
const char *const name
type name
Definition: UserInput.c:193
static const struct leaps_table leaps[]
time_t get_date(const char *p, const time_t *now)
Definition: getdate.c:2721
#define LAL_PI
Archimedes's constant, pi.
Definition: LALConstants.h:179
int XLALOutputVCSInfo(FILE *fp, const LALVCSInfoList vcs_list, const int verbose, const char *prefix)
Output VCS and build information, as generated by XLALVCSInfoString(), to a file pointer fp.
REAL8 XLALConvertCivilTimeToJD(const struct tm *civil)
Returns the Julian Day (JD) corresponding to the civil date and time given in a broken down time stru...
INT4 XLALUTCToGPS(const struct tm *utc)
Returns the GPS seconds since the GPS epoch for a specified UTC time structure.
int XLALLeapSeconds(INT4 gpssec)
Returns the leap seconds TAI-UTC at a given GPS second.
time_t XLALSecondsSinceUnixEpoch(const struct tm *utc)
Compute Unix epoch time: seconds since 1970 January 1 0h UTC (POSIX:2001 definition of Unix Epoch).
REAL8 XLALConvertCivilTimeToMJD(const struct tm *civil)
Returns the Modified Julian Day MJD corresponding to the civil date and time given in a broken down t...
struct tm * XLALGPSToUTC(struct tm *utc, INT4 gpssec)
Returns a pointer to a tm structure representing the time specified in seconds since the GPS epoch.
void XLALExitErrorHandler(const char *func, const char *file, int line, int errnum)
The XLAL error handler that calls exit.
Definition: XLALError.c:607
XLALErrorHandlerType * XLALSetErrorHandler(XLALErrorHandlerType *newHandler)
Sets the error handler to a new handler and returns the old handler.
Definition: XLALError.c:372
REAL8 XLALGreenwichMeanSiderealTime(const LIGOTimeGPS *gpstime)
Convenience wrapper, calling XLALGreenwichSiderealTime() with the equation of equinoxes set to 0.
int * flag
Definition: LALgetopt.h:90
Epoch relative to GPS epoch, see LIGOTimeGPS type for more details.
Definition: LALDatatypes.h:458
INT4 gpsSeconds
Seconds since 0h UTC 6 Jan 1980.
Definition: LALDatatypes.h:459
INT4 gpsNanoSeconds
Residual nanoseconds.
Definition: LALDatatypes.h:460
void usage(const char *program, int exitcode)
Definition: tconvert.c:620
int verbose
Definition: tconvert.c:103
int main(int argc, char *argv[])
Definition: tconvert.c:107
void reset_zone(void)
Definition: tconvert.c:601
static void output_leaps(int gps_sec)
Definition: tconvert.c:339
enum @4 site
int unix_to_gps(time_t unix_sec)
Definition: tconvert.c:523
static void output_jd(int gps_sec)
Definition: tconvert.c:315
static void output_secs(int gps_sec)
Definition: tconvert.c:193
static void output_date_site(int gps_sec)
Definition: tconvert.c:295
static void output_date_utc(int gps_sec)
Definition: tconvert.c:255
void set_zone(const char *zone)
Definition: tconvert.c:540
static void output_date(int gps_sec)
Definition: tconvert.c:225
static void output_lho_date(int gps_sec)
Definition: tconvert.c:277
enum @3 output_type
static int is_blank(const char *s)
Definition: tconvert.c:73
void set_zone_lho(void)
Definition: tconvert.c:607
#define LHO_LONGITUDE_RAD_E
Definition: tconvert.c:50
#define MAX_ENV_SIZE
Definition: tconvert.c:539
static void output_local_sidereal_time_llo(int gps_sec)
Definition: tconvert.c:407
static void output_gmst(int gps_sec)
Definition: tconvert.c:381
const char * utc_unix_date_format
Definition: tconvert.c:93
static void output_sidereal_time(double sidereal_time_rad)
Definition: tconvert.c:349
static void output_mjd(int gps_sec)
Definition: tconvert.c:327
int utc_flag
Definition: tconvert.c:101
static void output_sidereal_time_site(int gps_sec)
Definition: tconvert.c:420
void set_zone_llo(void)
Definition: tconvert.c:613
const char * unix_date_format
Definition: tconvert.c:92
@ SIDEREAL_RAD
Definition: tconvert.c:98
@ SIDEREAL_HMS
Definition: tconvert.c:98
FILE * fp
Definition: tconvert.c:105
static void output_llo_date(int gps_sec)
Definition: tconvert.c:286
static const char * skip_space(const char *s)
Definition: tconvert.c:53
time_t gps_to_unix(int gps_sec, int *leap)
Definition: tconvert.c:501
@ OUTPUT_LEAPS
Definition: tconvert.c:99
@ OUTPUT_MJD
Definition: tconvert.c:99
@ OUTPUT_DATE
Definition: tconvert.c:99
@ OUTPUT_SECS
Definition: tconvert.c:99
@ OUTPUT_JD
Definition: tconvert.c:99
@ OUTPUT_GMST
Definition: tconvert.c:99
@ OUTPUT_DEFAULT
Definition: tconvert.c:99
const char * iso_8601_format
Definition: tconvert.c:90
static void output_local_sidereal_time_lho(int gps_sec)
Definition: tconvert.c:394
char * parse_options(char *buf, int buflen, int argc, char **argv)
Definition: tconvert.c:712
static int is_integer(const char *s)
Definition: tconvert.c:60
enum @2 sidereal_format
static void output_date_local(int gps_sec)
Definition: tconvert.c:266
const char * tz
Definition: tconvert.c:95
enum @1 epoch
@ SITE_LLO
Definition: tconvert.c:100
@ SITE_UNSPECIFIED
Definition: tconvert.c:100
@ SITE_LHO
Definition: tconvert.c:100
#define LLO_LONGITUDE_RAD_E
Definition: tconvert.c:51
@ GPS_EPOCH
Definition: tconvert.c:97
@ UNIX_EPOCH
Definition: tconvert.c:97
void output(int gps_sec, int output_type)
Definition: tconvert.c:440
const char * output_date_format
Definition: tconvert.c:94
const char * rfc_822_format
Definition: tconvert.c:91