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


Recommended Posts

Posted

Why is it that this script returns a different response if ran within a scene on Fibaro HC 2 and within this test environment 

Please login or register to see this link.

 

-------------------------
-- Every other weekend --
-- Yes for Sat or Sun  --
-- 08/04/2017 Yes
-- 15/04/2017 No
-- 22/04/2017 Yes
-- 29/04/2017 No
-- 06/05/2017 Yes

print("New session");

function IsOurWeekend(d,m,y)
    local twoweeks = 1209600;   -- Seconds in two weeks.
    local oneday = 86400;       -- Seconds in one day.
    local reference = os.time{year=2017,month=1,day=14, hour=0, minute=0, second=0};
    --local current = os.time{year=now.year, month=now.month, day=now.day, hour=0, minute=0, second=0}
    local current = os.time{year=y, month = m, day= d, hour=0, minute=0, second=0};
    local wday = os.date("*t", current);
    if (wday ~= 7 and wday ~= 1) then -- Is it Saturday or Sunday?
        return false;
    end
    local dif = os.difftime(current, reference);
    local x = dif % twoweeks;
    return x == 0 or x == oneday;
end

--local now = os.date("*t");
--local current = os.time{year=now.year, month=now.month, day=now.day, hour=0, minute=0, second=0}
local now = os.date("*t", os.time{year=2017, month = 5, day= 6, hour=0, minute=0, second=0});
if IsOurWeekend(now.day, now.month, now.year) then
    print("Our Weekend");
else
    print("Not our weekend");
end

 

Posted

Because the tutorialspoint server is running in another time zone?

  • Topic Author
  • Posted

    I don't think it's a timezone issue as I am specifying the dates myself and not using the servers time. Additionally, with the test data at the top of the code, using 22/04/2017 it works and yet four weeks later 06/05/2017 does not.

     

    Its as though the numbers are too great.

  • Topic Author
  • Posted

    Oops, I've realised it was a bug in the program - Fixes in bold

    -------------------------
    -- Every other weekend --
    -- Yes for Sat or Sun  --
    -- 08/04/2017 Yes
    -- 15/04/2017 No
    -- 22/04/2017 Yes
    -- 29/04/2017 No
    -- 06/05/2017 Yes
    print("New session");
    function IsOurWeekend(d,m,y)
        local twoweeks = 1209600;   -- Seconds in two weeks.
        local oneday = 86400;       -- Seconds in one day.
        local reference = os.time{year=2017,month=1,day=14, hour=0, minute=0, second=0};
        --local current = os.time{year=now.year, month=now.month, day=now.day, hour=0, minute=0, second=0}
        local current = os.time{year=y, month = m, day= d, hour=0, minute=0, second=0};
        local d = os.date("*t", current);
        if (d.wday ~= 7 and d.wday ~= 1) then -- Is it Saturday or Sunday?

            return false;
        end
        local dif = os.difftime(current, reference);
        local x = dif % twoweeks;
        return x == 0 or x == oneday;
    end
    --local now = os.date("*t");
    --local current = os.time{year=now.year, month=now.month, day=now.day, hour=0, minute=0, second=0}
    local now = os.date("*t", os.time{year=2017, month = 5, day= 8, hour=0, minute=0, second=0});
    if IsOurWeekend(now.day, now.month, now.year) then
        print("Our Weekend");
    else
        print("Not our weekend");
    end

    Join the conversation

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

    Guest
    Reply to this topic...

    ×   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...