Skip to content

Commit

Permalink
Put the macros for compile-time computing of MJD for the start/end of…
Browse files Browse the repository at this point in the history
… a month into a new 'mjd_defs.h' file, and modified them slightly to work with 32-bit longs (not just 64-bit ones).
  • Loading branch information
Bill-Gray committed Oct 25, 2024
1 parent 9cce29e commit ac99be3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 45 deletions.
24 changes: 1 addition & 23 deletions delta_t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
#include <assert.h>
#include "watdefs.h"
#include "afuncs.h"
#include "mjd_defs.h"

#ifdef __WATCOMC__
#define sinl(x) ((long double)sin((double)x))
Expand Down Expand Up @@ -363,29 +364,6 @@ long double DLL_FUNC tdb_minus_tdt( const long double t_centuries)
return( rval); /* difference is in _seconds_ */
}

/* These macros determine the MJD of the given date in 'YEAR'. They
are valid for (Gregorian) years after roughly 20 billion years ago.
This was chosen to comfortably include all dates since the Big Bang.
We use the fact that February 1 is always 31 days after January 1,
but have to work backward from January 1 of the following year to
get March to December without having to consider leap days. */

#define BASE_YEAR 19999999999L
#define JAN_1( YEAR) (((YEAR) * 365L + ((YEAR) + BASE_YEAR) / 4L - ((YEAR) + BASE_YEAR) / 100L \
+ ((YEAR) + BASE_YEAR) / 400L) - 678940L \
- ((BASE_YEAR + 1L) / 400L) * 97L)
#define FEB_1( YEAR) (JAN_1( YEAR) + 31)
#define DEC_1( YEAR) (JAN_1( (YEAR)+1) - 31)
#define NOV_1( YEAR) (DEC_1( YEAR) - 30)
#define OCT_1( YEAR) (NOV_1( YEAR) - 31)
#define SEP_1( YEAR) (OCT_1( YEAR) - 30)
#define AUG_1( YEAR) (SEP_1( YEAR) - 31)
#define JUL_1( YEAR) (AUG_1( YEAR) - 31)
#define JUN_1( YEAR) (JUL_1( YEAR) - 30)
#define MAY_1( YEAR) (JUN_1( YEAR) - 31)
#define APR_1( YEAR) (MAY_1( YEAR) - 30)
#define MAR_1( YEAR) (APR_1( YEAR) - 31)

#define utc0 (JAN_1( 1972))
/* 'utc0' = MJD of date when the UTC leap seconds began */

Expand Down
40 changes: 40 additions & 0 deletions mjd_defs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef MJD_DEFS_H
#define MJD_DEFS_H

#include <stdint.h>
#include <limits.h>

/* These macros determine the MJD of the start of a month in the given
'YEAR' at compile time. If sizeof( long) == 8, they are valid for
(Gregorian) years after roughly 20 billion years ago. This was chosen
to comfortably include all dates since the Big Bang. If sizeof( long)
== 4, they only work between years -5877630 and +5881469; beyond
that, the result overflows the 32-bit long.
February 1 will always be 31 days after January 1. March 1 through
December 1 will always be a fixed number of days before January 1 of
the following year. */

#if LONG_MAX == INT64_MAX
#define BASE_YEAR 19999999999L
#elif LONG_MAX == INT32_MAX
#define BASE_YEAR 1999999999L
#else
#error "Long integers have an unrecognized size"
#endif
#define JAN_1( YEAR) (((YEAR) * 365L + ((YEAR) + BASE_YEAR) / 4L - ((YEAR) + BASE_YEAR) / 100L \
+ ((YEAR) + BASE_YEAR) / 400L) - 678940L \
- ((BASE_YEAR + 1L) / 400L) * 97L)
#define FEB_1( YEAR) (JAN_1( YEAR) + 31)
#define DEC_1( YEAR) (JAN_1( (YEAR)+1) - 31)
#define NOV_1( YEAR) (DEC_1( YEAR) - 30)
#define OCT_1( YEAR) (NOV_1( YEAR) - 31)
#define SEP_1( YEAR) (OCT_1( YEAR) - 30)
#define AUG_1( YEAR) (SEP_1( YEAR) - 31)
#define JUL_1( YEAR) (AUG_1( YEAR) - 31)
#define JUN_1( YEAR) (JUL_1( YEAR) - 30)
#define MAY_1( YEAR) (JUN_1( YEAR) - 31)
#define APR_1( YEAR) (MAY_1( YEAR) - 30)
#define MAR_1( YEAR) (APR_1( YEAR) - 31)

#endif /* #ifdef MJD_DEFS_H */
23 changes: 1 addition & 22 deletions utc_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,11 @@ along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA. */

/* These macros determine the MJD of the given date in 'YEAR'.
They are explained a bit in comments in 'delta_t.cpp' (q.v.).
We're only using the July and January ones (the months in which
leap seconds occur). Unused macros are commented out, just to
avoid nuisance compiler warnings. */

#define BASE_YEAR 19999999999L
#define JAN_1( YEAR) (((YEAR) * 365L + ((YEAR) + BASE_YEAR) / 4L - ((YEAR) + BASE_YEAR) / 100L \
+ ((YEAR) + BASE_YEAR) / 400L) - 678940L \
- ((BASE_YEAR + 1L) / 400L) * 97L)
// #define FEB_1( YEAR) (JAN_1( YEAR) + 31)
#define DEC_1( YEAR) (JAN_1( (YEAR)+1) - 31)
#define NOV_1( YEAR) (DEC_1( YEAR) - 30)
#define OCT_1( YEAR) (NOV_1( YEAR) - 31)
#define SEP_1( YEAR) (OCT_1( YEAR) - 30)
#define AUG_1( YEAR) (SEP_1( YEAR) - 31)
#define JUL_1( YEAR) (AUG_1( YEAR) - 31)
// #define JUN_1( YEAR) (JUL_1( YEAR) - 30)
// #define MAY_1( YEAR) (JUN_1( YEAR) - 31)
// #define APR_1( YEAR) (MAY_1( YEAR) - 30)
// #define MAR_1( YEAR) (APR_1( YEAR) - 31)

#include <stdio.h>
#include <stdlib.h>
#include "watdefs.h"
#include "afuncs.h"
#include "mjd_defs.h"

int main( const int argc, const char **argv)
{
Expand Down

0 comments on commit ac99be3

Please sign in to comment.