Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

convert number to time


Question

Posted

i have a number 18.9413 and need to convert it to a time string the result i should get is 18:56:29

 

how can i do that please?

20 answers to this question

Recommended Posts

  • 0
Posted

It is not epoch, what is the logic behind your number?

  • 0
Posted (edited)

Maybe not that most efficient

Please login or register to see this code.

 

Edited by jgab
  • 0
  • Inquirer
  • Posted
    5 hours ago, jgab said:

    Maybe not that most efficient

    Please login or register to see this code.

     

    amazing @jgab as always so spot on and thank you for your help.

    • 0
  • Inquirer
  • Posted
    5 hours ago, SmartHomeEddy said:

    It is not epoch, what is the logic behind your number?

    it is working sun times, in particular trying to get the time when the sun is 8 degrees below the horizan

    • 0
    Posted

    Ah, Decimal time

     

    Please login or register to see this link.

     

    Decimal time is the representation of the time of day using units which are 

    Please login or register to see this link.

     related. This term is often used specifically to refer to the time system used in 

    Please login or register to see this link.

     for a few years beginning in 1792 during the 

    Please login or register to see this link.

    , which divided the day into 10 decimal hours, each decimal hour into 100 decimal minutes and each decimal minute into 100 decimal seconds (100000 decimal seconds per day), as opposed to the more familiar 

    Please login or register to see this link.

     

    Please login or register to see this link.

    , which divides the day into 24 

    Please login or register to see this link.

    , each hour into 60 

    Please login or register to see this link.

     and each minute into 60 

    Please login or register to see this link.

     (86400 

    Please login or register to see this link.

     seconds per day)…

    • 0
    Posted

    @Jay Ess,

    are you willing to post your complete calculation sunrise and sunset? Looks interesting to use.

    Thanks in advance,

    //Sjakie

    • 0
  • Inquirer
  • Posted

    @Sjakie at the moment it is not working properly, have managed to set it up using sample code based on a particluar time and zone but when i try to run it on current dates and time and different time zone it bring back results which dont make sense.

     

    once i have managed to get it working i will be happy to post it.

     

    I do have a sunset/sunrise calculator but it is out by between 1 and 3 minutes depedning on the time of the year, hence why i am trying to fine tune it.

    • 0
    Posted

    At this moment I use to minimize the deviation a correction by month followed by % clouds.

    This is close to wath I want but % clouds has sometimes a big deviation.

    What I have works more convenient as an outside PIR.

    //Sjakie

    • 0
    Posted

    Just checking, did you looked at this quickapp?

     

    Please login or register to see this link.

     

     

    • 0
  • Inquirer
  • Posted (edited)

    @SmartHomeEddythank you for that link, will have a look at it.

     

    had to take down my code as posted by error.

     

     

    would you know how to get a specific time for a sun a position from that code please?

    Edited by Jay Ess
    • 0
  • Inquirer
  • Posted (edited)
    On 12/22/2021 at 11:01 AM, jgab said:

    Maybe not that most efficient

    Please login or register to see this code.

     

    think i am almost there, i need to take the result of this time, ie. 18:56:29 and then round it up or down to the nearest minute depeding on what i am working out, what is the most effecient way to do that please?

     

     

     

    Edited by Jay Ess
    • 0
    Posted

    function decTime2StringMinutes(a)
      return string.format("%02d:%02d",math.floor(a),math.floor(0.5+math.fmod(a,1)*60))
    end

    • 0
  • Inquirer
  • Posted

    @jgabthank you and use math.ceil for rounding up correct?

     

    so i now have a funtion to get the time, but at the moment the angle value is just a single number as in 0.83333, i want to run this function with a table of numbers, as take your function

    function decTime2StringMinutes(a)
      return string.format("%02d:%02d",math.floor(a),math.floor(0.5+math.fmod(a,1)*60))
    end

     

    for a i want to pass it a table of numbers.

     

    function QuickApp:GetTimes()

        local sunPosition = {

            ["SunSet"] = 1,

            ["blueHourDuskEnd"] = 2

        }

        local sortedKeys = 

            getSortedKeysByValue(

            sunPosition,

            function(a,b)

                return a < b

            end

        )

        --local sunTimes = nil

        for _,key in ipairs(sortedKeys) do

            print(key)

        end

    end

    but this does not work if i call it as GetTimes()

    • 0
  • Inquirer
  • Posted

    I have managed to build a table and iterate through it but the code seems clunky, how can i tidy this up please?

     

    function QuickApp:GetTimes()

        local sunPosition = {

            ["SunSet"] = -0.8333,

            ["blueHourDuskEnd"] = -8

        }

        function pairsByKeys (t, f)

          local a = {}

          for n in pairs(t) do table.insert(a, n) end

          table.sort(a, f)

          local i = 0      -- iterator variable

          local iter = function ()   -- iterator function

            i = i + 1

            if a[i] == nil then return nil

            else return a[i], t[a[i]]

            end

          end

          return iter

        end

        for name , angle in pairsByKeys(sunPosition)do

            R = angle

            print(hms(T))

        end

    end

    • 0
    Posted (edited)

    If you want to print out a key/value table in some specific order I recommend you to move the key/value to an array and sort the array on the keys.

    The create an iterator like in your case is massive overkill and not more efficient.

    Please login or register to see this code.

     

    Edited by jgab
    • 0
  • Inquirer
  • Posted

    @jgab that is amazing thank you, if i want to have three "fields" per row of data, need to change a calculation based on what angle i need, how would i do that?

     

    this is what my end table should be like

            {name = "sunset",angle = -0.8333, time = "A"}

            {name = "shabbosout",angle = -8, time = "A"}

     

     

    • 0
  • Inquirer
  • Posted

    thank you for all your help, i am one LOL step away from a working qa, i am stuck on round a decimal number as in 16.989568650404 when i do the following calculation

    i = 16.989568650404

    string.format("%02d:%02d",math.floor(i),math.ceil(math.fmod(i,1)*60)) 

    the retrun is 16.60 which is correct but is not a time, so i need to round it but am unsure how to do that. if someone could be so kind and show me how to accomplish that please?

     

    many thanks for all your contributions it really helps.

    • 0
    Posted
    On 12/30/2021 at 11:01 AM, jgab said:

    function decTime2StringMinutes(a)
      return string.format("%02d:%02d",math.floor(a),math.floor(0.5+math.fmod(a,1)*60))
    end

    Didn’t this work?

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...