hikyuu2/hikyuu_pywrap/_Datetime.cpp

113 lines
6.2 KiB
C++
Raw Normal View History

2015-01-07 01:26:14 +08:00
/*
* _Datetime.cpp
*
* Created on: 2012-9-27
* Author: fasiondog
*/
#include <hikyuu/serialization/Datetime_serialization.h>
2023-12-27 02:16:10 +08:00
#include "pybind_utils.h"
2015-01-07 01:26:14 +08:00
using namespace hku;
2023-12-26 18:25:50 +08:00
namespace py = pybind11;
2015-01-07 01:26:14 +08:00
2023-12-26 18:25:50 +08:00
void export_Datetime(py::module& m) {
py::class_<Datetime>(m, "Datetime",
R"(日期时间类(精确到微秒),通过以下方式构建:
2020-06-26 21:39:53 +08:00
- Datetime("2010-1-1 10:00:00")Datetime("2001-1-1")
Datetime("20010101")Datetime("20010101T232359)
- Python dateDatetime(date(2010,1,1))
- Python datetimeDatetime(datetime(2010,1,1,10)
- YYYYMMDDHHMMss YYYYMMDDHHMM YYYYMMDD Datetime(201001011000)Datetime(20010101)
2020-06-26 21:39:53 +08:00
- Datetime(year, month, day, hour=0, minute=0, second=0, millisecond=0, microsecond=0))")
2023-12-26 18:25:50 +08:00
.def(py::init<>())
.def(py::init<const std::string&>())
.def(py::init<unsigned long long>())
2024-09-02 15:24:05 +08:00
.def(py::init<const Datetime&>())
2023-12-26 18:25:50 +08:00
.def(py::init<long, long, long, long, long, long, long, long>(), py::arg("year"),
py::arg("month"), py::arg("day"), py::arg("hour") = 0, py::arg("minute") = 0,
py::arg("second") = 0, py::arg("millisecond") = 0, py::arg("microsecond") = 0)
.def("__init__",
[](Datetime& self, const py::object& source) { self = pydatetime_to_Datetime(source); })
2020-06-26 21:39:53 +08:00
2023-12-28 12:30:33 +08:00
.def("__str__", &Datetime::str)
.def("__repr__", &Datetime::repr)
2020-06-26 21:39:53 +08:00
2023-12-26 18:25:50 +08:00
.def_property_readonly("year", &Datetime::year, "")
.def_property_readonly("month", &Datetime::month, "")
.def_property_readonly("day", &Datetime::day, "")
.def_property_readonly("hour", &Datetime::hour, "")
.def_property_readonly("minute", &Datetime::minute, "")
.def_property_readonly("second", &Datetime::second, "")
.def_property_readonly("millisecond", &Datetime::millisecond, "毫秒")
.def_property_readonly("microsecond", &Datetime::microsecond, "微秒")
.def_property_readonly("number", &Datetime::number, "返回显示如 YYYYMMDDhhmm 的数字")
.def_property_readonly("hex", &Datetime::hex,
"返回用后7个字节表示世纪、世纪年、月、日、时、分、秒的64位整数")
.def_property_readonly("ym", &Datetime::ym, "返回显示如 YYYYMM 的数字")
.def_property_readonly("ymd", &Datetime::ymd, "返回显示如 YYYYMMDD 的数字")
.def_property_readonly("ymdh", &Datetime::ymdh, "返回显示如 YYYYMMDDhh 的数字")
.def_property_readonly("ymdhm", &Datetime::ymdhm, "返回显示如 YYYYMMDDhhmm 的数字")
.def_property_readonly("ymdhms", &Datetime::ymdhms, "返回显示如 YYYYMMDDhhmms 的数字")
.def_property_readonly("ticks", &Datetime::ticks, "返回距离最小日期过去的微秒数")
2020-07-08 00:18:51 +08:00
.def("is_null", &Datetime::isNull, "\n是否是Null值等于 Datetime() 直接创建的对象")
2020-06-26 21:39:53 +08:00
2021-01-31 00:37:05 +08:00
.def("day_of_week", &Datetime::dayOfWeek, "\n返回是一周中的第几天周日为0周一为1")
2020-07-08 00:18:51 +08:00
.def("day_of_year", &Datetime::dayOfYear, "\n返回一年中的第几天1月1日为一年中的第1天")
.def("start_of_day", &Datetime::startOfDay, "\n返回当天 0点0分0秒")
2021-01-31 00:37:05 +08:00
.def("end_of_day", &Datetime::endOfDay, "\n返回当日 23点59分59秒")
2020-07-08 00:18:51 +08:00
.def("next_day", &Datetime::nextDay, "\n返回下一自然日")
.def("next_week", &Datetime::nextWeek, "\n返回下周周一日期")
.def("next_month", &Datetime::nextMonth, "\n返回下月首日日期")
.def("next_quarter", &Datetime::nextQuarter, "\n返回下一季度首日日期")
.def("next_halfyear", &Datetime::nextHalfyear, "\n返回下一半年度首日日期")
.def("next_year", &Datetime::nextYear, "\n返回下一年度首日日期")
.def("pre_day", &Datetime::preDay, "\n返回前一自然日日期")
.def("pre_week", &Datetime::preWeek, "\n返回上周周一日期")
.def("pre_month", &Datetime::preMonth, "\n返回上月首日日期")
.def("pre_quarter", &Datetime::preQuarter, "\n返回上一季度首日日期")
.def("pre_halfyear", &Datetime::preHalfyear, "\n返回上一半年度首日日期")
.def("pre_year", &Datetime::preYear, "\n返回上一年度首日日期")
.def("date_of_week", &Datetime::dateOfWeek,
2020-06-26 21:39:53 +08:00
R"(
2023-12-26 18:25:50 +08:00
06
:param int day: 066)")
2020-06-26 21:39:53 +08:00
2020-07-08 00:18:51 +08:00
.def("start_of_week", &Datetime::startOfWeek, "\n返回周起始日期(周一)")
.def("end_of_week", &Datetime::endOfWeek, "\n返回周结束日期(周日)")
.def("start_of_month", &Datetime::startOfMonth, "\n返回月度起始日期")
.def("end_of_month", &Datetime::endOfMonth, "\n返回月末最后一天日期")
.def("start_of_quarter", &Datetime::startOfQuarter, "\n返回季度起始日期")
.def("end_of_quarter", &Datetime::endOfQuarter, "\n返回季度结束日期")
.def("start_of_halfyear", &Datetime::startOfHalfyear, "\n返回半年度起始日期")
.def("end_of_halfyear", &Datetime::endOfHalfyear, "\n返回半年度结束日期")
.def("start_of_year", &Datetime::startOfYear, "\n返回年度起始日期")
2020-06-27 00:43:47 +08:00
.def("endOfYear", &Datetime::endOfYear, "\n返回年度结束日期")
2023-12-28 02:21:36 +08:00
.def_static("min", &Datetime::min, "\n获取支持的最小日期, Datetime(1400, 1, 1)")
.def_static("max", &Datetime::max, "\n获取支持的最大日期, Datetime(9999, 12, 31)")
.def_static("now", &Datetime::now, "\n获取系统当前日期时间")
.def_static("today", &Datetime::today, "\n获取当前的日期")
.def_static("from_hex", &Datetime::fromHex, "\n兼容oracle用后7个字节表示的datetime")
2019-12-19 02:32:06 +08:00
2023-12-26 18:25:50 +08:00
.def(py::self == py::self)
.def(py::self != py::self)
.def(py::self >= py::self)
.def(py::self <= py::self)
.def(py::self > py::self)
.def(py::self < py::self)
.def(py::self - py::self)
2020-06-27 00:43:47 +08:00
2023-12-26 18:25:50 +08:00
.def(py::self + TimeDelta())
2023-12-27 02:16:10 +08:00
//.def(other<TimeDelta>() + self) 在python里扩展支持
2023-12-26 18:25:50 +08:00
.def(py::self - TimeDelta())
2019-12-19 02:32:06 +08:00
2023-12-26 18:25:50 +08:00
DEF_PICKLE(Datetime);
2017-09-23 19:51:22 +08:00
2023-12-26 18:25:50 +08:00
m.def("get_date_range", getDateRange, py::arg("start"), py::arg("end"));
2015-01-07 01:26:14 +08:00
}