2 * Functions related to the different time conversions in rdmc
9 #define JD2000 (2451545.0) /* Julian day for Jan 1st 2000 12:00 noon */
10 /* = standard epoch to be used as reference */
11 #define JCENTURY (36525.0) /* number od days per Julian century */
13 /************************************************************************/
14 /* rdmc_o_dateconv() converts a YYMMDD-date into the unix time */
15 /* (sec since 1.1.70) */
16 /************************************************************************/
18 time_t rdmc_o_dateconv(int date)
22 if (date <= 0) return RDMC_NA;
24 d.tm_sec = 0; /* second (0-61, allows for leap seconds) */
25 d.tm_min = 0; /* minute (0-59) */
26 d.tm_hour = 12; /* hour (0-23) */
27 d.tm_mday = date % 100; /* day of the month (1-31) */
28 d.tm_mon = (date/100) % 100 - 1; /* month (0-11) */
29 d.tm_year = date/10000; /* years since 1900 */
30 d.tm_isdst = 0; /* non-0 if daylight savings time is in effect */
32 return mktime(&d); /* time in sec since 1.1.70 */
34 } /* function o_dateconv() */
36 /************************************************************************/
37 /* rdmc_o_rdateconv() converts the unix time into the YYMMDD format */
38 /************************************************************************/
39 int rdmc_o_rdateconv(time_t time)
43 if (time <= 0) return RDMC_NA;
46 return (date->tm_year * 10000 + (date->tm_mon + 1) * 100 + date->tm_mday);
47 } /* function o_rdateconv() */
49 /************************************************************************/
50 /* rdmc_gps_to_mjd() converts the time in GPS/UT year and day into */
51 /* Modified Julian Day (50084 < mjd < 51545 for 1996-99) */
52 /* NOTES: jdyr = (Julian date of 1200 UT 1/1/year) = GPS/UT day one */
53 /************************************************************************/
54 int rdmc_gps_to_mjd(int gpsyear, int gpsday)
56 /* reference: http://www.starlink.rl.ac.uk/ function cldj.f
57 * or Zombeck p107 equation for julian date */
62 if ((gpsyear <= 0) || (gpsday <=0 ) )
65 mjdyr = (1461*(gpsyear+4799))/4 - (3*((gpsyear+4899)/100))/4 - 2431738.5;
66 mjd = (mjdyr + gpsday) - 1;
71 /************************************************************************/
72 /* rdmc_mjd_to_gps() converts the time in Modified Julian Days to */
73 /* GPS/UT year and day within year */
74 /* This uses an approximation which may err high in late December. */
75 /* It then checks using the exact calculation of mjdyr and gets gpsday. */
76 /* NOTES: jdyr = (Julian date of 1200 UT 1/1/year) = GPS/UT day one */
77 /************************************************************************/
78 void rdmc_mjd_to_gps(int mjd, int *gpsyear, int *gpsday)
88 year = (4*(mjd + 678960)) / 1461; /* Approx, erring high */
89 mjdyr = (1461*(year+4799))/4 - (3*((year+4899)/100))/4 - 2431738.5;
92 mjdyr = (1461*(year+4799))/4 - (3*((year+4899)/100))/4 - 2431738.5;
95 *gpsday = mjd - mjdyr + 1;
99 /************************************************************************/
100 /* rdmc_mjd_to_tjd() converts the time in Modified Julian Days to */
101 /* Truncated Julian Days. */
102 /* this is a trivial conversion but useful for GRB */
103 /* analysis with the batse catalog */
104 /************************************************************************/
105 void rdmc_mjd_to_tjd(int mjd, int secs, int ns, int *tjd, double *sec)
108 *sec=(double)secs+(double)ns/1e9;
111 /************************************************************************/
112 /* rdmc_tjd_to_mjd() converts the time in Truncated Julian Days to */
113 /* Modified Julian Days. */
114 /* this is a trivial conversion but useful for GRB */
115 /* analysis with the batse catalog */
116 /************************************************************************/
117 void rdmc_tjd_to_mjd(int tjd, double sec, int *mjd, int *secs, int *ns)
121 *ns = (sec - *secs) * 1e9;
124 /************************************************************************/
125 /* rdmc_mjd_to_jd() converts the time in Modified Julian Days to */
127 /* this is a trivial conversion but stands here for */
129 /************************************************************************/
130 void rdmc_mjd_to_jd(int mjd, int secs, int ns, double *jd)
132 *jd=2400000.5+mjd+((secs+ns/1e9)/86400);
135 /************************************************************************/
136 /* rdmc_jd_to_mjd() converts the time in Julian Days to */
137 /* Modified Julian Days. */
138 /* this is a trivial conversion but stands here for */
140 /************************************************************************/
141 void rdmc_jd_to_mjd(double jd, int *mjd, int *secs, int *ns)
144 *secs=(jd-(double)*mjd)*86400;
145 *ns=((jd-(double)*mjd)*86400-(double)*secs)*1e9;
147 /************************************************************************/
148 /* rdmc_mjd_to_unixtime() converts the time (unix in sec since 1.1.70) */
149 /* from mjd and secs */
150 /************************************************************************/
151 time_t rdmc_mjd_to_unixtime(int mjd, int secs)
155 if ((mjd <= 0) || (secs <= 0)) return RDMC_NA; /* if no mjd->no unix time*/
156 unix_time = (mjd - 40587)* 86400 + secs; /* 40587 is the 1.1.1970 */
158 } /* mjd_to_unixtime() */
160 /************************************************************************/
161 /* rdmc_unixtime_to_mjd() converts UNIX time(sec since 1.1.70) into mjd */
162 /************************************************************************/
163 int rdmc_unixtime_to_mjd(time_t unix_time)
165 if (unix_time <= 0) return RDMC_NA;
166 return unix_time/86400 + 40587;
167 } /* unixtime_to_mjd() */
170 /************************************************************************/
171 /* rdmc_unixtime_to_mjdsecs() converts the time (unic in sec since */
172 /* 1.1.70) into secs after mjd */
173 /************************************************************************/
174 int rdmc_unixtime_to_mjdsecs(time_t unix_time)
176 if (unix_time <= 0) return RDMC_NA;
177 return unix_time % 86400;
178 } /* unixtime_to_mjdsecs() */
181 /************************************************************************/
182 /* rdmc_jd_to_gmst() converts mjd date and time to Greenwich mean */
184 /************************************************************************/
185 void rdmc_jd_to_gmst(double jd, double *gmst)
189 * GMST from derived NOVAS package sidereal_time() function.
190 * NOVAS is online at http://aa.usno.navy.mil/aa/
196 tdt = (jd - JD2000) / JCENTURY;
197 gst = ((((-6.2e-6 * tdt + 0.093104) * tdt + 184.812866) * tdt
198 + 67310.54841) + 3.1644e9 * tdt);
199 igst= (int)(gst/86400.0);
200 *gmst = gst/3600.0 - (double)(igst*24.0); /* gmst is smaller than 24 */
201 if (*gmst < 0.0) *gmst += 24.0; /* gmst is larger than 0 */
203 } /* rdmc_jd_to_gmst() */
207 /************************************************************************/
208 /* rdmc_gmst_to_jd() converts Greenwich mean sidereal time to mjd */
210 /************************************************************************/
211 void rdmc_gmst_to_jd(double gmst, int intjd, double *jd)
214 * GMST in hours, intjd is integer part of jd
215 * GMST from derived NOVAS package sidereal_time() function.
216 * NOVAS is online at http://aa.usno.navy.mil/aa/
217 * Inverted direction according to functions GMST->UT and UT->GMST
218 * From "practical Astronomy with your Calculator", Cambridge UP 1988
222 /* Caution: Function untested in any respect */
223 double tdt, gst, fracjd;
225 tdt = ((double)intjd - JD2000) / JCENTURY;
226 gst = ((((-6.2e-6 * tdt + 0.093104) * tdt + 184.812866) * tdt
227 + 67310.54841) + 3.1644e9 * tdt);
229 fracjd = fmod ((fracjd / 3600.0), 24.0);
230 if (fracjd < 0.0) fracjd += 24.0;
231 fracjd=fracjd/24.0*0.9972695663;
232 *jd=fracjd+(double)intjd;
234 } /* rdmc_jd_to_gmst() */