home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / astrnomy / ephem421.zip / TIME.C < prev    next >
C/C++ Source or Header  |  1990-09-13  |  3KB  |  127 lines

  1. /* get the time from the os.
  2.  *
  3.  * here are two methods I was able to verify; pick one for your system and
  4.  *   define exactly one of TZA or TZB:
  5.  * TZA works on our ibm-pc/turbo-c and at&t systems,
  6.  * TZB works on our 4.2 BSD vax.
  7.  *
  8.  * I'm told that on Sun OS 4.0.3 (BSD 4.3?) and Apollo SR 10.1 TZB works if
  9.  *   you use <sys/time.h> in place of <time.h>.
  10.  * 
  11.  * On VMS, you DON'T want to define EITHER TZA nor TZB since it can't handle
  12.  *   time zones, period. time_fromsys() will detect that fact based on gmtime()
  13.  *   returning 0.
  14.  */
  15.  
  16. #define    TZA
  17.  
  18. #ifdef VMS
  19. #undef TZA
  20. #undef TZB
  21. #endif
  22.  
  23. #include <stdio.h>
  24. #include <time.h>
  25.  
  26. #include "astro.h"
  27. #include "circum.h"
  28.  
  29. extern char *strncpy();
  30. extern long time();
  31.  
  32. static long c0;
  33. static double mjd0;
  34.  
  35. /* save current mjd and corresponding system clock for use by inc_mjd().
  36.  * this establishes the base correspondence between the mjd and system clock.
  37.  */
  38. set_t0 (np)
  39. Now *np;
  40. {
  41.     mjd0 = mjd;
  42.     (void) time (&c0);
  43. }
  44.  
  45. /* fill in n_mjd/tz/tznm from system clock.
  46.  */
  47. time_fromsys (np)
  48. Now *np;
  49. {
  50.     extern struct tm *gmtime(), *localtime();
  51.     struct tm *tp;
  52.     long c;
  53.     double day, hr;
  54.  
  55.     (void) time (&c);
  56.  
  57.     tp = gmtime (&c);
  58.     if (tp) {
  59.         cal_mjd (tp->tm_mon+1, (double)tp->tm_mday, tp->tm_year+1900, &day);
  60.         sex_dec (tp->tm_hour, tp->tm_min, tp->tm_sec, &hr);
  61.         mjd = day + hr/24.0;
  62.         tp = localtime (&c);
  63.         settzstuff (tp->tm_isdst ? 1 : 0, np);
  64.     } else {
  65.         /* if gmtime() doesn't work, we assume the timezone stuff won't
  66.          * either, so we just use what it is and leave it alone. Some
  67.          * systems (like VMS) do not know about time zones, so this is the
  68.          * best guess in that case.
  69.          */
  70.         tp = localtime (&c);
  71.         cal_mjd (tp->tm_mon+1, (double)tp->tm_mday, tp->tm_year+1900, &day);
  72.         sex_dec (tp->tm_hour, tp->tm_min, tp->tm_sec, &hr);
  73.         mjd = day + hr/24.0 + tz/24.0;
  74.     }
  75. }
  76.  
  77. /* given whether dst is now in effect (must be strictly 0 or 1), fill in
  78.  * tzname and tz within np.
  79.  */
  80. static
  81. settzstuff (dst, np)
  82. int dst;
  83. Now *np;
  84. {
  85. #ifdef TZA
  86.     extern long timezone;
  87.     extern char *tzname[2];
  88.  
  89.     tzset();
  90.     tz = timezone/3600;
  91.     if (dst)
  92.         tz -= 1.0;
  93.     (void) strncpy (tznm, tzname[dst], sizeof(tznm)-1);
  94. #endif
  95. #ifdef TZB
  96.     extern char *timezone();
  97.     struct timeval timev;
  98.     struct timezone timez;
  99.  
  100.     gettimeofday (&timev, &timez);
  101.     tz = timez.tz_minuteswest/60;
  102.     if (dst)
  103.         tz -= 1.0;
  104.     (void) strncpy (tznm, timezone(timez.tz_minuteswest, dst),
  105.                                 sizeof(tznm)-1);
  106. #endif
  107.     tznm[sizeof(tznm)-1] = '\0';    /* insure string is terminated */
  108. }
  109.  
  110. inc_mjd (np, inc)
  111. Now *np;
  112. double inc;
  113. {
  114.     if (inc == RTC) {
  115.         long c;
  116.         (void) time (&c);
  117.         mjd = mjd0 + (c - c0)/SPD;
  118.     } else
  119.         mjd += inc/24.0;
  120.  
  121.     /* round to nearest whole second.
  122.      * without this, you can get fractional days so close to .5 but
  123.      * not quite there that mjd_hr() can return 24.0
  124.      */
  125.     rnd_second (&mjd);
  126. }
  127.