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

Function time_to_string_resolve_descriptors

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

In the current setup both strings and unicode casts support all outputs */

Source from the content-addressed store, hash-verified

3940
3941/* In the current setup both strings and unicode casts support all outputs */
3942static NPY_CASTING
3943time_to_string_resolve_descriptors(
3944 PyArrayMethodObject *NPY_UNUSED(self),
3945 PyArray_DTypeMeta *dtypes[2],
3946 PyArray_Descr **given_descrs,
3947 PyArray_Descr **loop_descrs,
3948 npy_intp *NPY_UNUSED(view_offset))
3949{
3950 if (given_descrs[1] != NULL && dtypes[0]->type_num == NPY_DATETIME) {
3951 /*
3952 * At the time of writing, NumPy does not check the length here,
3953 * but will error if filling fails.
3954 */
3955 Py_INCREF(given_descrs[1]);
3956 loop_descrs[1] = given_descrs[1];
3957 }
3958 else {
3959 /* Find the correct string length, possibly based on the unit */
3960 int size;
3961 if (given_descrs[0]->type_num == NPY_DATETIME) {
3962 PyArray_DatetimeMetaData *meta = get_datetime_metadata_from_dtype(given_descrs[0]);
3963 assert(meta != NULL);
3964 size = get_datetime_iso_8601_strlen(0, meta->base);
3965 }
3966 else {
3967 /*
3968 * This is arguably missing space for the unit, e.g. for:
3969 * `np.timedelta64(1231234342124, 'ms')`
3970 */
3971 size = 21;
3972 }
3973 if (dtypes[1]->type_num == NPY_UNICODE) {
3974 size *= 4;
3975 }
3976 loop_descrs[1] = PyArray_DescrNewFromType(dtypes[1]->type_num);
3977 if (loop_descrs[1] == NULL) {
3978 return -1;
3979 }
3980 loop_descrs[1]->elsize = size;
3981 }
3982
3983 loop_descrs[0] = NPY_DT_CALL_ensure_canonical(given_descrs[0]);
3984 if (loop_descrs[0] == NULL) {
3985 Py_DECREF(loop_descrs[1]);
3986 return -1;
3987 }
3988
3989 return NPY_UNSAFE_CASTING;
3990}
3991
3992static int
3993datetime_to_string_get_loop(

Callers

nothing calls this directly

Tested by

no test coverage detected