Files
spark/src/lib/utils/datetime.h
T

108 lines
4.7 KiB
C++
Executable File

#pragma once
#include "bts_export.h"
#include <string>
#include <chrono>
#include "windows_wrapper.h" // tick_count.h includes windows.h
namespace boost {namespace posix_time {class ptime;}}
namespace bts {namespace utils {class TimeStamp;}}
namespace bts {
namespace datetime {
typedef boost::posix_time::ptime PTime;
typedef unsigned long long LLDateTime;
// Simple Date structure
struct Date {
Date() : year(0), month(0), day(0) {}
Date(uint16_t y, uint8_t m, uint8_t d) : year(y), month(m), day(d) {}
Date(uint16_t date) : year((date >> 9) + 1970), month(date >> 5 & 15), day(date&31) {}
uint16_t year;
uint8_t month;
uint8_t day;
bool operator<(const Date& other) const {
if (year < other.year) return true;
if (year == other.year && month < other.month) return true;
if (year == other.year && month == other.month && day < other.day) return true;
return false;
}
bool operator>(const Date& other) const { return !(*this == other) && !(*this < other); }
bool operator==(const Date& other) const {
return day == other.day && month == other.month && year == other.year;
}
bool operator!=(const Date& other) const { return !(*this == other); }
uint16_t to_short() { return ((year - 1970) << 9) + (month << 5) + day; } // NOLINT
};
extern "C" BTS_EXPORT LLDateTime ll_now ();
extern "C" BTS_EXPORT LLDateTime ll_local_now ();
extern "C" BTS_EXPORT double OA_date_from_ll (LLDateTime ll);
extern "C" BTS_EXPORT LLDateTime ll_from_OA_date (double oa_date);
extern "C" BTS_EXPORT long unix_now();
BTS_EXPORT std::chrono::system_clock::time_point utc_ll_to_timepoint(unsigned long long ll) ;
BTS_EXPORT PTime now ();
BTS_EXPORT PTime local_now ();
BTS_EXPORT PTime make_ptime (int year, int month, int day);
BTS_EXPORT PTime make_ptime (int year, int month, int day, int hour, int minute, int second, int millisecond);
BTS_EXPORT LLDateTime ptime_to_ll (PTime const &t);
BTS_EXPORT PTime ll_to_ptime (LLDateTime ll);
extern "C" BTS_EXPORT int ll_to_cstr (LLDateTime ll, char*buffer, int buffer_sz);
BTS_EXPORT long long ll_diff_in_us (unsigned long long start, unsigned long long stop);
BTS_EXPORT long long ll_diff_in_seconds(unsigned long long start, unsigned long long stop);
BTS_EXPORT long long ptime_diff_in_us (PTime const &t1, PTime const &t2);
BTS_EXPORT long long ptime_diff_in_ms (PTime const &t1, PTime const &t2);
BTS_EXPORT long long ptime_diff_in_secs (PTime const &t1, PTime const &t2);
BTS_EXPORT PTime timestamp_to_ptime(utils::TimeStamp const&t);
utils::TimeStamp ptime_to_timestamp(PTime const &t);
BTS_EXPORT long long usecs_since (PTime const &t1);
BTS_EXPORT long long msecs_since (PTime const &t1);
BTS_EXPORT std::string time_string (PTime const &t);
BTS_EXPORT std::string date_string (PTime const &t);
BTS_EXPORT std::string iso_date_time_string (PTime const &t);
BTS_EXPORT PTime parse_iso_date_time_string (std::string const &t);
BTS_EXPORT std::string simple_date_time_string (PTime const &t);
BTS_EXPORT PTime parse_simple_date_time_string (std::string const &t);
BTS_EXPORT std::string FIX_date_time_string (PTime const &t);
BTS_EXPORT PTime parse_FIX_timestamp (const std::string &ts);
BTS_EXPORT std::string FIX_timestamp (PTime const &t);
BTS_EXPORT void FIX_timestamp (PTime const &t,std::string *out);
BTS_EXPORT bool is_from_today(PTime const &time);
BTS_EXPORT bool is_at_least_msecs_old (PTime const &time, uint64_t ms);
BTS_EXPORT bool is_at_least_secs_old (PTime const &time, uint64_t sec);
BTS_EXPORT bool is_at_least_mins_old (PTime const &time, uint64_t min);
BTS_EXPORT bool is_at_least_hours_old (PTime const &time, uint64_t hour);
BTS_EXPORT bool is_at_least_hours_old (PTime const &time, uint64_t hour);
BTS_EXPORT bool is_at_least_days_old (PTime const &time, uint64_t days);
BTS_EXPORT LLDateTime ll_from_unix_epoch(long);
BTS_EXPORT LLDateTime ll_from_unix_epoch_ms(long long);
extern "C" BTS_EXPORT void ll_to_parts(LLDateTime, int& year, int& month, int& day, int& hour, int& minute, int& second, int& millisecond);
BTS_EXPORT Date ll_to_date(LLDateTime);
extern "C" BTS_EXPORT LLDateTime ll_from_parts(int year, int month, int day, int hour, int minute, int second, int millisecond);
BTS_EXPORT LLDateTime ll_from_date(const Date& date);
bool is_local_time_sunday();
bool is_local_time_between(int hh_start, int mm_start, int hh_end, int mm_end);
#ifndef NO_CPP0X
extern "C" BTS_EXPORT LLDateTime ll_today();
BTS_EXPORT Date today();
BTS_EXPORT Date add_days(Date date, int days);
extern "C" BTS_EXPORT LLDateTime ll_yesterday();
#endif
}}
// namespace std {
// template <>
// struct numeric_limits<bts::datetime::Date> {
// static bts::datetime::Date min() { return bts::datetime::Date(1970,1,1); }
// static bts::datetime::Date max() { return bts::datetime::Date(2097,12,31); }
// };
// }