| 332 | } |
| 333 | |
| 334 | Result<std::string> PyTZInfo_utcoffset_hhmm(PyObject* pytzinfo) { |
| 335 | // attempt to convert timezone offset objects to "+/-{hh}:{mm}" format |
| 336 | OwnedRef pydelta_object(PyObject_CallMethod(pytzinfo, "utcoffset", "O", Py_None)); |
| 337 | RETURN_IF_PYERROR(); |
| 338 | |
| 339 | if (!PyDelta_Check(pydelta_object.obj())) { |
| 340 | return Status::Invalid( |
| 341 | "Object returned by tzinfo.utcoffset(None) is not an instance of " |
| 342 | "datetime.timedelta"); |
| 343 | } |
| 344 | auto pydelta = reinterpret_cast<PyDateTime_Delta*>(pydelta_object.obj()); |
| 345 | |
| 346 | // retrieve the offset as seconds |
| 347 | auto total_seconds = internal::PyDelta_to_s(pydelta); |
| 348 | |
| 349 | // determine whether the offset is positive or negative |
| 350 | auto sign = (total_seconds < 0) ? "-" : "+"; |
| 351 | total_seconds = abs(total_seconds); |
| 352 | |
| 353 | // calculate offset components |
| 354 | int64_t hours, minutes, seconds; |
| 355 | seconds = split_time(total_seconds, 60, &minutes); |
| 356 | minutes = split_time(minutes, 60, &hours); |
| 357 | if (seconds > 0) { |
| 358 | // check there are no remaining seconds |
| 359 | return Status::Invalid("Offset must represent whole number of minutes"); |
| 360 | } |
| 361 | |
| 362 | // construct the timezone string |
| 363 | std::stringstream stream; |
| 364 | stream << sign << std::setfill('0') << std::setw(2) << hours << ":" << std::setfill('0') |
| 365 | << std::setw(2) << minutes; |
| 366 | return stream.str(); |
| 367 | } |
| 368 | |
| 369 | // Converted from python. See https://github.com/apache/arrow/pull/7604 |
| 370 | // for details. |
no test coverage detected