MCPcopy Create free account
hub / github.com/apache/arrow / StringToTzinfo

Function StringToTzinfo

python/pyarrow/src/arrow/python/datetime.cc:371–479  ·  view source on GitHub ↗

Converted from python. See https://github.com/apache/arrow/pull/7604 for details.

Source from the content-addressed store, hash-verified

369// Converted from python. See https://github.com/apache/arrow/pull/7604
370// for details.
371Result<PyObject*> StringToTzinfo(const std::string& tz, bool prefer_zoneinfo) {
372 std::string_view sign_str, hour_str, minute_str;
373 OwnedRef pytz;
374 OwnedRef zoneinfo;
375 OwnedRef datetime;
376
377 // Legacy behavior: prefer pytz objects when available
378 if (!prefer_zoneinfo && internal::ImportModule("pytz", &pytz).ok()) {
379 if (MatchFixedOffset(tz, &sign_str, &hour_str, &minute_str)) {
380 int sign = -1;
381 if (sign_str == "+") {
382 sign = 1;
383 }
384 OwnedRef fixed_offset;
385 RETURN_NOT_OK(internal::ImportFromModule(pytz.obj(), "FixedOffset", &fixed_offset));
386 uint32_t minutes, hours;
387 if (!::arrow::internal::ParseUnsigned(hour_str.data(), hour_str.size(), &hours) ||
388 !::arrow::internal::ParseUnsigned(minute_str.data(), minute_str.size(),
389 &minutes)) {
390 return Status::Invalid("Invalid timezone: ", tz);
391 }
392 OwnedRef total_minutes(PyLong_FromLong(
393 sign * ((static_cast<int>(hours) * 60) + static_cast<int>(minutes))));
394 RETURN_IF_PYERROR();
395 auto tzinfo =
396 PyObject_CallFunctionObjArgs(fixed_offset.obj(), total_minutes.obj(), NULL);
397 RETURN_IF_PYERROR();
398 return tzinfo;
399 }
400
401 OwnedRef timezone;
402 RETURN_NOT_OK(internal::ImportFromModule(pytz.obj(), "timezone", &timezone));
403 OwnedRef py_tz_string(
404 PyUnicode_FromStringAndSize(tz.c_str(), static_cast<Py_ssize_t>(tz.size())));
405 auto tzinfo = PyObject_CallFunctionObjArgs(timezone.obj(), py_tz_string.obj(), NULL);
406 RETURN_IF_PYERROR();
407 return tzinfo;
408 }
409
410 // Handle fixed offsets with datetime.timezone
411 if (MatchFixedOffset(tz, &sign_str, &hour_str, &minute_str)) {
412 RETURN_NOT_OK(internal::ImportModule("datetime", &datetime));
413 int sign = -1;
414 if (sign_str == "+") {
415 sign = 1;
416 }
417
418 // import timezone and timedelta module to create a tzinfo object
419 OwnedRef class_timezone;
420 OwnedRef class_timedelta;
421 RETURN_NOT_OK(
422 internal::ImportFromModule(datetime.obj(), "timezone", &class_timezone));
423 RETURN_NOT_OK(
424 internal::ImportFromModule(datetime.obj(), "timedelta", &class_timedelta));
425
426 // check input
427 uint32_t minutes, hours;
428 if (!::arrow::internal::ParseUnsigned(hour_str.data(), hour_str.size(), &hours) ||

Callers 1

VisitMethod · 0.85

Calls 8

ImportModuleFunction · 0.85
MatchFixedOffsetFunction · 0.85
ImportFromModuleFunction · 0.85
ParseUnsignedFunction · 0.85
InvalidFunction · 0.50
okMethod · 0.45
dataMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected