ClockWork DB CoreAPI 1.0.48
Abstract Time Series and Storage/Management Library
Loading...
Searching...
No Matches
dates.hpp
1/* don't include this
2 * It is included automatically by scalar.hpp and is only
3 * separate to ease maintenance!
4 */
5#ifndef HAVE_TOM__DATES_HPP
6#define HAVE_TOM__DATES_HPP
7
8#include <tom-util/calendar.hpp>
9
10namespace tom {
11
12using tom::calendars::greg_year;
13using tom::calendars::greg_month;
14using tom::calendars::greg_day;
15
16class TOM_UTIL_API date_interval_observation;
17
18// Date is just an ulong that holds the value of days() for a date.
19// subsequent date_intervals will hold ulongs as well that are
20// tied to a specific calendar
21class TOM_UTIL_API Date : public scalar<unsigned int>
22{
23public:
24 typedef unsigned int value_type;
29
30 Date( ) : scalar<value_type>() { }
32 Date( greg_year y, greg_month m, greg_day d );
33
34// Date( const tom::observation &year, const tom::observation &month, const tom::observation &day );
35 Date( tom::calendars::date d ) : scalar<value_type>( d.days() ) { }
36 // set Date by a date string
37 Date(const tom::observation &str_date, const tom::observation &image);
38
39 // set missing type Dates
41
42 // this maybe dangerous but i'll leave it 4 now...tom mccubbin
43 Date( tom::calendars::date_int_type d ) : scalar<value_type> ( d ) { }
44 // same as a date_int_type!
45 //Date( value_type d ) : scalar<value_type>( d ) { }
46
47 // we must implement, but be carefull, as not all observations
48 // can logically convert to the internal date representation
49 virtual
51 operator=( const observation &rhs );
52
53
54 Date &
55 operator=( const date_interval_observation & );
56
57 // safe assignment w/out check
58 Date &
59 operator=( const tom::calendars::date &rhs );
60
61 // duh...
62 Date &
63 operator=( const Date & rhs );
64
65 // assign missing types
66 Date &
67 operator=( tom::value_type t );
68
69 Date &
70 operator=( value_type t ) { m_value = t; return *this; }
71
72 // print me
73 virtual
74 std::ostream &
75 print( std::ostream &os ) const;
76
77};
78
79// abstract date_interval
80// deriving classes need only fullfill
81class TOM_UTIL_API date_interval_observation : public Date
82{
83public:
84 typedef Date::value_type value_type;
89
90 // constructors
92 { }
94 { }
95
96 // d must have been converted using the appropriate calendar
97 date_interval_observation( tom::calendars::date_int_type d ) : Date( d )
98 { }
99
100 // copy a date_interval_observation
102
103 // assignment
104 virtual
106 operator=( const date_interval_observation & );
107
108 virtual
110 operator=( const observation & );
111
112 virtual
114 operator=( const tom::Date & );
115
116 // these are the additions to the interface
117 virtual
119 get_calendar() const = 0;
120
121 // special overload for virtual operator unsigned long int
122 // to adjust from calendar back to natural date.days()
123 virtual
124 operator unsigned long int() const
125 {
126 if ( is_normal() )
127 return get_calendar()( m_value.value_ ).days();
128 else
129 return m_value.value_;
130 }
131 virtual
133 get_range() const;
134
135 // print here! we know what to do w/out
136 // worrying about the calendar
137 virtual
138 std::ostream &
139 print( std::ostream &os ) const;
140
141 virtual
142 bool
143 operator==( const observation & rhs ) const
144 {
145 scalar_type tmp = cast_observation_<Date::value_type>( rhs );
146 if ( tmp.is_normal() )
147 return m_value == get_calendar()( tom::calendars::date( tmp.value_ ) );
148 else
149 return m_value == tmp;
150 }
151 virtual
152 bool
153 operator!=( const observation & rhs ) const
154 {
155 return ! ( *this == rhs );
156 }
157
158};
159
160template <typename CalendarT>
162{
163public:
164 typedef date_interval_observation::value_type value_type;
168 typedef CalendarT calendar_type;
169
170 // constructors
172 { }
174 date_interval_observation( calendar_type::Instance()(d) )
175 { }
177 date_interval_scalar( calendars::greg_year y,calendars::greg_month m, calendars::greg_day d ) :
178 date_interval_observation( calendar_type::Instance()( calendars::date( y,m,d) ) )
179 { }
181 { }
182
183 // this seems like bad policy...do we trust them to
184 // pass valid date_int_types???
185 date_interval_scalar( value_type d )
186 {
187 m_value = d;
188 }
189
190 // copy constructors
193
194 template <typename CalendarTp>
196
198 clone() const
199 {
200 return *new date_interval_scalar( *this );
201 }
202
203 // asignment operators
204 // not dangerous...may result in ND though
205 virtual
207 operator=( const observation &rhs );
208
210 operator=( const date_interval_scalar & );
211
212 template <typename CalendarTp>
214 operator=( const date_interval_scalar<CalendarTp> & );
215
217 operator=( const Date & );
218
220 operator=( const tom::calendars::date &rhs );
221
222 // not a good idea! lets axe this one...
224 operator=( const value_type &rhs );
225
227 operator=( tom::value_type t );
228
229 virtual
231 get_calendar() const { return calendar_type::Instance(); }
232
233 static const date_interval_scalar HAS_NO_DATA;
234 static const date_interval_scalar HOLIDAY;
235 static const date_interval_scalar NON_CALC;
236
237};
238
239
271
272// aliases for common DateIntervals
277
278
279template <typename CalendarT>
281{
282 if ( d.is_normal() )
283 // why use operator unsigned long?
284 // because that offers the Date, or derivative to adjust the value
285 // from the calendar, back to the normal Date int!
286 m_value = calendar_type::Instance()( tom::calendars::date( d.operator unsigned long() ) );
287 else
288 m_value = d.value();
289}
290template <typename CalendarT>
291date_interval_scalar<CalendarT>::date_interval_scalar( const observation & rhs )
292{
293 m_value = cast_observation_<typename date_interval_scalar<CalendarT>::value_type> ( rhs );
294 if ( this->is_normal() )
295 m_value = this->get_calendar()( tom::calendars::date( m_value.value() ) );
296
297}
298template <typename CalendarT>
299date_interval_scalar<CalendarT>::date_interval_scalar( const date_interval_scalar &rhs )
300{
301 m_value = rhs.value();
302}
303template <typename CalendarT>
304template <typename CalendarTp>
305date_interval_scalar<CalendarT>::date_interval_scalar( const date_interval_scalar<CalendarTp> &rhs )
306{
307 if ( rhs.is_normal() )
308 // this adjusts for differnt calendars
309 m_value = get_calendar()( rhs.get_calendar() ( rhs.value() ) );
310 else
311 m_value = rhs.value();
312}
313
314template <typename CalendarT>
315inline
316date_interval_scalar<CalendarT> &
317date_interval_scalar<CalendarT>::operator=( const date_interval_scalar & rhs )
318{
319 m_value = rhs.value();
320 return *this;
321}
322
323template <typename CalendarT>
324template <typename CalendarTp>
325inline
326date_interval_scalar<CalendarT> &
327date_interval_scalar<CalendarT>::operator=( const date_interval_scalar<CalendarTp> & rhs)
328{
329 if ( rhs.is_normal() )
330 m_value = get_calendar()( rhs.get_calendar()( rhs.value() ) );
331 else
332 m_value = rhs.value();
333 return *this;
334}
335
336template <typename CalendarT>
337inline
338date_interval_scalar<CalendarT> &
339date_interval_scalar<CalendarT>::operator=( const tom::calendars::date &rhs )
340{
341 m_value = get_calendar()( rhs );
342 return *this;
343}
344template <typename CalendarT>
345inline
346date_interval_observation &
347date_interval_scalar<CalendarT>::operator=( const observation &rhs )
348{
349 // warning: this is inplace for date_interval_observations
350 // and should be avoided. Strings, Floats, Doubles,
351 // and others probably won't asign as expected!
352 m_value = rhs.operator value_type();
353
354 // lets adjust for the calendar
355 m_value = this->get_calendar()( tom::calendars::date( m_value.value() ) );
356 return *this;
357}
358template <typename CalendarT>
359inline
360date_interval_scalar<CalendarT> &
361date_interval_scalar<CalendarT>::operator=( const value_type &rhs )
362{
363 // it assumed that if you pass the underlying
364 // value_type that is has been adjusted by
365 // the calendar...this operator is mainly
366 // needed for initializing from persisted
367 // data! Programmers should really use another
368 // operator!!!
369 this->Date::operator=( rhs );
370 return *this;
371}
372template <typename CalendarT>
373inline
374date_interval_scalar<CalendarT> &
375date_interval_scalar<CalendarT>::operator=( tom::value_type t )
376{
377 this->Date::operator=( t );
378 return *this;
379}
380template <typename CalendarT>
381inline
382date_interval_scalar<CalendarT> &
383date_interval_scalar<CalendarT>::operator=( const Date & rhs )
384{
385 if ( rhs.is_normal() )
386 m_value = get_calendar()( tom::calendars::date( rhs.value() ) );
387 else
388 m_value = rhs.value();
389 return *this;
390}
391
392template <typename CalendarT>
393const date_interval_scalar<CalendarT>
394date_interval_scalar<CalendarT>::HAS_NO_DATA( tom::value_type::no_data() );
395
396template <typename CalendarT>
397const date_interval_scalar<CalendarT>
398date_interval_scalar<CalendarT>::HOLIDAY( tom::value_type::holiday() );
399
400template <typename CalendarT>
401const date_interval_scalar<CalendarT>
402date_interval_scalar<CalendarT>::NON_CALC( tom::value_type::non_calc() );
403
404
405} // end tom namespace
406
407#endif
Definition dates.hpp:22
Definition calendar.hpp:120
Definition calendar.hpp:47
Definition calendar.hpp:147
Definition dates.hpp:82
Definition dates.hpp:162
Definition observation.hpp:13
Definition scalar.hpp:19
Definition value_types.hpp:381
Definition value_types.hpp:144
Definition value_types.hpp:324
Definition value_types.hpp:319
Definition value_types.hpp:329