Formatted print for Duration

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Formatted print for Duration

Julien Delplanque-2
Hello,

I do not see how to do a formatted print from a duration.

I would like to be able to do something like:

String streamContents: [ :s | 90 minutes formatted: ‘hh:mm’ printOn: s ]

The Duration>>#printOn: method prints the ANSI 5.8.2.16 format: [-]D:HH:MM:SS[.S]

The Duration>>#printHumanReadableOn: is not printing in the format I want.

Am I missing something or such feature does not exist actually?

Julien

---
Julien Delplanque
Doctorant à l’Université de Lille 1
Bâtiment B 40, avenue Halley 59650 Villeneuve d'Ascq
Numéro de téléphone: +333 59 35 86 40

Reply | Threaded
Open this post in threaded view
|

Re: Formatted print for Duration

Andreas Brodbeck-3
Am 03.01.18 um 14:07 schrieb Julien:

> Hello,
>
> I do not see how to do a formatted print from a duration.
>
> I would like to be able to do something like:
>
> String streamContents: [ :s | 90 minutes formatted: ‘hh:mm’ printOn: s ]
>
> The Duration>>#printOn: method prints the ANSI 5.8.2.16 format: [-]D:HH:MM:SS[.S]
>
> The Duration>>#printHumanReadableOn: is not printing in the format I want.
>
> Am I missing something or such feature does not exist actually?

Hi Julien

I did not find a ready made method for that, either. Probably because
there is no standard way to print a *Duration* as minutes and seconds,
because it is not obvious how to print it (Compared to instances of the
*Time* class).

My solution would be to implement your own printing method using the
GRPrinter from the Grease package (Installable through Catalog Browser
in Pharo 6):

*****************************

| hoursPrinter minutesPrinter printer |

"Note: you can not use 'duration minutes', since this will give you only
the partial minutes inside a day"
hoursPrinter := GRMappedPrinter
        block: [ :duration | duration asMinutes // 60 ]
        next: (GRNumberPrinter new).

minutesPrinter := GRMappedPrinter
        block: [ :duration | duration minutes ]
        next: (GRPrinter numberWithAtLeastDigits: 2).

printer := hoursPrinter, $:, minutesPrinter.
               
^printer print: (9876 minutes) "Will result in '164:36'"

*****************************



2. solution: Or just dead simple:

*****************************

| duration |
duration := (9876 minutes).

^(duration asMinutes // 60) asString, ':',
duration minutes asTwoCharacterString

*****************************


Cheers, Andreas

--
Andreas Brodbeck
www.mindclue.ch


Reply | Threaded
Open this post in threaded view
|

Re: Formatted print for Duration

Julien Delplanque-2
I chosen something like the second solution to avoid adding a dependency to my project (it is a bit overkill for what I wanted to achieve). But shouldn’t this formatted-print feature be added in Duration?

Julien

---
Julien Delplanque
Doctorant à l’Université de Lille 1
Bâtiment B 40, avenue Halley 59650 Villeneuve d'Ascq
Numéro de téléphone: +333 59 35 86 40

Le 3 janv. 2018 à 23:02, Andreas Brodbeck <[hidden email]> a écrit :

Am 03.01.18 um 14:07 schrieb Julien:
Hello,

I do not see how to do a formatted print from a duration.

I would like to be able to do something like:

String streamContents: [ :s | 90 minutes formatted: ‘hh:mm’ printOn: s ]

The Duration>>#printOn: method prints the ANSI 5.8.2.16 format: [-]D:HH:MM:SS[.S]

The Duration>>#printHumanReadableOn: is not printing in the format I want.

Am I missing something or such feature does not exist actually?

Hi Julien

I did not find a ready made method for that, either. Probably because
there is no standard way to print a *Duration* as minutes and seconds,
because it is not obvious how to print it (Compared to instances of the
*Time* class).

My solution would be to implement your own printing method using the
GRPrinter from the Grease package (Installable through Catalog Browser
in Pharo 6):

*****************************

| hoursPrinter minutesPrinter printer |

"Note: you can not use 'duration minutes', since this will give you only
the partial minutes inside a day"
hoursPrinter := GRMappedPrinter
block: [ :duration | duration asMinutes // 60 ]
next: (GRNumberPrinter new).

minutesPrinter := GRMappedPrinter
block: [ :duration | duration minutes ]
next: (GRPrinter numberWithAtLeastDigits: 2).

printer := hoursPrinter, $:, minutesPrinter.

^printer print: (9876 minutes) "Will result in '164:36'"

*****************************



2. solution: Or just dead simple:

*****************************

| duration |
duration := (9876 minutes).

^(duration asMinutes // 60) asString, ':',
duration minutes asTwoCharacterString

*****************************


Cheers, Andreas

-- 
Andreas Brodbeck
www.mindclue.ch

Reply | Threaded
Open this post in threaded view
|

Re: Formatted print for Duration

Stephane Ducasse-3
Hi julien

Yes it would be nice :)

Stef

On Thu, Jan 4, 2018 at 2:28 PM, Julien <[hidden email]> wrote:

> I chosen something like the second solution to avoid adding a dependency to
> my project (it is a bit overkill for what I wanted to achieve). But
> shouldn’t this formatted-print feature be added in Duration?
>
> Julien
>
> ---
> Julien Delplanque
> Doctorant à l’Université de Lille 1
> http://juliendelplanque.be/phd.html
> Equipe Rmod, Inria
> Bâtiment B 40, avenue Halley 59650 Villeneuve d'Ascq
> Numéro de téléphone: +333 59 35 86 40
>
> Le 3 janv. 2018 à 23:02, Andreas Brodbeck <[hidden email]> a écrit :
>
> Am 03.01.18 um 14:07 schrieb Julien:
>
> Hello,
>
> I do not see how to do a formatted print from a duration.
>
> I would like to be able to do something like:
>
> String streamContents: [ :s | 90 minutes formatted: ‘hh:mm’ printOn: s ]
>
> The Duration>>#printOn: method prints the ANSI 5.8.2.16 format:
> [-]D:HH:MM:SS[.S]
>
> The Duration>>#printHumanReadableOn: is not printing in the format I want.
>
> Am I missing something or such feature does not exist actually?
>
>
> Hi Julien
>
> I did not find a ready made method for that, either. Probably because
> there is no standard way to print a *Duration* as minutes and seconds,
> because it is not obvious how to print it (Compared to instances of the
> *Time* class).
>
> My solution would be to implement your own printing method using the
> GRPrinter from the Grease package (Installable through Catalog Browser
> in Pharo 6):
>
> *****************************
>
> | hoursPrinter minutesPrinter printer |
>
> "Note: you can not use 'duration minutes', since this will give you only
> the partial minutes inside a day"
> hoursPrinter := GRMappedPrinter
> block: [ :duration | duration asMinutes // 60 ]
> next: (GRNumberPrinter new).
>
> minutesPrinter := GRMappedPrinter
> block: [ :duration | duration minutes ]
> next: (GRPrinter numberWithAtLeastDigits: 2).
>
> printer := hoursPrinter, $:, minutesPrinter.
>
> ^printer print: (9876 minutes) "Will result in '164:36'"
>
> *****************************
>
>
>
> 2. solution: Or just dead simple:
>
> *****************************
>
> | duration |
> duration := (9876 minutes).
>
> ^(duration asMinutes // 60) asString, ':',
> duration minutes asTwoCharacterString
>
> *****************************
>
>
> Cheers, Andreas
>
> --
> Andreas Brodbeck
> www.mindclue.ch
>
>