Menu

[r12]: / trunk / libtop_engine / rdf_date_time.h  Maximize  Restore  History

Download this file

148 lines (118 with data), 4.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#ifndef RDF_DATE_TIME_H_
#define RDF_DATE_TIME_H_
#include "rdf_rule_core.h"
namespace rdf {
using boost::gregorian::from_simple_string;
using boost::gregorian::to_iso_extended_string;
using boost::posix_time::ptime;
using boost::posix_time::time_duration;
using boost::posix_time::time_from_string;
using boost::posix_time::duration_from_string;
using boost::posix_time::to_simple_string;
namespace internal {
inline ptime as_ptime(unsigned int const t)
{
// decode unsigned int into a ptime:
// - 2378497 is the offset date of 1800-01-01
// - 20 bits are used to store the number of days since the offset date
// - 6 bits are used for the number of hours in the day.
// - 6 bits are used for the number of minutes in the day.
// - seconds are not stored.
int nmin = t & 0x003F;
int nhrs = (t & 0x0FC0) >> 6;
unsigned int ndays = (t >> 12) + 2378497;
return ptime(date_adaptor(ndays), time_duration(nhrs, nmin, 0));
};
inline unsigned int as_number(ptime const& t)
{
// encode ptime into an unsigned int:
// - 2378497 is the offset date of 1800-01-01
// - 20 bits are used to store the number of days since the offset date
// - 6 bits are used for the number of hours in the day.
// - 6 bits are used for the number of minutes in the day.
// - seconds are not stored.
unsigned int ndays = (date_adaptor(t.date()).as_number() - 2378497) << 12;
time_duration td = t.time_of_day();
return ndays | (td.hours()<<6) | td.minutes();
};
}; /* internal namespace */
inline time_type as_time_type(ptime const& t)
{
time_type d;
d.d = internal::as_number(t);
return d;
};
inline ptime as_ptime(time_type const& t)
{
return internal::as_ptime(t.d);
};
namespace internal {
inline unsigned int as_number(time_duration const& t)
{
// encode time_duration into an unsigned int:
// - 26 bits are used to store the number of hours of the duration
// maximum duration is 134,217,727 hours or 5,592,405 days or 15,321 years!
// - 6 bits are used for the number of minutes.
// - seconds are not stored.
return (t.hours() << 6) | t.minutes();
};
inline time_duration as_time_duration(unsigned int const t)
{
// decode unsigned int into a time_duration:
// - 26 bits are used to store the number of hours of the duration
// maximum duration is 134,217,727 hours or 5,592,405 days or 15,321 years!
// - 6 bits are used for the number of minutes.
// - seconds are not stored.
int nmin = t & 0x003F;
int nhrs = t >> 6;
return time_duration(nhrs, nmin, 0, 0);
};
}; /* internal namespace */
inline duration_type as_duration_type(time_duration const& t)
{
duration_type d;
d.d = internal::as_number(t);
return d;
};
inline time_duration as_time_duration(duration_type const& t)
{
return internal::as_time_duration(t.d);
};
// utility methods
inline std::string to_string(date_type const d){return to_iso_extended_string(date_adaptor(d));};
inline std::string to_string(time_type const d){return to_simple_string(as_ptime(d));};
inline std::string to_string(duration_type const d){return to_simple_string(as_time_duration(d));};
inline date_type date_type_from_string(std::string const& s){return date_adaptor(from_simple_string(s)).as_date_type();};
inline time_type time_type_from_string(std::string const& s){return as_time_type(time_from_string(s));};
inline duration_type duration_type_from_string(std::string const& s){return as_duration_type(duration_from_string(s));};
//////////////////////////////////////////////////////////////////////////////////////////////////////
// to_string for literal class
//
//////////////////////////////////////////////////////////////////////////////////////////////////////
inline std::ostream& operator<<(std::ostream& out, literal const& l)
{
out << "\"";
switch(l.m_type)
{
case unset: out << l.m_data.m_nv; break;
case integer: out << l.m_data.m_int; break;
case unsigned_integer: out << l.m_data.m_uint; break;
case real: out << l.m_data.m_real; break;
case date: out << to_string(l.m_data.m_date); break;
case time: out << to_string(l.m_data.m_time); break;
case duration: out << to_string(l.m_data.m_duration); break;
default: out << l.m_str;
};
return out << "\"";
};
inline std::ostream& operator<<(std::ostream& out, resource_base const& r)
{
if(r.is_literal()) {
return out << static_cast<literal const&>(r);
} else {
out << r.get_name();
}
return out;
};
}; /* rdf namespace */
#endif /*RDF_DATE_TIME_H_*/
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.