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
23class MJD {
24
25 private:
26 int days;
27 int secs;
28 double fracsec;
29
30 void add_day (double dd);
31 void settle();
32
33 public:
34 static const MJD zero;
35
36 static double precision;
37 static unsigned ostream_precision;
38
39 static int verbose;
40
42 MJD () { days=0;secs=0;fracsec=0.0; };
43
45 MJD (double dd); // mjd given as day
46 MJD (int intday, double fracday); // mjd given with separate fracday
47 MJD (double dd, double ss, double fs); // mjd with seconds and fracsec
48 MJD (int dd, int ss, double fs); // again
49
51 MJD (const char* mjdstring);
52
54 MJD (time_t time); // returned by time()
55 MJD (const struct tm& greg); // returned by gmtime()
56 MJD (const struct timeval& tp); // returned by gettimeofday()
57
59 MJD (const utc_t& utc);
60
62 MJD (const std::string& mjd);
63
65 int Construct (const char* mjdstr);
66
68 int Construct (time_t time);
69
71 int Construct (const struct tm& greg);
72
74 int Construct (const struct timeval& tp);
75
77 int Construct (const utc_t& utc);
78
80
81 int Construct (uint64_t bat);
82
83 double in_seconds() const;
84 double in_days() const;
85 double in_minutes() const;
86
87 // cast into other forms
88 int UTC (utc_t* utc, double* fsec=NULL) const;
89 int gregorian (struct tm* gregdate, double* fsec=NULL) const;
90
91 MJD & operator = (const MJD &);
92 MJD & operator += (const MJD &);
93 MJD & operator -= (const MJD &);
94 MJD & operator += (const double &);
95 MJD & operator -= (const double &);
96 MJD & operator *= (const double &);
97 MJD & operator /= (const double &);
98 friend const MJD operator + (const MJD &, const MJD &);
99 friend const MJD operator - (const MJD &, const MJD &);
100 friend const MJD operator + (const MJD &, double); // Add seconds to an MJD
101 friend const MJD operator - (const MJD &, double); // Take seconds from MJD
102 friend const MJD operator * (const MJD &, double);
103 friend const MJD operator * (double a, const MJD& m) { return m * a; }
104 friend const MJD operator / (const MJD &, double);
105
106 // return the -ve of m
107 friend const MJD operator - (MJD m);
108
109 friend int operator > (const MJD &, const MJD &);
110 friend int operator < (const MJD &, const MJD &);
111 friend int operator >= (const MJD &, const MJD &);
112 friend int operator <= (const MJD &, const MJD &);
113 friend int operator == (const MJD &, const MJD &);
114 friend int operator != (const MJD &, const MJD &);
115
117 float plotval() const { return float (in_days()); };
118
119 // These bits are useful for tempo
120 int intday() const; // To access the integer day
121 double fracday() const; // To access fractional day
122 int get_secs() const {return(secs);};
123 double get_fracsec() const {return(fracsec);};
124
126
127 double LST (double longitude) const;
128
130 double GMST () const;
131
132 std::string printdays (unsigned precision) const;
133
134 int print (FILE *stream);
135 int println (FILE *stream);
136 std::string printall() const;
137 std::string printdays() const;
138 std::string printhhmmss() const;
139 std::string printfs() const;
140 std::string strtempo() const;
141
143
144 char* datestr (char* dstr, int len, const char* format) const;
145
147 std::string datestr (const char* format, unsigned fractional_second_digits = 0) const;
148
150 void from_datestr (const std::string& datestr, const std::string& format = "%Y-%m-%d-%H:%M:%S");
151
152 protected:
153 friend bool equal (const MJD &m1, const MJD &m2);
154
155};
156
157inline double cast_double(const MJD&m) {return m.in_days();}
158
159std::ostream& operator<< (std::ostream& ostr, const MJD& mjd);
160std::istream& operator>> (std::istream& istr, MJD& mjd);
161
162// Enable use of the MJD class with std C++ numeric_limits traits
163namespace std {
164 template<>
165 class numeric_limits<MJD> {
166 public:
167 static const int digits10 = 15;
168 };
169}
170
171#endif /* not __MJD_H defined */
172
Class for representing modified Julian dates with high precision.
Definition MJD.h:23
void from_datestr(const std::string &datestr, const std::string &format="%Y-%m-%d-%H:%M:%S")
Parse MJD from the given datestr using strptime and the specified format.
Definition MJD.C:672
double GMST() const
return Greenwich Mean Sidereal Time in radians
Definition sla_MJD.C:16
int Construct(const char *mjdstr)
construct from a string of the form 51298.45034 ish
Definition MJD.C:653
MJD()
null constructor
Definition MJD.h:42
double LST(double longitude) const
return Local Sidereal Time in hours
Definition sla_MJD.C:27
float plotval() const
return plotable value to xyplot template class
Definition MJD.h:117
char * datestr(char *dstr, int len, const char *format) const
return a string formatted by strftime
Definition MJD.C:152
STL namespace.

Generated using doxygen 1.14.0