Files
spark/src/lib/utils/datetime.h
T
2025-01-03 16:59:07 +00:00

168 lines
5.1 KiB
C++

#pragma once
#include "bts_export.h"
#include <chrono>
#include <string>
#include "windows_wrapper.h" // tick_count.h includes windows.h
namespace boost {
namespace posix_time {
class ptime;
}
} // namespace boost
namespace boost {
namespace posix_time {
class time_duration;
}
} // namespace boost
namespace bts {
namespace utils {
class TimeStamp;
}
} // namespace bts
namespace bts {
namespace datetime {
typedef boost::posix_time::ptime PTime;
typedef boost::posix_time::time_duration PDuration;
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() const {
return ((year - 1970) << 9) + (month << 5) + day;
} // NOLINT
};
extern "C" BTS_EXPORT LLDateTime ll_now();
extern "C" LLDateTime ll_local_now();
extern "C" double OA_date_from_ll(LLDateTime ll);
extern "C" LLDateTime ll_from_OA_date(double oa_date);
extern "C" long unix_now();
#ifndef _WIN32
inline uint64_t unix_nano_now() {
struct timespec ts;
// CLOCK_REALTIME is already anchored at the unix epoch
clock_gettime(CLOCK_REALTIME, &ts);
return static_cast<int64_t>(1000000000UL) * static_cast<int64_t>(ts.tv_sec) +
static_cast<int64_t>(ts.tv_nsec);
}
#endif
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);
LLDateTime ptime_to_ll(PTime const &t);
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);
long long ptime_diff_in_us(PTime const &t1, PTime const &t2);
long long ptime_diff_in_ms(PTime const &t1, PTime const &t2);
long long ptime_diff_in_secs(PTime const &t1, PTime const &t2);
PTime timestamp_to_ptime(utils::TimeStamp const &t);
utils::TimeStamp ptime_to_timestamp(PTime const &t);
long long usecs_since(PTime const &t1);
long long msecs_since(PTime const &t1);
std::string time_string(PTime const &t);
std::string date_string(PTime const &t);
std::string iso_date_time_string(PTime const &t);
PTime parse_iso_date_time_string(std::string const &t);
PTime parse_iso_date_time_tz(const std::string &date);
std::string simple_date_time_string(PTime const &t);
PTime parse_simple_date_time_string(std::string const &t);
PDuration parse_duration(std::string const &time_str);
std::string FIX_date_time_string(PTime const &t);
PTime parse_FIX_timestamp(const std::string &ts);
std::string FIX_timestamp(PTime const &t);
void FIX_timestamp(PTime const &t, std::string *out);
bool is_from_today(PTime const &time);
bool is_at_least_msecs_old(PTime const &time, uint64_t ms);
bool is_at_least_secs_old(PTime const &time, uint64_t sec);
bool is_at_least_mins_old(PTime const &time, uint64_t min);
bool is_at_least_hours_old(PTime const &time, uint64_t hour);
bool is_at_least_hours_old(PTime const &time, uint64_t hour);
bool is_at_least_days_old(PTime const &time, uint64_t days);
LLDateTime ll_from_unix_epoch(long);
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 datetime
} // namespace bts
// 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); }
// };
// }