Mantid
Loading...
Searching...
No Matches
DateAndTime.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
9#include <boost/python/class.hpp>
10#include <boost/python/make_constructor.hpp>
11#include <boost/python/operators.hpp> // Also provides self
12#include <cstddef>
13#include <memory>
14#include <numpy/arrayobject.h>
15#include <numpy/arrayscalars.h>
16
17using namespace Mantid::Types::Core;
18using namespace Mantid::PythonInterface;
19using namespace boost::python;
20using boost::python::arg;
21
28std::string ISO8601StringPlusSpace(const DateAndTime &self) {
29 // TODO this should cmake check and turn off the behavior
30 return self.toISO8601String() + " ";
31}
32
33int64_t total_nanoseconds(const DateAndTime &self) {
34 if (PyErr_Warn(PyExc_DeprecationWarning, ".total_nanoseconds() is deprecated. Use .totalNanoseconds() instead.") < 0)
35 throw boost::python::error_already_set();
36 return self.totalNanoseconds();
37}
38
40 class_<DateAndTime>("DateAndTime", no_init)
41 // Constructors
42 .def(init<const std::string>((arg("self"), arg("ISO8601 string")), "Construct from an ISO8601 string"))
43 .def(init<double, double>((arg("self"), arg("seconds"), arg("nanoseconds")),
44 "Construct using a number of seconds and nanoseconds (floats)since "
45 "1990-01-01T00:00"))
46 .def(init<int64_t, int64_t>((arg("self"), arg("seconds"), arg("nanoseconds")),
47 "Construct using a number of seconds and nanoseconds (integers) "
48 "since 1990-01-01T00:00"))
49 .def(init<int64_t>((arg("self"), arg("total_nanoseconds")),
50 "Construct a total number of nanoseconds since 1990-01-01T00:00"))
51 .def("__init__", make_constructor(Converters::to_dateandtime, default_call_policies(), (arg("other"))),
52 "Construct from numpy.datetime64")
53 .def("total_nanoseconds", &total_nanoseconds, arg("self"), "Since 1990-01-01T00:00")
54 .def("totalNanoseconds", &DateAndTime::totalNanoseconds, arg("self"), "Since 1990-01-01T00:00")
55 .def("toISO8601String", &DateAndTime::toISO8601String, arg("self"),
56 "Converts the time into ISO8601Standard and returns the string")
57 .def("setToMinimum", &DateAndTime::setToMinimum, arg("self"))
58 .def("to_datetime64", &Mantid::PythonInterface::Converters::to_datetime64, arg("self"),
59 "Convert to numpy.datetime64") // this is panda's name for the
60 // function
61 .def("__str__", &ISO8601StringPlusSpace, arg("self"))
62 .def("__long__", &DateAndTime::totalNanoseconds, arg("self"))
63 .def("__int__", &DateAndTime::totalNanoseconds, arg("self"))
64 .def(self == self)
65 .def(self != self)
66 // cppcheck-suppress duplicateExpression
67 .def(self < self)
68 .def(self + int64_t())
69 .def(self += int64_t())
70 .def(self - int64_t())
71 .def(self -= int64_t())
72 .def(self - self);
73}
74
75int64_t time_duration_total_nanoseconds(time_duration &self) {
76 if (PyErr_Warn(PyExc_DeprecationWarning, ".total_nanoseconds() is deprecated. Use .totalNanoseconds() instead.") < 0)
77 throw boost::python::error_already_set();
78 return self.total_nanoseconds();
79}
80
82 class_<time_duration>("time_duration", no_init)
83 .def("hours", &time_duration::hours, arg("self"), "Returns the normalized number of hours")
84 .def("minutes", &time_duration::minutes, arg("self"), "Returns the normalized number of minutes +/-(0..59)")
85 .def("seconds", &time_duration::seconds, arg("self"), "Returns the normalized number of seconds +/-(0..59)")
86 .def("total_seconds", &time_duration::total_seconds, arg("self"),
87 "Get the total number of seconds truncating any fractional seconds")
88 .def("total_milliseconds", &time_duration::total_milliseconds, arg("self"),
89 "Get the total number of milliseconds truncating any remaining "
90 "digits")
91 .def("total_microseconds", &time_duration::total_microseconds, arg("self"),
92 "Get the total number of microseconds truncating any remaining "
93 "digits")
94 .def("total_nanoseconds", &time_duration_total_nanoseconds, arg("self"),
95 "Get the total number of nanoseconds truncating any remaining "
96 "digits")
97 .def("totalNanoseconds", &time_duration::total_nanoseconds, arg("self"),
98 "Get the total number of nanoseconds truncating any remaining "
99 "digits");
100}
void export_DateAndTime()
int64_t time_duration_total_nanoseconds(time_duration &self)
std::string ISO8601StringPlusSpace(const DateAndTime &self)
Circumvent a bug in IPython 1.1, which chokes on nanosecond precision datetimes.
int64_t total_nanoseconds(const DateAndTime &self)
void export_time_duration()
MANTID_PYTHONINTERFACE_CORE_DLL std::shared_ptr< Types::Core::DateAndTime > to_dateandtime(const boost::python::api::object &value)
MANTID_PYTHONINTERFACE_CORE_DLL PyObject * to_datetime64(const Types::Core::DateAndTime &dateandtime)
Convert to numpy's datetime64. This is panda's name for the function.