MJD.h
1 //-*-C++-*-
2 /***************************************************************************
3  *
4  * Copyright (C) 1998 by Matthew Britton
5  * Licensed under the Academic Free License version 2.1
6  *
7  ***************************************************************************/
8 
9 // psrchive/Util/genutil/MJD.h
10 
11 #ifndef __GENUTIL_MJD_H
12 #define __GENUTIL_MJD_H
13 
14 #include <inttypes.h>
15 #include "utc.h"
16 
17 #include <iostream>
18 #include <limits>
19 #include <string>
20 #include <stdio.h>
21 
22 #ifdef HAVE_MPI
23 #include <mpi.h>
24 #endif
25 
27 class MJD {
28 
29  private:
30  int days;
31  int secs;
32  double fracsec;
33 
34  void add_day (double dd);
35  void settle();
36 
37  public:
38  static const MJD zero;
39 
40  static double precision;
41  static unsigned ostream_precision;
42 
43  static int verbose;
44 
45  // null constructor
46  MJD () { days=0;secs=0;fracsec=0.0; };
47 
48  // construct from standard C types
49  MJD (double dd); // mjd given as day
50  MJD (int intday, double fracday); // mjd given with separate fracday
51  MJD (double dd, double ss, double fs); // mjd with seconds and fracsec
52  MJD (int dd, int ss, double fs); // again
53 
54  // construct from a C string of the form "50312.4569"
55  MJD (const char* mjdstring);
56 
57  // some standard C time formats
58  MJD (time_t time); // returned by time()
59  MJD (const struct tm& greg); // returned by gmtime()
60  MJD (const struct timeval& tp); // returned by gettimeofday()
61 
62  // simple little struct invented in S2 days
63  MJD (const utc_t& utc);
64 
65  // construct from a C++ string
66  MJD (const std::string& mjd);
67 
68  // parses a string of the form 51298.45034 ish
69  int Construct (const char* mjdstr);
70 
71  // constructs an MJD from the unix time_t
72  int Construct (time_t time);
73 
74  // constructs an MJD from the unix struct tm
75  int Construct (const struct tm& greg);
76 
77  // another UNIX time standard
78  int Construct (const struct timeval& tp);
79 
80  // constructs an MJD from the home-grown utc_t
81  int Construct (const utc_t& utc);
82 
83  // constructs from a BAT (binary atomic time), ie MJD in microseconds
84  // stored in two 32 bit unsigned integers --- as returned by the AT clock
85  int Construct (uint64_t bat);
86 
87  double in_seconds() const;
88  double in_days() const;
89  double in_minutes() const;
90 
91  // cast into other forms
92  int UTC (utc_t* utc, double* fsec=NULL) const;
93  int gregorian (struct tm* gregdate, double* fsec=NULL) const;
94 
95  MJD & operator = (const MJD &);
96  MJD & operator += (const MJD &);
97  MJD & operator -= (const MJD &);
98  MJD & operator += (const double &);
99  MJD & operator -= (const double &);
100  MJD & operator *= (const double &);
101  MJD & operator /= (const double &);
102  friend const MJD operator + (const MJD &, const MJD &);
103  friend const MJD operator - (const MJD &, const MJD &);
104  friend const MJD operator + (const MJD &, double); // Add seconds to an MJD
105  friend const MJD operator - (const MJD &, double); // Take seconds from MJD
106  friend const MJD operator * (const MJD &, double);
107  friend const MJD operator * (double a, const MJD& m)
108  { return m * a; }
109  friend const MJD operator / (const MJD &, double);
110 
111  // return the -ve of m
112  friend const MJD operator - (MJD m);
113 
114  friend int operator > (const MJD &, const MJD &);
115  friend int operator < (const MJD &, const MJD &);
116  friend int operator >= (const MJD &, const MJD &);
117  friend int operator <= (const MJD &, const MJD &);
118  friend int operator == (const MJD &, const MJD &);
119  friend int operator != (const MJD &, const MJD &);
120 
121  // function to return plotable value to xyplot template class
122  float plotval() const { return float (in_days()); };
123 
124  // These bits are useful for tempo
125  int intday() const; // To access the integer day
126  double fracday() const; // To access fractional day
127  int get_secs() const {return(secs);};
128  double get_fracsec() const {return(fracsec);};
129 
130  // return LST in hours (longitude given in degrees East of Greenwich)
131  double LST (double longitude) const;
132 
133  // return Greenwich Mean Sidereal Time
134  double GMST () const;
135 
136  std::string printdays (unsigned precision) const;
137 
138  int print (FILE *stream);
139  int println (FILE *stream);
140  std::string printall() const;
141  std::string printdays() const;
142  std::string printhhmmss() const;
143  std::string printfs() const;
144  std::string strtempo() const;
145 
146  // returns a string formatted by strftime
147  // e.g. format = "%Y-%m-%d %H:%M:%S"
148  char* datestr (char* dstr, int len, const char* format) const;
149 
150  // more convenient interface, with optional fractional seconds
151  std::string datestr (const char* format, unsigned fractional_second_digits = 0) const;
152 
153 #ifdef HAVE_MPI
154  friend int mpiPack_size (const MJD&, MPI_Comm comm, int* size);
155  friend int mpiPack (const MJD&, void* outbuf, int outcount,
156  int* position, MPI_Comm comm);
157  friend int mpiUnpack (void* inbuf, int insize, int* position,
158  MJD*, MPI_Comm comm);
159 #endif
160 
161  protected:
162  friend bool equal (const MJD &m1, const MJD &m2);
163 
164 };
165 
166 inline double cast_double(const MJD&m) {return m.in_days();}
167 
168 std::ostream& operator<< (std::ostream& ostr, const MJD& mjd);
169 std::istream& operator>> (std::istream& istr, MJD& mjd);
170 
171 // Enable use of the MJD class with std C++ numeric_limits traits
172 namespace std {
173  template<>
174  class numeric_limits<MJD> {
175  public:
176  static const int digits10 = 15;
177  };
178 }
179 
180 #endif /* not __MJD_H defined */
181 
A convenient exception handling class.
Definition: Error.h:54
Class for representing modified Julian dates with high precision.
Definition: MJD.h:27

Generated using doxygen 1.8.17