MCPcopy Create free account
hub / github.com/numpy/numpy / convert_pyobject_to_datetime_metadata

Function convert_pyobject_to_datetime_metadata

numpy/core/src/multiarray/datetime.c:1935–1985  ·  view source on GitHub ↗

* Converts an input object into datetime metadata. The input * may be either a string or a tuple. * * Returns 0 on success, -1 on failure. */

Source from the content-addressed store, hash-verified

1933 * Returns 0 on success, -1 on failure.
1934 */
1935NPY_NO_EXPORT int
1936convert_pyobject_to_datetime_metadata(PyObject *obj,
1937 PyArray_DatetimeMetaData *out_meta)
1938{
1939 if (PyTuple_Check(obj)) {
1940 return convert_datetime_metadata_tuple_to_datetime_metadata(
1941 obj, out_meta, NPY_FALSE);
1942 }
1943
1944 /* Get a UTF8 string */
1945 PyObject *utf8 = NULL;
1946 if (PyBytes_Check(obj)) {
1947 /* Allow bytes format strings: convert to unicode */
1948 utf8 = PyUnicode_FromEncodedObject(obj, NULL, NULL);
1949 if (utf8 == NULL) {
1950 return -1;
1951 }
1952 }
1953 else if (PyUnicode_Check(obj)) {
1954 utf8 = obj;
1955 Py_INCREF(utf8);
1956 }
1957 else {
1958 PyErr_SetString(PyExc_TypeError,
1959 "Invalid object for specifying NumPy datetime metadata");
1960 return -1;
1961 }
1962
1963 Py_ssize_t len = 0;
1964 char const *str = PyUnicode_AsUTF8AndSize(utf8, &len);
1965 if (str == NULL) {
1966 Py_DECREF(utf8);
1967 return -1;
1968 }
1969
1970 if (len > 0 && str[0] == '[') {
1971 int r = parse_datetime_metadata_from_metastr(str, len, out_meta);
1972 Py_DECREF(utf8);
1973 return r;
1974 }
1975 else {
1976 if (parse_datetime_extended_unit_from_string(str, len,
1977 NULL, out_meta) < 0) {
1978 Py_DECREF(utf8);
1979 return -1;
1980 }
1981
1982 Py_DECREF(utf8);
1983 return 0;
1984 }
1985}
1986
1987/*
1988 * Return the datetime metadata as a Unicode object.

Callers

nothing calls this directly

Tested by

no test coverage detected