Saturday, April 29, 2017

#26 2017-04-16 07:11:02 am

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 4263

Re: Seconds to hours minutes and seconds

Nice.  smile  How about …

Applescript:

secondsToHMS from 5240 --> "01:27:20"

on secondsToHMS from theSeconds
   tell theSeconds to tell (it div 36000 as text) & (100000 + it div hours mod 10 * 10000 + it mod hours div minutes * 100 + it mod minutes as integer) to return text 1 thru -7 & character -5 & ":" & text -4 thru -3 & ":" & text -2 thru -1
end secondsToHMS


NG

Offline

 

#27 2017-04-16 10:16:16 pm

bmose
Member
From: Massachusetts
Registered: 2006-01-02
Posts: 190

Re: Seconds to hours minutes and seconds

Nigel, your one-liner executes 2.5x faster than mine in a speed test.  Sweet!

Last edited by bmose (2017-04-16 11:17:09 pm)

Offline

 

#28 2017-04-17 01:36:26 am

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 4998

Re: Seconds to hours minutes and seconds

And these days we can actually fulfil Kel's original request easily enough:

Applescript:

use AppleScript version "2.5" -- 10.11 or later to use AppleScript dates
use framework "Foundation"
use scripting additions

set startDate to current date
set endDate to startDate + 5240 -- whatever

set df to current application's NSDateComponentsFormatter's new()
df's setAllowedUnits:((current application's NSCalendarUnitHour) + (get current application's NSCalendarUnitMinute) + (get current application's NSCalendarUnitSecond))
df's setUnitsStyle:(current application's NSDateComponentsFormatterUnitsStyleFull)
(df's stringFromDate:startDate toDate:endDate) as text

Yvan should be happy with that smile


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#29 2017-04-17 04:36:39 am

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 4263

Re: Seconds to hours minutes and seconds

bmose wrote:

Nigel, your one-liner executes 2.5x faster than mine in a speed test.  Sweet!

I thought mine might be slightly faster, but based on theoretical considerations rather than side-by-side speed tests:

BM                               NG
10 integer math operations       11 integer math operations
6 implicit coercions to text     2 coercions to text (1 explicit, 1 implicit)
8 text concatentations           6 text concatenations
1 list creation                  1 explicit number-to-integer coercion
6 list accesses                  4 extractions from text

There'd be little to chose between them in a real-life situation.  smile


NG

Offline

 

#30 2017-04-17 04:39:05 am

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 4263

Re: Seconds to hours minutes and seconds

Shane Stanley wrote:

And these days we can actually fulfil Kel's original request easily enough:

I was going to criticise the script for not returning the result in the same format, but having read kel's opening monologue several times, I can't see anything specifying either a format or three separate figures.  hmm

Interesting script, as always.


NG

Offline

 

#31 2017-04-17 06:38:43 am

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 4998

Re: Seconds to hours minutes and seconds

Nigel Garvey wrote:

I was going to criticise the script for not returning the result in the same format

And I was expecting that wink But in trying to match that format, I hit a bug. As I read the docs, this should do it:

Applescript:

use AppleScript version "2.5" -- 10.11 or later to use AppleScript dates
use framework "Foundation"
use scripting additions

set startDate to current date
set endDate to startDate + 5240 -- whatever

set df to current application's NSDateComponentsFormatter's new()
df's setAllowedUnits:((current application's NSCalendarUnitHour) + (get current application's NSCalendarUnitMinute) + (get current application's NSCalendarUnitSecond))
df's setZeroFormattingBehavior:(current application's NSDateComponentsFormatterZeroFormattingBehaviorPad)
(df's stringFromDate:startDate toDate:endDate) as text

The .h file for NSDateComponentsFormatter says, in part:

NSDateComponentsFormatterZeroFormattingBehaviorPad = (1 << 16), // Off: "1:0:10", On: "01:00:10"

But in fact it doesn't pad the leading value, and instead returns "1:27:20". (It pads the other values fine.) And that's when I went back to see exactly what was asked for...


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#32 2017-04-17 08:36:49 am

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 4263

Re: Seconds to hours minutes and seconds

Shane Stanley wrote:

The .h file for NSDateComponentsFormatter says, in part:

NSDateComponentsFormatterZeroFormattingBehaviorPad = (1 << 16), // Off: "1:0:10", On: "01:00:10"

But in fact it doesn't pad the leading value, and instead returns "1:27:20". (It pads the other values fine.) And that's when I went back to see exactly what was asked for...

Hmm. The observed behaviour's what described in the Xcode documentation.  Seems a bit limited. hmm

Applescript:

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

secondsToHMS from 5240 -- whatever

on secondsToHMS from theSeconds
   set |⌘| to current application
   set df to |⌘|'s class "NSDateComponentsFormatter"'s new()
   tell df to setAllowedUnits:((|⌘|'s NSCalendarUnitHour) + (get |⌘|'s NSCalendarUnitMinute) + (get |⌘|'s NSCalendarUnitSecond))
   tell df to setZeroFormattingBehavior:(current application's NSDateComponentsFormatterZeroFormattingBehaviorPad)
   return ((df's stringFromTimeInterval:(theSeconds))'s stringByReplacingOccurrencesOfString:("^(?=[0-9]:)") withString:("0") options:(|⌘|'s NSRegularExpressionSearch) range:({0, 2})) as text
end secondsToHMS

Last edited by Nigel Garvey (2017-04-17 08:48:30 am)


NG

Offline

 

#33 2017-04-18 04:20:40 am

bmose
Member
From: Massachusetts
Registered: 2006-01-02
Posts: 190

Re: Seconds to hours minutes and seconds

Apologies for prolonging this thread, but here is one more one-line version of Stefan's solution for those looking for a pure Applescript solution that perhaps wins the simplicity contest (but stills comes in second place to Nigel's in terms of execution speed, which is about 1.4 x faster):

Applescript:

secondsToHMS from 5240 --> "01:27:20"

on secondsToHMS from theSeconds
   tell theSeconds to tell {"0" & it div hours, "0" & it mod hours div minutes, "0" & it mod minutes} to return item 1's text -2 thru -1 & ":" & item 2's text -2 thru -1 & ":" & item 3's text -2 thru -1
end secondsToHMS

Last edited by bmose (2017-04-18 04:25:53 am)

Offline

 

#34 2017-04-18 05:16:21 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 2957

Re: Seconds to hours minutes and seconds

I just open this thread today.
I was wondering what meant "Yvan should be happy with that" in message #28
I understood when I ran the script displaying :"1 heure, 27 minutes et 20 secondes"

I would not guessed because there is no reference to locale in the code.

I will put it in my library although I never use this format.

I would use the one in message #31.

The one posted by Nigel in message #32 gives the same result but I'm not fond of RegularExpression wink roll


Yvan KOENIG running Sierra 10.12.4 in French (VALLAURIS, France) mardi 18 avril 2017 12:16:15

Offline

 

#35 2017-04-18 09:51:09 am

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 4263

Re: Seconds to hours minutes and seconds

Here's a slightly faster one, but it's only good up to 359,999,999 seconds (595 weeks 1 days 15 hours 59 minutes 59 seconds) in El Capitan:

Applescript:

secondsToHMS from 0 --> "00:00:00"
secondsToHMS from 5240 --> "01:27:20"
secondsToHMS from 595 * weeks + 1 * days + 15 * hours + 59 * minutes + 59 --> "99999:59:59"

on secondsToHMS from theSeconds
   tell theSeconds to tell (it div hours / 10 + it mod hours div minutes / 10000 + it mod minutes / 1000000 + 0.0100004) as text to return text 1 thru -9 & character -7 & ":" & text -5 thru -4 & ":" & text -3 thru -2
end secondsToHMS


NG

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.049 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)