#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_*/