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 via email to