Seconds to String

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Seconds to String

30 Dec 2013, 10:00

Seconds to String

This function will return the duration of the given time period in days, hours, minutes and seconds.
e.g. secsToStr(1234567) would return "14 days, 6 hours, 56 minutes, 7 seconds"

Code: Select all

secsToStr(s)
{
    if (s >= 86400)
    {
        d := floor(s / 86400), s := mod(s, 86400)
        r := d " day" ((d <> 1) ? "s" : "") ((s > 0) ? ", " : "")
    }
    if (s >= 3600)
    {
        h := floor(s / 3600), s := mod(s, 3600)
        r .= h " hour" ((h <> 1) ? "s" : "") ((s > 0) ? ", " : "")
    }
    if (s >= 60)
    {
        m := floor(s / 60), s := mod(s, 60)
        r .= m " minute" ((m <> 1) ? "s" : "") ((s > 0) ? ", " : "")
    }
    r .= s " second" ((s <> 1) ? "s" : "")

    return, % r
}

MsgBox, % secsToStr(1234567)
Last edited by jNizM on 09 Jan 2014, 06:32, edited 2 times in total.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Seconds to String

30 Dec 2013, 14:12

Just another method. :) (Slower than yours.)

Code: Select all

FormatSecondsToStr(s)
{
	static Unit := ["day", "hour", "minute", "second"]

	time := 19990101
	time += %s%, seconds
	FormatTime, ms, %time%, m#s
	d_h_m_s := s//3600//24 "#" Mod(s//3600, 24) "#" ms

	for i, v in StrSplit(d_h_m_s, "#")
		str .= v ? ( v " " Unit[i] (v>1 ? "s, " : ", ") ) : ""

	Return RTrim(str, ", ")
}
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: Seconds to String

30 Dec 2013, 16:04

very "classy" with no classes! :lol: :ugeek:
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Seconds to String

09 Jan 2014, 06:33

update:
- shorter code
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
joedf
Posts: 8960
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Seconds to String

09 Jan 2014, 12:53

Guest10 wrote:very "classy" with no classes! :lol: :ugeek:
Haha yeah... 8-)
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
User avatar
jballi
Posts: 724
Joined: 29 Sep 2013, 17:34

Re: Seconds to String

09 Jan 2014, 20:17

Beans of the cool variety. :)
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: Seconds to String

09 Jan 2014, 21:56

this pil·fered from the old forum :lol: ... however, why i always get today's date? :? :ugeek:

Code: Select all

msgbox % DateParse(Dec-31-13)

; http://www.autohotkey.com/board/topic/18760-date-parser-convert-any-date-format-to-yyyymmddhh24miss/page-5
DateParse(str, americanOrder=1) {
	static monthNames := "(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\w*"
		, dayAndMonth := "(?:(\d{1,2}|" . monthNames . ")[\s\.\-\/,]+)?(\d{1,2}|" . monthNames . ")"
	If RegExMatch(str, "i)^\s*(?:(\d{4})([\s\-:\/])(\d{1,2})\2(\d{1,2}))?"
		. "(?:\s*[T\s](\d{1,2})([\s\-:\/])(\d{1,2})(?:\6(\d{1,2})\s*(?:(Z)|(\+|\-)?"
		. "(\d{1,2})\6(\d{1,2})(?:\6(\d{1,2}))?)?)?)?\s*$", i) ;ISO 8601 timestamps
		year := i1, month := i3, day := i4, t1 := i5, t2 := i7, t3 := i8
	Else If !RegExMatch(str, "^\W*(\d{1,2}+)(\d{2})\W*$", t){
		RegExMatch(str, "i)(\d{1,2})"					;hours
				. "\s*:\s*(\d{1,2})"				;minutes
				. "(?:\s*:\s*(\d{1,2}))?"			;seconds
				. "(?:\s*([ap]m))?", t)				;am/pm
		StringReplace, str, str, %t%
		If Regexmatch(str, "i)(\d{4})[\s\.\-\/,]+" . dayAndMonth, d) ;2004/22/03
			year := d1, month := d3, day := d2
		Else If Regexmatch(str, "i)" . dayAndMonth . "[\s\.\-\/,]+(\d{2,4})", d)  ;22/03/2004 or 22/03/04
			year := d3, month := d2, day := d1
		If (RegExMatch(day, monthNames) or americanOrder and !RegExMatch(month, monthNames)) ;try to infer 

day/month order
			tmp := month, month := day, day := tmp
	}
	f = %A_FormatFloat%
	SetFormat, Float, 02.0
	d := (StrLen(year) == 2 ? "20" . year : (year ? year : A_YYYY))
		. ((month := month + 0 ? month : InStr(monthNames, SubStr(month, 1, 3)) // 4 ) > 0 ? month + 0.0 : A_MM)
		. ((day += 0.0) ? day : A_DD) 
		. t1 + (t1 == 12 ? t4 = "am" ? -12.0 : 0.0 : t4 = "pm" ? 12.0 : 0.0)
		. t2 + 0.0 . t3 + 0.0
	SetFormat, Float, %f%
	return, d
}
User avatar
smorgasbord
Posts: 493
Joined: 30 Sep 2013, 09:34

Re: Seconds to String

24 May 2014, 08:11

i am getting bored.
can it be done in 1 line?
John ... you working ?
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

Re: Seconds to String

24 May 2014, 08:19

@Guest10
msgbox % DateParse("Dec-31-13") surround with quotes :-)
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Seconds to String

24 May 2014, 12:19

smorgasbord wrote:i am getting bored.
can it be done in 1 line?

Code: Select all

secsToStr(s){
	return (s>86400?(s//86400) " Days, ":"")(s>3600?(s:=mod(s,86400))//3600 " Hours, ":"")(s>60?(s:=mod(s,3600))//60 " Minutes, ":"") mod(s,60) " Seconds"
}
User avatar
smorgasbord
Posts: 493
Joined: 30 Sep 2013, 09:34

Re: Seconds to String

24 May 2014, 12:55

AlphaBravo wrote:
smorgasbord wrote:i am getting bored.
can it be done in 1 line?

Code: Select all

secsToStr(s){
	return (s>86400?(s//86400) " Days, ":"")(s>3600?(s:=mod(s,86400))//3600 " Hours, ":"")(s>60?(s:=mod(s,3600))//60 " Minutes, ":"") mod(s,60) " Seconds"
}
:shock: :shock:
:o :o
:( :(
:x :x
:evil: :evil:
:mrgreen: :mrgreen:
John ... you working ?
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: Seconds to String

24 May 2014, 18:02

Coco wrote:@Guest10
msgbox % DateParse("Dec-31-13") surround with quotes :-)
thanks, tested and it was a hit! :lol:
Last edited by Guest10 on 25 May 2014, 00:20, edited 1 time in total.
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: Seconds to String

24 May 2014, 18:06

AlphaBravo wrote:
smorgasbord wrote:i am getting bored.
can it be done in 1 line?

Code: Select all

secsToStr(s){
	return (s>86400?(s//86400) " Days, ":"")(s>3600?(s:=mod(s,86400))//3600 " Hours, ":"")(s>60?(s:=mod(s,3600))//60 " Minutes, ":"") mod(s,60) " Seconds"
}
i'm sure this will be a hit! any examples to test? :lol:
garry
Posts: 3770
Joined: 22 Dec 2013, 12:50

Re: Seconds to String

25 May 2014, 05:21

example, AlphaBravo, works fine , maybe days missing ( if exact or below 1 day )

Code: Select all

s:=86462    ;- ok = 1 days 0 hours 1 minutes 2 seconds
;s:=86400   ;- should be 1 day , missing message 1 day
;s:=86399   ;- ok maybe add 0 days
aaa:= % secsToStr(s)
msgbox,%aaa%
return

secsToStr(s){
    return (s>86400?(s//86400) " Days, ":"")(s>3600?(s:=mod(s,86400))//3600 " Hours, ":"")(s>60?(s:=mod(s,3600))//60 " Minutes, ":"") mod(s,60) " Seconds"
}
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Seconds to String

25 May 2014, 10:38

@garry, thanks for the examples, this should be easy to modify to show "0 days" if desired

Code: Select all

list =
(comment
86462	;- ok = 1 days 0 hours 1 minutes 2 seconds
86400	;- should be 1 day , missing message 1 day
86399	;- ok maybe add 0 days
)

loop, parse, list, `n
	res .= secsToStr(A_LoopField) "`n"
MsgBox % res
return

secsToStr(s){
    return (s>=86400?(s//86400)" Days, ":"")(s>=3600?(s:=mod(s,86400))//3600 :0)" Hours, "(s>=60?(s:=mod(s,3600))//60 : 0)" Minutes, "mod(s,60) " Seconds"
}
Edit: I merely shrunk the original post, credit goes to jNizM
garry
Posts: 3770
Joined: 22 Dec 2013, 12:50

Re: Seconds to String

25 May 2014, 11:28

thank you AlphaBravo , jNizM , tmplinshi , Guest10 ... for help and scripts , works fine
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: Seconds to String

26 May 2014, 12:51

could you post a COMPREHENSIVE script of this kind that converts any date format in the following LIST to 2 string types: (1) YYYYMMDD and (2) YYYYMMDDhhmmss:? :ugeek:

YYMMDD
YYYYYMMDD
YY/MM/DD
YYYYY/MM/DD
MMDDYY
MMDDYYYYY
MM/DD/YY
MM/DD/YYYY
DDMMYY
DDMMYYYYY
DD/MM/YY
DD/MM/YYYY
User avatar
atnbueno
Posts: 89
Joined: 12 Oct 2013, 04:45
Contact:

Re: Seconds to String

26 May 2014, 15:51

Guest10, there are too many ambiguities if you want to autodetect the format. For example "101214" could be either DDMMYY, MMDDYY, or YYMMDD.

Or did you mean something else?
User avatar
joedf
Posts: 8960
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Seconds to String

26 May 2014, 21:36

@AlphaBravo Nice there! But I know that it can be even shorter, and you can do it... ;)
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: Seconds to String

27 May 2014, 13:47

atnbueno wrote:Guest10, there are too many ambiguities if you want to autodetect the format. For example "101214" could be either DDMMYY, MMDDYY, or YYMMDD.

Or did you mean something else?
doh!!! thanks for mentioning the conflict. i have the following in my archives:

Code: Select all

; http://www.autohotkey.com/board/topic/18760-date-parser-convert-any-date-format-to-yyyymmddhh24miss/

; Date Parser: Converts any date format to YYYYMMDDHH24MISS

/*
	Function: DateParse
		Converts almost any date format to a YYYYMMDDHH24MISS value.

	Parameters:
		str - a date/time stamp as a string

	Returns:
		A valid YYYYMMDDHH24MISS value which can be used by FormatTime, EnvAdd and other time commands.

	Example:
		time := DateParse("2:35 PM, 27 November, 2007")

	License:
		- Version 1.05 <http://www.autohotkey.net/~polyethene/#dateparse>
		- Dedicated to the public domain (CC0 1.0) <http://creativecommons.org/publicdomain/zero/1.0/>
*/

; Function:

DateParse(str) {
	static e2 = "i)(?:(\d{1,2}+)[\s\.\-\/,]+)?(\d{1,2}|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\w*)[\s\.\-\/,]+(\d{2,4})"
	str := RegExReplace(str, "((?:" . SubStr(e2, 42, 47) . ")\w*)(\s*)(\d{1,2})\b", "$3$2$1", "", 1)
	If RegExMatch(str, "i)^\s*(?:(\d{4})([\s\-:\/])(\d{1,2})\2(\d{1,2}))?"
		. "(?:\s*[T\s](\d{1,2})([\s\-:\/])(\d{1,2})(?:\6(\d{1,2})\s*(?:(Z)|(\+|\-)?"
		. "(\d{1,2})\6(\d{1,2})(?:\6(\d{1,2}))?)?)?)?\s*$", i)
		d3 := i1, d2 := i3, d1 := i4, t1 := i5, t2 := i7, t3 := i8
	Else If !RegExMatch(str, "^\W*(\d{1,2}+)(\d{2})\W*$", t)
		RegExMatch(str, "i)(\d{1,2})\s*:\s*(\d{1,2})(?:\s*(\d{1,2}))?(?:\s*([ap]m))?", t)
			, RegExMatch(str, e2, d)
	f = %A_FormatFloat%
	SetFormat, Float, 02.0
	d := (d3 ? (StrLen(d3) = 2 ? 20 : "") . d3 : A_YYYY)
		. ((d2 := d2 + 0 ? d2 : (InStr(e2, SubStr(d2, 1, 3)) - 40) // 4 + 1.0) > 0
			? d2 + 0.0 : A_MM) . ((d1 += 0.0) ? d1 : A_DD) . t1
			+ (t1 = 12 ? t4 = "am" ? -12.0 : 0.0 : t4 = "am" ? 0.0 : 12.0) . t2 + 0.0 . t3 + 0.0
	SetFormat, Float, %f%
	Return, d
}

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: gwarble and 164 guests