Jump to content

Convert units of time


Recommended Posts

I'm feeling lazy. I would like someone to provide a UFD that does the following, if possible:

convertTime($quantity, $units_of_quantity, $largest_units_in_result)

Maybe we should also specify $smallest_units_in_result... Might also consider rounding...

convertTime(72, "minutes", "hours")
; would return "1 hour, 12 minutes"  or perhaps "1:12"

convertTime(4000, "seconds", "minutes")
; would return "66 minutes, 40 seconds" or perhaps "66:40"

convertTime(4000, "seconds", "hours")
; would return "1 hour, 6 minutes, 40 seconds" or perhaps "1:6:40"

convertTime(1, "hour", "minutes")
; would return "60 minutes" or perhaps "60"
Edited by CyberSlug
Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
Link to comment
Share on other sites

I'll give it a shot.

I suck at math :)

MsgBox("","",TimeToStr ( 4354335, "The time from seconds is: %d day(s) %h:%m:%s" ))

Func TimeToStr( $iAmount, $sFormat)
    Local $Days = 0, $Hours = 0, $Minutes = 0, $Seconds = 0, $sReturn
    
; Parsing out days
    If StringRegExp ( $sFormat, "%d" ) Then
        If $iAmount >= 86400 Then
            $Days = Int ( $iAmount/86400 )
            $iAmount = $iAmount - $Days * 86400
        EndIf   
    EndIf
    
; Parsing out hours
    If StringRegExp ( $sFormat, "%h" ) Then
        If $iAmount >= 3600 Then
            $Hours = Int ( $iAmount/3600 )
            $iAmount = $iAmount - $Hours * 3600
            If $Hours < 10 Then $Hours = "0" & $Hours
        EndIf
    EndIf
    
; Parsing out minutes   
    If StringRegExp ( $sFormat, "%m" ) Then
        If $iAmount >= 60 Then
            $Minutes = Int ( $iAmount/60 )
            $iAmount = $iAmount - $Minutes * 60
            If $Hours < 10 Then $Hours = "0" & $Minutes
        EndIf
    EndIf
    
; Parsing out seconds   
    If StringRegExp ( $sFormat, "%s" ) Then
    ; For the sake of beauty I alias $Seconds to the remaining seconds
        $Seconds = Int ( $iAmount )
        If $Seconds < 10 Then $Seconds = "0" & $Seconds
    EndIf
    
; Multi-level string replace, because I'm lazy
    return StringRegExpReplace( StringRegExpReplace( StringRegExpReplace( StringRegExpReplace( $sFormat, "%s", $Seconds ), "%m", $Minutes ), "%h", $Hours ), "%d", $Days )
EndFunc

If this isn't what you want I'll be glad to do _exactly_ as you said :D

EDIT:

Silly me, completely forgot rounding and stuff, only tested it once.

EDIT#2:

Modified so if unit is included in the format it will be processed.

Edited by Insolence
"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.
Link to comment
Share on other sites

Hi,

First slight fix; "endif"s changed, and line 19 typo hours for minutes -OK!

;;MsgBox("","",TimeToStr ( 4354335, "The time from seconds is: %d day(s) %h:%m:%s" ))

MsgBox("","",TimeToStr ( 4320000, "The time from seconds is: %d day(s) %h:%m:%s" ))

Func TimeToStr( $iAmount, $sFormat)

    Local $Days = 0, $Hours = 0, $Minutes = 0, $Seconds = 0, $sReturn

   

; Parsing out days

    If StringRegExp ( $sFormat, "%d" ) Then

        If $iAmount >= 86400 Then

            $Days = Int ( $iAmount/86400 )

            $iAmount = $iAmount - $Days * 86400

        EndIf   

    EndIf

   

; Parsing out hours

    If StringRegExp ( $sFormat, "%h" ) Then

        If $iAmount >= 3600 Then

            $Hours = Int ( $iAmount/3600 )

            $iAmount = $iAmount - $Hours * 3600

        EndIf

            If $Hours < 10 Then $Hours = "0" & $Hours

    EndIf

   

; Parsing out minutes   

    If StringRegExp ( $sFormat, "%m" ) Then

        If $iAmount >= 60 Then

            $Minutes = Int ( $iAmount/60 )

            $iAmount = $iAmount - $Minutes * 60

        EndIf

            If $Minutes < 10 Then $Minutes = "0" & $Minutes

    EndIf

   

; Parsing out seconds   

    If StringRegExp ( $sFormat, "%s" ) Then

    ; For the sake of beauty I alias $Seconds to the remaining seconds

        $Seconds = Int ( $iAmount )

    EndIf

        If $Seconds < 10 Then $Seconds = "0" & $Seconds

   

; Multi-level string replace, because I'm lazy

    return StringRegExpReplace( StringRegExpReplace( StringRegExpReplace( StringRegExpReplace( $sFormat, "%s", $Seconds ), "%m", $Minutes ), "%h", $Hours ), "%d", $Days )

EndFunc

Best, Randall Edited by randallc
Link to comment
Share on other sites

Thanks.

Only wrote it in about 10 minutes, so problems are to be expected :)

"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...