1 | // -*- C++ -*-
|
---|
2 | //
|
---|
3 | // $Id: m_date.h,v 1.1.1.1 2006/05/30 11:05:27 mervart Exp $
|
---|
4 | //
|
---|
5 |
|
---|
6 | #if !defined(__m_date_h__)
|
---|
7 | // protects the header from been included more than once
|
---|
8 | #define __m_date_h_
|
---|
9 | #if defined(__GNUC__)
|
---|
10 | using namespace std;
|
---|
11 | #endif
|
---|
12 | //
|
---|
13 | // Date & StructDate
|
---|
14 | // Wrapper around c-lib and system service for date and time.
|
---|
15 | // class Date is a wrapper around time_t.
|
---|
16 | // class StructDate is a wrapper around struct tm.
|
---|
17 | //
|
---|
18 | #include "m_data.h"
|
---|
19 |
|
---|
20 | class Date;
|
---|
21 |
|
---|
22 | typedef time_t DateType;
|
---|
23 |
|
---|
24 | // Date - Date -> DateDiff ...
|
---|
25 | class DateDiff : public C_Data<DateType> {
|
---|
26 | public:
|
---|
27 | friend class Date;
|
---|
28 | explicit DateDiff(DateType t= 0):C_Data<DateType>(t) {} ;
|
---|
29 | DateDiff operator+(const DateDiff &arg2) { return DateDiff(get()+arg2.get()); }
|
---|
30 | DateDiff operator*(int factor) { return DateDiff(get()*factor); }
|
---|
31 | };
|
---|
32 |
|
---|
33 | class Date : public C_Data<DateType> {
|
---|
34 | public:
|
---|
35 |
|
---|
36 | typedef C_Data<DateType> BaseType;
|
---|
37 | typedef DateDiff DiffType;
|
---|
38 |
|
---|
39 | friend class StructDate;
|
---|
40 |
|
---|
41 | Date():C_Data<DateType>(0) {}
|
---|
42 |
|
---|
43 | Date(int day,int month,int year);
|
---|
44 | Date(int day,int month,int year,int h,int m,int s);
|
---|
45 |
|
---|
46 |
|
---|
47 | enum {
|
---|
48 | GPS_START = 315964800, // 6.1.1980 00:00 UTC
|
---|
49 | SECONDS_PER_WEEK = 60 * 60 * 24 * 7
|
---|
50 | } ;
|
---|
51 |
|
---|
52 | // convient conversion to some stream format
|
---|
53 | friend ostream &operator<<(ostream &os,const Date &d) { return d.print(os);}
|
---|
54 |
|
---|
55 | // convient conversion from yymmdd
|
---|
56 | friend istream &operator>>(istream &is,Date &d) {
|
---|
57 | #if 1
|
---|
58 | d.read_some(is);
|
---|
59 | #else
|
---|
60 | d.get_i(is);
|
---|
61 | #endif
|
---|
62 | return is;
|
---|
63 | }
|
---|
64 |
|
---|
65 | void tag_set(long tag) {
|
---|
66 | set(tag + GPS_START);
|
---|
67 | }
|
---|
68 |
|
---|
69 | long tag_get() const {
|
---|
70 | return get() - GPS_START;
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | void gps_set(short gpsWeek,long gpsSeconds) {
|
---|
75 | // GPS-Zeit ist linear wie UNIX-Zeit, faengt am an.
|
---|
76 | tag_set(gpsWeek * SECONDS_PER_WEEK + gpsSeconds);
|
---|
77 | }
|
---|
78 |
|
---|
79 | void gps_get(short &gpsWeek,long &gpsSeconds) const {
|
---|
80 | #if 1
|
---|
81 | gpsWeek = tag_get() / SECONDS_PER_WEEK;
|
---|
82 | gpsSeconds = tag_get() % SECONDS_PER_WEEK;
|
---|
83 | #else
|
---|
84 | divmod(tag_get(),(long)SECONDS_PER_WEEK,gpsWeek,gpsSeconds);
|
---|
85 | #endif
|
---|
86 | }
|
---|
87 |
|
---|
88 | void doy_set(short doy);
|
---|
89 | short doy_get() const;
|
---|
90 |
|
---|
91 | istream &read_rinex(istream &is,double &fraction = rinex_default_secondsFraction);
|
---|
92 | istream &read_some(istream &is);
|
---|
93 | ostream &write_rinex(ostream &os);
|
---|
94 |
|
---|
95 | // Translate the Date into a string ...
|
---|
96 | // with some extentions for special needs ...
|
---|
97 | string form(int s= 64,bool trail0=true,const char *fmt="%Y %m %d %H %M %S") const;
|
---|
98 |
|
---|
99 | ostream &print(ostream &os,const char *fmt="%d%m%y") const ;
|
---|
100 |
|
---|
101 | // getDate reads a day in the form "ttmmyy"
|
---|
102 | // for the current system time, but with the day as in the stream.
|
---|
103 | // expects that the time is present in the stream.
|
---|
104 | //
|
---|
105 | // For a complete missing date the function returns false leaving the
|
---|
106 | // stream unchanged via putback().
|
---|
107 | // For a partial malformed date the character get lost.
|
---|
108 |
|
---|
109 | bool get_i(istream &is);
|
---|
110 |
|
---|
111 |
|
---|
112 | // returns the time of invocation: unix system time
|
---|
113 | static Date timeNow();
|
---|
114 |
|
---|
115 | DateDiff operator-(const Date &arg2) const {
|
---|
116 | const DateType diff(get() - arg2.get());
|
---|
117 | return DateDiff(diff);
|
---|
118 | }
|
---|
119 |
|
---|
120 | Date operator+(const DateDiff &arg2) const {
|
---|
121 | return Date(get() + arg2.get());
|
---|
122 | }
|
---|
123 |
|
---|
124 | private:
|
---|
125 | Date(DateType dt):C_Data<DateType>(dt) {}
|
---|
126 |
|
---|
127 | bool get_i2(istream &is,int &result);
|
---|
128 |
|
---|
129 | static DateType _timeNow();
|
---|
130 | static double rinex_default_secondsFraction;
|
---|
131 | } ;
|
---|
132 |
|
---|
133 | //
|
---|
134 | // StructDate
|
---|
135 | // wrapper around gmtime,mktime and strftime
|
---|
136 | //
|
---|
137 | class StructDate {
|
---|
138 | public:
|
---|
139 | typedef struct tm BaseType;
|
---|
140 |
|
---|
141 | StructDate();
|
---|
142 | // default copy-constructor
|
---|
143 | // default assignment
|
---|
144 | // default destructor
|
---|
145 |
|
---|
146 | StructDate(const Date &date) { set(date); }
|
---|
147 | StructDate(int day,int month,int year) { set(day,month,year); }
|
---|
148 | StructDate(int day,int month,int year,int h,int m,int s) { set(day,month,year,h,m,s); }
|
---|
149 |
|
---|
150 | Date date() const;
|
---|
151 |
|
---|
152 |
|
---|
153 | string form(const char *fmt) const;
|
---|
154 |
|
---|
155 | void set(int day,int month,int year,int h= 12,int m= 0,int s= 0);
|
---|
156 | void set(const Date &date);
|
---|
157 |
|
---|
158 | int sec() const {
|
---|
159 | return tms.tm_sec;
|
---|
160 | }
|
---|
161 |
|
---|
162 | int min() const {
|
---|
163 | return tms.tm_min;
|
---|
164 | }
|
---|
165 |
|
---|
166 | int hour() const {
|
---|
167 | return tms.tm_hour;
|
---|
168 | }
|
---|
169 |
|
---|
170 | int day() const {
|
---|
171 | return tms.tm_mday;
|
---|
172 | }
|
---|
173 |
|
---|
174 | int month() const {
|
---|
175 | return tms.tm_mon+1;
|
---|
176 | }
|
---|
177 | static const char *_monthNames[];
|
---|
178 | const char *monthStr();
|
---|
179 |
|
---|
180 | int year4() const {
|
---|
181 | return tms.tm_year + 1900;
|
---|
182 | }
|
---|
183 |
|
---|
184 | int year2() const {
|
---|
185 | return tms.tm_year%100;
|
---|
186 | }
|
---|
187 |
|
---|
188 | // returns the day of year 0-based.
|
---|
189 | int dayOfYear0() const {
|
---|
190 | return tms.tm_yday;
|
---|
191 | }
|
---|
192 |
|
---|
193 | // return the day of year 1-based.
|
---|
194 | int dayOfYear1() const {
|
---|
195 | return tms.tm_yday+1;
|
---|
196 | }
|
---|
197 |
|
---|
198 | BaseType tms;
|
---|
199 | } ;
|
---|
200 |
|
---|
201 | #endif
|
---|