Title: How to convert Period or Duration to proper string · python-pendulum/pendulum · Discussion #675 · GitHub
Open Graph Title: How to convert Period or Duration to proper string · python-pendulum/pendulum · Discussion #675
X Title: How to convert Period or Duration to proper string · python-pendulum/pendulum · Discussion #675
Description: How to convert Period or Duration to proper string
Open Graph Description: I want some kind of Duration with 6 minutes and 58 seconds to look like this when casted to str "0:06:58" Is there a builtin function for formatting Durations or I gotta do it by myself
X Description: I want some kind of Duration with 6 minutes and 58 seconds to look like this when casted to str "0:06:58" Is there a builtin function for formatting Durations or I gotta do it by myself
Opengraph URL: https://github.com/python-pendulum/pendulum/discussions/675
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"QAPage","mainEntity":{"@type":"Question","name":"How to convert Period or Duration to proper string","text":"\u003cp dir=\"auto\"\u003eI want some kind of Duration with 6 minutes and 58 seconds to look like this when casted to str\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-python notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"\u0026quot;0:06:58\u0026quot;\"\u003e\u003cpre class=\"notranslate\"\u003e\u003cspan class=\"pl-s\"\u003e\"0:06:58\"\u003c/span\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eIs there a builtin function for formatting Durations or I gotta do it by myself\u003c/p\u003e","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"\u003cp dir=\"auto\"\u003eFor that representation, convert the duration to a standard \u003ccode class=\"notranslate\"\u003edatetime.timedelta\u003c/code\u003e with \u003ccode class=\"notranslate\"\u003eas_timedelta()\u003c/code\u003e:\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-python notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"import pendulum\n\nduration = pendulum.duration(minutes=6, seconds=58)\nprint(str(duration.as_timedelta()))\n# 0:06:58\"\u003e\u003cpre class=\"notranslate\"\u003e\u003cspan class=\"pl-k\"\u003eimport\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ependulum\u003c/span\u003e\n\n\u003cspan class=\"pl-s1\"\u003eduration\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ependulum\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eduration\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eminutes\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003e6\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003eseconds\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003e58\u003c/span\u003e)\n\u003cspan class=\"pl-en\"\u003eprint\u003c/span\u003e(\u003cspan class=\"pl-en\"\u003estr\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eduration\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eas_timedelta\u003c/span\u003e()))\n\u003cspan class=\"pl-c\"\u003e# 0:06:58\u003c/span\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eI checked this with Pendulum 3.2.0. \u003ccode class=\"notranslate\"\u003estr(duration)\u003c/code\u003e gives \u003ccode class=\"notranslate\"\u003e6 minutes 58 seconds\u003c/code\u003e, while \u003ccode class=\"notranslate\"\u003estr(duration.as_timedelta())\u003c/code\u003e gives the exact representation in your question. \u003ca href=\"https://github.com/python-pendulum/pendulum/blob/3.2.0/src/pendulum/duration.py#L292-L299\"\u003eThe implementation\u003c/a\u003e exposes the conversion and uses \u003ccode class=\"notranslate\"\u003ein_words()\u003c/code\u003e for the duration's own string representation.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eThis follows standard \u003ccode class=\"notranslate\"\u003etimedelta\u003c/code\u003e formatting: a 27-hour duration displays \u003ccode class=\"notranslate\"\u003e1 day, 3:00:00\u003c/code\u003e, and fractional seconds are included when present. If you want total hours such as \u003ccode class=\"notranslate\"\u003e27:00:00\u003c/code\u003e, that would need a custom format.\u003c/p\u003e","upvoteCount":1,"url":"https://github.com/python-pendulum/pendulum/discussions/675#discussioncomment-18846523"}}}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/Voltron::DiscussionsFragmentsController/show/:user_id/:repository/:discussion_number/discussion_layout(.:format) |
| route-controller | voltron_discussions_fragments |
| route-action | discussion_layout |
| fetch-nonce | v2:cb46ea58-c252-ddf0-a24a-6690ab542716 |
| current-catalog-service-hash | 9f0abe34da433c9b6db74bffa2466494a717b579a96b30a5d252e5090baea7be |
| request-id | 826C:632D5:165E1A:1F4749:6B0465A8 |
| html-safe-nonce | d92edb131a7389ed0db594edfd957aa9d910720a4e895acf7f0ec3a96b91657c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MjZDOjYzMkQ1OjE2NUUxQToxRjQ3NDk6NkIwNDY1QTgiLCJ2aXNpdG9yX2lkIjoiNjEyMTAyMTI3MTMwOTU3NTU5MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 4665c538835a472666def8abbbe8150dd3747d8e22f6c942e194f568f7a6f245 |
| hovercard-subject-tag | discussion:4583784 |
| github-keyboard-shortcuts | repository,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/Voltron::DiscussionsFragmentsController/show/python-pendulum/pendulum/675/discussion_layout |
| twitter:image | https://opengraph.githubassets.com/634be4ef0501d761a88b65113e8512ff7d90098cefb86d9ae8d6c9021a73cdd1/python-pendulum/pendulum/discussions/675 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/634be4ef0501d761a88b65113e8512ff7d90098cefb86d9ae8d6c9021a73cdd1/python-pendulum/pendulum/discussions/675 |
| og:image:alt | I want some kind of Duration with 6 minutes and 58 seconds to look like this when casted to str "0:06:58" Is there a builtin function for formatting Durations or I gotta do it by myself |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 83e4c0e58c5162177f3c161c5fbad38527ae04c11ac14980f9f9d85ae2027b1f |
| turbo-cache-control | no-preview |
| go-import | github.com/python-pendulum/pendulum git https://github.com/python-pendulum/pendulum.git |
| octolytics-dimension-user_id | 191041900 |
| octolytics-dimension-user_login | python-pendulum |
| octolytics-dimension-repository_id | 62095504 |
| octolytics-dimension-repository_nwo | python-pendulum/pendulum |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 62095504 |
| octolytics-dimension-repository_network_root_nwo | python-pendulum/pendulum |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | ca4d07a7cef4e4b75f821dc25e7300d80c1450a9 |
| ui-target | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width