Awkward Array: include/awkward/datetime_util.h Source File

Loading...
Searching...
No Matches
datetime_util.h
Go to the documentation of this file.
1// BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE
2
3#ifndef AWKWARD_DATETIME_UTIL_H_
4#define AWKWARD_DATETIME_UTIL_H_
5
6#include <string>
7#include <vector>
8#include <map>
9#include <memory>
10#include <array>
11
12#include "awkward/common.h"
13
14#ifndef _MSC_VER
15 #include "dlfcn.h"
16#endif
17
18namespace awkward {
19
20 namespace util {
22 const std::string
23 format_to_units(const std::string& format);
24
26 const std::string
27 units_to_format(dtype dt, const std::string& units, int64_t step);
28
29 template <class T>
30 struct NameValuePair {
31 using value_type = T;
32 const T value;
33 const char* const name;
34 const int64_t scale_up;
35 const int64_t scale_down;
36 };
37
38 template <class Mapping, class V>
39 std::string name(Mapping a, V value) {
40 auto pos = std::find_if(
41 std::begin(a), std::end(a), [&value](const typename Mapping::value_type& t) { return ((V)t.value == value); });
42 if (pos != std::end(a)) {
43 return pos->name;
44 }
45 return std::begin(a)->name;
46 }
47
48 template <class Mapping>
49 typename Mapping::value_type::value_type value(Mapping a, const std::string& name) {
50 auto unit_name(name);
51 std::string chars = "[]1234567890";
52 unit_name.erase(remove_if(unit_name.begin(), unit_name.end(),
53 [&chars](const char &c) {
54 return chars.find(c) != std::string::npos;
55 }),
56 unit_name.end());
57 auto pos = std::find_if(
58 std::begin(a), std::end(a), [&](const typename Mapping::value_type& t) {
59 return (unit_name == t.name);
60 });
61 if (pos != std::end(a)) {
62 return pos->value;
63 }
64 return std::begin(a)->value;
65 }
66
70 enum class datetime_units {
71 unknown = -1, // unknown
72 Y = 1, // year
73 M = 2, // month
74 W = 3, // week
75 D = 4, // day
76 h = 5, // hour
77 m = 6, // minute
78 s = 7, // second
79 ms = 8, // millisecond
80 us = 9, // microsecond: note, 'μs' string is not supported
81 ns = 10, // nanosecond
82 ps = 11, // picosecond
83 fs = 12, // femtosecond
84 as = 13, // attosecond
85 };
86
87 // One calendar common year has 365 days: 31536000 seconds
88 // One calendar leap year has 366 days: 31622400 seconds
89 const std::array<const NameValuePair<datetime_units>, 14> units_map {
90 {{datetime_units::unknown, "unknown", 1, 1},
91 {datetime_units::Y, "Y", 31556952, 1}, // seconds in average Gregorian year
92 {datetime_units::M, "M", 2629746, 1},
93 {datetime_units::W, "W", 604800, 1},
94 {datetime_units::D, "D", 86400, 1},
95 {datetime_units::h, "h", 3600, 1},
96 {datetime_units::m, "m", 60, 1},
97 {datetime_units::s, "s", 1, 1},
98 {datetime_units::ms, "ms", 1, 1000},
99 {datetime_units::us, "us", 1, 1000000},
100 {datetime_units::ns, "ns", 1, 1000000000},
101 {datetime_units::ps, "ps", 1, 1000000000000},
102 {datetime_units::fs, "fs", 1, 1000000000000000},
103 {datetime_units::as, "as", 1, 1000000000000000000}}
104 };
105
106 std::tuple<std::string, int64_t> datetime_data(const std::string& format);
107
108 double scale_from_units(const std::string& format, uint64_t index);
109 }
110}
111
112#endif // AWKWARD_DATETIME_UTIL_H_
common.h
awkward::util::units_map
const std::array< const NameValuePair< datetime_units >, 14 > units_map
Definition: datetime_util.h:89
awkward::util::datetime_data
std::tuple< std::string, int64_t > datetime_data(const std::string &format)
awkward::util::scale_from_units
double scale_from_units(const std::string &format, uint64_t index)
awkward::util::datetime_units
datetime_units
Valid datetime units. Different units of two datetime type Arrays will be normalized to the smallest ...
Definition: datetime_util.h:70
awkward::util::datetime_units::s
@ s
awkward::util::datetime_units::us
@ us
awkward::util::datetime_units::h
@ h
awkward::util::datetime_units::Y
@ Y
awkward::util::datetime_units::W
@ W
awkward::util::datetime_units::M
@ M
awkward::util::datetime_units::m
@ m
awkward::util::datetime_units::ps
@ ps
awkward::util::datetime_units::unknown
@ unknown
awkward::util::datetime_units::fs
@ fs
awkward::util::datetime_units::ms
@ ms
awkward::util::datetime_units::ns
@ ns
awkward::util::datetime_units::D
@ D
awkward::util::datetime_units::as
@ as
awkward::util::units_to_format
const std::string units_to_format(dtype dt, const std::string &units, int64_t step)
Convert a datetime units string into a format string.
awkward::util::value
Mapping::value_type::value_type value(Mapping a, const std::string &name)
Definition: datetime_util.h:49
awkward::util::dtype
dtype
NumPy dtypes that can be interpreted within Awkward C++ (only the primitive, fixed-width types)....
Definition: util.h:26
awkward::util::name
std::string name(Mapping a, V value)
Definition: datetime_util.h:39
awkward::util::format_to_units
const std::string format_to_units(const std::string &format)
Convert a format string into a datetime units string.
awkward
Definition: BitMaskedArray.h:15
awkward::state::index
@ index
awkward::util::NameValuePair
Definition: datetime_util.h:30
awkward::util::NameValuePair::scale_up
const int64_t scale_up
Definition: datetime_util.h:34
awkward::util::NameValuePair::scale_down
const int64_t scale_down
Definition: datetime_util.h:35
awkward::util::NameValuePair::value
const T value
Definition: datetime_util.h:32
awkward::util::NameValuePair::name
const char *const name
Definition: datetime_util.h:33
awkward::util::NameValuePair::value_type
T value_type
Definition: datetime_util.h:31