Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #109130 > unrolled thread
| Started by | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| First post | 2016-05-26 15:16 +1000 |
| Last post | 2016-05-26 08:43 +0300 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.python
Format a timedelta object Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-05-26 15:16 +1000
Re: Format a timedelta object Zachary Ware <zachary.ware+pylist@gmail.com> - 2016-05-26 00:28 -0500
Re: Format a timedelta object Steven D'Aprano <steve@pearwood.info> - 2016-05-28 02:21 +1000
Re: Format a timedelta object Zachary Ware <zachary.ware+pylist@gmail.com> - 2016-05-27 11:34 -0500
Re: Format a timedelta object Pete Forman <petef4+usenet@gmail.com> - 2016-05-27 21:41 +0100
Re: Format a timedelta object Marko Rauhamaa <marko@pacujo.net> - 2016-05-26 08:43 +0300
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2016-05-26 15:16 +1000 |
| Subject | Format a timedelta object |
| Message-ID | <574686cc$0$1584$c3e8da3$5496439d@news.astraweb.com> |
I have a timedelta object, and I want to display it in a nice human-readable format like 03:45:17 for "three hours, forty five minutes, 17 seconds". Is there a standard way to do this? -- Steve
[toc] | [next] | [standalone]
| From | Zachary Ware <zachary.ware+pylist@gmail.com> |
|---|---|
| Date | 2016-05-26 00:28 -0500 |
| Message-ID | <mailman.0.1464240542.2277.python-list@python.org> |
| In reply to | #109130 |
On Thu, May 26, 2016 at 12:16 AM, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote: > I have a timedelta object, and I want to display it in a nice human-readable > format like 03:45:17 for "three hours, forty five minutes, 17 seconds". > > Is there a standard way to do this? >>> timedelta(100) datetime.timedelta(100) >>> str(timedelta(seconds=100)) '0:01:40' >>> str(timedelta(hours=100)) '4 days, 4:00:00' (I recently spent *way* too long trying to figure out how to properly format the thing before being reminded that a plain str call gives exactly what I was after.) -- Zach
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2016-05-28 02:21 +1000 |
| Message-ID | <57487425$0$1597$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #109131 |
On Thu, 26 May 2016 03:28 pm, Zachary Ware wrote: > On Thu, May 26, 2016 at 12:16 AM, Steven D'Aprano > <steve+comp.lang.python@pearwood.info> wrote: >> I have a timedelta object, and I want to display it in a nice >> human-readable format like 03:45:17 for "three hours, forty five minutes, >> 17 seconds". >> >> Is there a standard way to do this? > > >>> timedelta(100) > datetime.timedelta(100) > >>> str(timedelta(seconds=100)) > '0:01:40' > >>> str(timedelta(hours=100)) > '4 days, 4:00:00' > > (I recently spent *way* too long trying to figure out how to properly > format the thing before being reminded that a plain str call gives > exactly what I was after.) Thanks Zach. Unfortunately, the format is not quite how I want it, so I guess I'll have to extract the H:M:S fields manually from the seconds. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Zachary Ware <zachary.ware+pylist@gmail.com> |
|---|---|
| Date | 2016-05-27 11:34 -0500 |
| Message-ID | <mailman.27.1464366899.2277.python-list@python.org> |
| In reply to | #109179 |
On Fri, May 27, 2016 at 11:21 AM, Steven D'Aprano <steve@pearwood.info> wrote: > On Thu, 26 May 2016 03:28 pm, Zachary Ware wrote: >> On Thu, May 26, 2016 at 12:16 AM, Steven D'Aprano >> <steve+comp.lang.python@pearwood.info> wrote: >>> I have a timedelta object, and I want to display it in a nice >>> human-readable format like 03:45:17 for "three hours, forty five minutes, >>> 17 seconds". >> >> >>> str(timedelta(seconds=100)) >> '0:01:40' > > Thanks Zach. Unfortunately, the format is not quite how I want it, so I > guess I'll have to extract the H:M:S fields manually from the seconds. What's missing the mark, no leading 0 on the hour, or the extra 0 hour specification? Yes, unfortunately it looks like manual extraction is your only option for that. I'd support an RFE for a strftime-like __format__ method on timedelta that only supported certain %-codes, though. I haven't checked to see if there's already an open issue. -- Zach
[toc] | [prev] | [next] | [standalone]
| From | Pete Forman <petef4+usenet@gmail.com> |
|---|---|
| Date | 2016-05-27 21:41 +0100 |
| Message-ID | <m17fefxmh2.fsf@iKarel.lan> |
| In reply to | #109179 |
Steven D'Aprano <steve@pearwood.info> writes: > On Thu, 26 May 2016 03:28 pm, Zachary Ware wrote: > >> On Thu, May 26, 2016 at 12:16 AM, Steven D'Aprano >> <steve+comp.lang.python@pearwood.info> wrote: >>> I have a timedelta object, and I want to display it in a nice >>> human-readable format like 03:45:17 for "three hours, forty five minutes, >>> 17 seconds". >>> >>> Is there a standard way to do this? >> >> >>> timedelta(100) >> datetime.timedelta(100) >> >>> str(timedelta(seconds=100)) >> '0:01:40' >> >>> str(timedelta(hours=100)) >> '4 days, 4:00:00' >> >> (I recently spent *way* too long trying to figure out how to properly >> format the thing before being reminded that a plain str call gives >> exactly what I was after.) > > Thanks Zach. Unfortunately, the format is not quite how I want it, so I > guess I'll have to extract the H:M:S fields manually from the seconds. It might be useful if timedelta were to get an isoformat() method. ISO 8601 specifies formats for durations; most people are familiar only with the date amd time formats. There are variations available but PnDTnHnMnS is probably the best. The biggest timedelta unit is days. Years and months are not appropriate. -- Pete Forman
[toc] | [prev] | [next] | [standalone]
| From | Marko Rauhamaa <marko@pacujo.net> |
|---|---|
| Date | 2016-05-26 08:43 +0300 |
| Message-ID | <874m9lz85v.fsf@elektro.pacujo.net> |
| In reply to | #109130 |
Steven D'Aprano <steve+comp.lang.python@pearwood.info>:
> I have a timedelta object, and I want to display it in a nice
> human-readable format like 03:45:17 for "three hours, forty five
> minutes, 17 seconds".
>
> Is there a standard way to do this?
>>> import datetime
>>> td = datetime.timedelta(hours=3, minutes=45, seconds=17)
>>> d = datetime.datetime(2000, 1, 1)
>>> (d + td).strftime("%T")
'03:45:17'
>>> "%02d:%02d:%02d" % (
... td.seconds // 3600, td.seconds % 3600 // 60, td.seconds % 60)
'03:45:17'
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web