ocssw  1.0
/data1/web/ocssw/build/src/libosmi/time-utils.c (r10485/r2592)
Go to the documentation of this file.
00001 /*
00002  *----------------------------------------------------------------------
00003  *  @(#) time-utils.c   1.0 05 Jun 98   <shc>
00004  *  Copyright (c) 1993, CSIRO Division of Oceanography.
00005  *  Copyright (c) 1998, Datron Transco Inc.
00006  *----------------------------------------------------------------------
00007  *
00008  * time-utils --
00009  *
00010  *  A collection of routines for converting between normal time/date
00011  *  formats and Julian dates.
00012  *
00013  * History:
00014  *  05 Jun 98  <shc>
00015  *     Split out from old julian.c
00016  *
00017  *----------------------------------------------------------------------
00018  */
00019 
00020 #include <math.h>
00021 #include <time.h>
00022 
00023 #include "orbit.h"  /* Prototype checking */
00024 
00025 
00026 /*
00027  *  This function converts the date and time in a tm struct to the
00028  *  modified Julian day format used internally in all the astronomical
00029  *  and orbit prediction code.
00030  */
00031 
00032 double
00033 tmtojul(time)
00034 struct tm *time;
00035 {
00036     int year, month, day;
00037     int32_t hour, minute, second;
00038     int32_t mjd;
00039     double fday;
00040     double result;
00041 
00042     day    = time->tm_mday;
00043     month  = time->tm_mon + 1;  /* tm_mon is in range 0-11 */
00044     year   = 1900 + time->tm_year;  /* tm_year is years since 1900 */
00045 
00046     second = time->tm_sec;
00047     minute = time->tm_min;
00048     hour   = time->tm_hour;
00049 
00050     mjd = julday(year, month, day) - 2400000;
00051 
00052     fday = ((hour*60 + minute)*60 + second) / 86400.0;
00053     
00054     result = mjd + fday - 0.5;
00055     return (result);
00056 }
00057 
00058 
00059 /*
00060  *  This function does the inverse of TOJUL.
00061  */
00062 
00063 void
00064 jultotm(tjd, time)
00065 double tjd;
00066 struct tm *time;
00067 {
00068     int32_t mjd;
00069     double secs;
00070     int year, month, day;
00071     int hour, minute, second;
00072 
00073     mjd = (int32_t)(tjd + 0.5);
00074     secs = (tjd + 0.5 - mjd) * 86400.0 + 0.0005;
00075     mjd += 2400000;
00076 
00077     caldat(mjd, &year, &month, &day);
00078 
00079     hour = (int)(secs / 3600.0);
00080     minute = (int)(secs / 60.0) % 60;
00081     second = (int)(secs) % 60;
00082 
00083     time->tm_sec   = second;
00084     time->tm_min   = minute;
00085     time->tm_hour  = hour;
00086     time->tm_mday  = day;
00087     time->tm_mon   = month;
00088     time->tm_year  = year;
00089     time->tm_isdst = 0;
00090 
00091     return;
00092 }
00093 
00094 
00095 /*
00096  *  This function converts a date and time in compact integer form to the
00097  *  modified Julian day format used internally in all the orbit prediction
00098  *  code.  Note that a year number of less than 50 is interpreted as being
00099  *  in the range 2000 - 2049.
00100  */
00101 
00102 double
00103 itojul(date, time)
00104 int32_t date;       /* yymmdd */
00105 int32_t time;       /* hhmmssfff */
00106 {
00107     int year, month, day, hour, minute, second, millisec;
00108     int32_t mjd;
00109     double fday, result;
00110 
00111     day    = date % 100L;
00112     month  = date/100L % 100L;
00113     year   = date/10000L % 100L;
00114     millisec   = time % 1000L;
00115     second = time/1000L % 100L;
00116     minute = time/100000L % 100L;
00117     hour   = time/10000000L % 100L;
00118 
00119     if (year >= 50)
00120         year = year + 1900;
00121     else
00122         year = year + 2000;
00123 
00124     mjd = julday(year, month, day) - 2400000L;
00125 
00126     fday = (((hour*60.0 + minute)*60.0+ second) + millisec/1000.0)/86400.0;
00127 
00128     result = mjd + fday - 0.5;
00129     return(result);
00130 }
00131 
00132 
00133 /*
00134  *  This function does the inverse of ITOJUL.
00135  */
00136 
00137 void
00138 jultoi(tjd, date, time)
00139 double tjd;
00140 int32_t *date, *time;
00141 {
00142     int year, month, day;
00143     int32_t mjd, hour, minute, second, millisec;
00144     double secs;
00145 
00146     mjd = (int32_t)(tjd + 0.5);
00147     secs = (tjd + 0.5 - mjd) * 86400.0 + 0.0005;
00148     caldat(mjd + 2400000L, &year, &month, &day);
00149 
00150     hour     = (int32_t)(secs / 3600.0);
00151     minute   = (int32_t)(secs/60.0) % 60L;
00152     second   = (int32_t)(secs) % 60L;
00153     millisec = (int32_t)(secs*1000.0) % 1000L;
00154 
00155     *date = ((int32_t)year % 100L)*10000L + (int32_t)month*100L + (int32_t)day;
00156     *time = hour*10000000L + minute*100000L + second*1000L + millisec;
00157 
00158     return;
00159 }