Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Format number of seconds to days, hours, minutes, seconds


  • Please log in to reply
32 replies to this topic
Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
Here is a mod of Titan's script, with toralf's parse:
GetFormatedTime(s) {

   w = day.hour.minute.second

   Loop Parse, w, .

   {

      x := 60**(4-A_Index) - (A_Index=1)*129600

      t := s // x

      s -= t * x

      If (t or A_Index = 4)

         m := m  t  " "  A_LoopField  Chr((t != 1) * 115)  " "

   }

   Return m

}


polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
Good job Laszlo, I just knew you'd come up with something. Thanks to toralf too :)
Apologies to PhiLho for hijacking this thread. I don't use this function now but I know it can be useful.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005

Apologies to PhiLho for hijacking this thread.

No problem, I like to post improvements on script too...
And these messages are right on the topic, so that's not really hijacking.

I like Laszlo's last version. I would remove the trailing space, but otherwise it is nice and quite understandable.
I still don't like much the Chr trick, but I suppose we have to live with such hacks until Chris implements ? : operator.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
v.1.0.46.00 is here...
GetFormatedTime(_seconds)
{
	local x, t, ft
	static units

	If (_seconds = 0)
		Return "0 seconds"
	units = day.hour.minute.second
	Loop Parse, units, .
	{
		x := A_Index = 1 ? 24 * 3600 : 60**(4 - A_Index)
		t := _seconds // x
		_seconds -= t * x
		If (t != 0)
			ft .= t . " " . A_LoopField . (t = 1 ? "" : "s") . (A_Index = 4 ? "" : " ")
	}
	Return ft
}

Test:

MsgBox %
( Join
 GetFormatedTime(11001001) . "`n" .
 GetFormatedTime(1009920) . "`n" .
 GetFormatedTime(100808) . "`n" .
 GetFormatedTime(11001) . "`n" .
 GetFormatedTime(3661) . "`n" .
 GetFormatedTime(1001) . "`n" .
 GetFormatedTime(100) . "`n" .
 GetFormatedTime(10) . "`n" .
 GetFormatedTime(1) . "`n" .
 GetFormatedTime(0) . "`n" .
 ""
)

Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

v.1.0.46.00 is here...

Sure is ...
gft(s) {

	w = day.hour.minute.second

	loop parse, w, .

		s -= (t := s // (x := 60 ** (e := 4 - a_index) - 129600 * (e = 3))) * x

		, t ? m .= t . " " . a_loopfield . chr(115 * (t != 1)) . chr(32 * !!e)

	return m ? m : "0 seconds"

}

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Hum, the , on start of line is a bit artificial, you could have omitted the comma... I suppose that's to demonstrate you could have put all on the same line...
Still using * (e = 3) and chr(115 * (t != 1)), I though the interest of the ternary operator was to avoid those tricks...
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
This is risky: it relies on the left-to-right order of the evaluation of the terms in the expression. It might work now, but if AHK ever changes this order, or an optimizer evaluates sub-expressions parallel, the result will be wrong. Sure, you can save a few characters this way, but this style is what you have to avoid.

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Even the chr(115 * (t != 1)) expression is risky, relying on a given internal boolean encoding. It is unlikely, but if Chris ever choose to change this, it is broken.

Note I wrote some months ago that obfuscated code is nearly impossible in AutoHotkey. Titan proves brilliantly that the latest changes in the language made a big step toward this... :evil:
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

This is risky: it relies on the left-to-right order of the evaluation of the terms in the expression.

Even the chr(115 * (t != 1)) expression is risky, relying on a given internal boolean encoding.

I did stress 'v.1.0.46.00' :p

Note I wrote some months ago that obfuscated code is nearly impossible in AutoHotkey. Titan proves brilliantly that the latest changes in the language made a big step toward this... :evil:

I believe a language should be flexible to allow users their own personal style of coding - especially AutoHotkey which appeals to non-programmers with no interest to learn rigid syntax/parsing rules. Even in human languages there are regional dialects and slang, it proves we're not all the same. Besides, so long as we have Tidy I don't think it's a problem, and if you're worried about me, I never normally write scripts in that fashion anyway.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Yes, I know you are doing this as a challenge, the devil wasn't for you, but because I don't find these changes absolutely necessary.

AutoHotkey [...] appeals to non-programmers with no interest to learn rigid syntax/parsing rules

I can be wrong, but I believe that rigidity can help non-programmers: they don't have to choose between alternative syntaxes, they are forced to write semi-clean programs, etc.

Now, I was just teasing you, not criticizing! :-) Hey, at least, you add spaces around the operators... ;-)
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

jonny
  • Members
  • 2951 posts
  • Last active: Feb 24 2008 04:22 AM
  • Joined: 13 Nov 2004

I can be wrong, but I believe that rigidity can help non-programmers: they don't have to choose between alternative syntaxes, they are forced to write semi-clean programs, etc.


I tend to disagree. Having multiple ways to do one thing makes it simpler, because you can do whatever works first, rather than having to find the one clean way. It's not like you line up all possible methods and then choose which one you'd like to use... you pursue a line of thought to its conclusion, using whatever method presents itself.

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005

Having multiple ways to do one thing makes it simpler, because you can do whatever works first, rather than having to find the one clean way.

You may end up with ugly code, which is hard to maintain, debug. You pay dearly later for not doing it "right" in the first place. (The syntax should make the clean choices the most obvious, and avoid complications, like operators behaving differently in different positions.)

Of course, there are always many ways to do things. Some are better than others. We ought to encourage clean styles over saving a few characters - in the expense of making the code harder to maintain or comprehend. At least for beginners. Code gurus will always take the challenge to minimize the length of the code, in some sense (number of lines, characters, instructions etc).

SoggyDog
  • Members
  • 803 posts
  • Last active: Mar 04 2013 06:27 AM
  • Joined: 02 May 2006

...You pay dearly later for not doing it "right" in the first place.


I honestly haven't really followed this thread so I'm speaking out of context, but I have to agree. When I was first learning to code PHP for MySQL I just did what worked, no matter how ugly it was... I just needed it to work and it did. Now that I have a better sense of what I'm doing, I have gone back to old websites that I built and I can't make heads or tails out of the spaghetti code that I used back then.

jonny
  • Members
  • 2951 posts
  • Last active: Feb 24 2008 04:22 AM
  • Joined: 13 Nov 2004
Well, this isn't really an issue from this thread... and this is not about spaghetti code at all, the choice of words is just making it sound like that. See this topic.

SoggyDog
  • Members
  • 803 posts
  • Last active: Mar 04 2013 06:27 AM
  • Joined: 02 May 2006

Well, this isn't really an issue from this thread... and this is not about spaghetti code at all, the choice of words is just making it sound like that. See this topic.

I did preface by saying that my comments were taking the thread out of it's context... and perhaps I should choose my words more carefully.

In the end, I'm really just trying to add a little to both sides of what I'm reading here. I agree that the flexibility of doing things a number of different ways can be helpful for getting a job done, but I also see the practicality of learning to do things the "right" way.