source: ntrip/trunk/BNC/RTCM/m_date.cpp@ 203

Last change on this file since 203 was 35, checked in by mervart, 19 years ago

Imported sources

File size: 4.7 KB
Line 
1// -*- C++ -*-
2//
3// $Id: m_date.cpp,v 1.1.1.1 2006/01/11 09:34:31 mervart Exp $
4//
5// Comment: c.f. m_date.h
6
7
8#include "m_date.h"
9
10// for memset
11#include <string.h>
12
13//
14// Initialize
15//
16StructDate::StructDate() {
17 memset(&tms,0,sizeof(tms));
18}
19
20//
21// construct from Date-Instance
22//
23void StructDate::set(const Date &date) {
24 // is not 100% thread save ...
25 tms = *gmtime(&date.data);
26}
27
28//
29// calculate a date-Instance
30//
31Date StructDate::date() const {
32 Date d;
33 // breaks constness: tms can be normalized by mktime
34#if !defined(__CYGWIN32__)
35 d.data = mktime((BaseType *)&tms) - timezone;
36#else
37 d.data = mktime((BaseType *)&tms);
38#endif
39 return d;
40}
41
42//
43// convert via well-known strftime timeformat string into a string
44//
45string StructDate::form(const char *fmt) const {
46 char buffer[1024];
47 strftime(buffer,sizeof(buffer),fmt,&tms);
48 return string(buffer);
49}
50
51//
52// Construct from day,month year ...
53//
54void StructDate::set(int day,int month,int year,int h,int m,int s) {
55 StructDate sd(Date::timeNow());
56 sd.tms.tm_sec= s;
57 sd.tms.tm_min= m;
58 sd.tms.tm_hour= h;
59 sd.tms.tm_mday = day;
60 sd.tms.tm_mon = month-1;
61 sd.tms.tm_year = ((year >= 1900) ? -1900 : 0) + year + ((year < 80) ? 100 : 0);
62 *this = sd;
63}
64
65
66const char *StructDate::_monthNames[]
67= { "jan","feb","mar","apr","may","jun"
68 ,"jul","aug","sep","oct","nov","dez" } ;
69
70const char *StructDate::monthStr() {
71 return _monthNames[tms.tm_mon];
72}
73
74// ------------------------------------------------------------------------------------
75
76Date::Date(int day,int month,int year) {
77 StructDate sd(day,month,year);
78 set(sd.date().get());
79}
80
81Date::Date(int day,int month,int year,int h,int m,int s) {
82 StructDate sd(day,month,year,h,m,s);
83 set(sd.date().get());
84}
85
86void Date::doy_set(short doy) {
87 const StructDate current(timeNow());
88 const StructDate sd2(doy,1,current.year4());
89 set(sd2.date().get());
90}
91
92short Date::doy_get() const {
93 const StructDate sd(*this);
94 return sd.dayOfYear1();
95}
96
97
98
99// Converting to string and streaming out is more a matter of DateStruct
100// but Date was the first.
101
102string Date::form(int s,bool trail0,const char *fmt) const {
103 const StructDate sd(*this);
104 string buffer(sd.form(fmt));
105 if(s>0) {
106 buffer.resize(s,' ');
107 if(trail0) buffer[s-1]= '\0';
108 }
109 return buffer;
110}
111
112
113//
114// prints out the date on the given ostream
115//
116ostream & Date::print(ostream &os,const char *fmt) const {
117 const StructDate sd(*this);
118 os << sd.form(fmt);
119 return os;
120}
121
122istream & Date::read_some(istream &is) {
123 string formS("");
124 if(is.peek() == '('
125 && is.get()) {
126 while(is.peek() != ')') {
127 formS = formS + char(is.get());
128 }
129 is.get();
130 if(formS == "rinex") return read_rinex(is);
131 else if(formS == "gps_tag") {
132 long tag;
133 if(is >> tag) {
134 tag_set(tag);
135 return is;
136 }
137 } else if (formS == "gps") {
138 short week;
139 long weekSec;
140 if ( is >> week
141 && is >> weekSec) {
142 gps_set(week,weekSec);
143 return is;
144 }
145 }
146 } else {
147 get_i(is);
148 }
149 return is;
150}
151
152
153//
154double Date::rinex_default_secondsFraction;
155
156//
157istream & Date::read_rinex(istream &is,double &fraction) {
158 StructDate sd;
159 float fsec;
160
161 if ( is >> sd.tms.tm_year
162 && is >> sd.tms.tm_mon
163 && is >> sd.tms.tm_mday
164 && is >> sd.tms.tm_hour
165 && is >> sd.tms.tm_min
166 && is >> fsec ) {
167 sd.tms.tm_sec = int(fsec);
168 sd.tms.tm_mon--;
169 fraction = fsec - sd.tms.tm_sec;
170 // y2k !!!
171 if(sd.tms.tm_year <= 80) sd.tms.tm_year += 100;
172 Date d2(sd.date());
173 set(d2.get());
174 }
175 return is;
176}
177
178//
179bool Date::get_i(istream &is) {
180 int day;
181 int month;
182 int year;
183
184 if( get_i2(is,day)
185 && get_i2(is,month)
186 && get_i2(is,year)) {
187 const StructDate sd(day,month,year);
188 *this = sd.date();
189 return true;
190 }
191 return false;
192}
193
194//
195// get2 - misplaces decimal to number converter and reader reads exactly 2 digits
196// and writes them into result.
197
198bool Date::get_i2(istream &is,int &result) {
199 char c1;
200 result= 0;
201
202 do {
203 is >> c1;
204 } while(is && (c1 == ' '));
205
206 if(isdigit(c1)) {
207 result = 10 * ( c1 - '0' );
208 char c2;
209 is >> c2;
210 if(isdigit(c2)) {
211 result += (c2 - '0');
212 return true;
213 } else is.putback(c2);
214 } else is.putback(c1);
215 return false;
216}
217
218// arquire the current time ...
219
220// DateType Date::_timeNow()
221// gets the current time
222DateType Date::_timeNow() {
223 return time(NULL);
224}
225
226//
227// (static) Date Date::time()
228// Constructs a Date from the system time at time of invocation.
229Date Date::timeNow() {
230 Date d;
231 d.data = _timeNow();
232 return d;
233}
234
Note: See TracBrowser for help on using the repository browser.