{"action":"closed","issue":{"url":"https://api.github.com/repos/daurnimator/luatz/issues/8","repository_url":"https://api.github.com/repos/daurnimator/luatz","labels_url":"https://api.github.com/repos/daurnimator/luatz/issues/8/labels{/name}","comments_url":"https://api.github.com/repos/daurnimator/luatz/issues/8/comments","events_url":"https://api.github.com/repos/daurnimator/luatz/issues/8/events","html_url":"https://github.com/daurnimator/luatz/issues/8","id":155429576,"number":8,"title":"Possible optimisation for normalise(...) function in timetable.lua","user":{"login":"maciejmiklas","id":3326660,"avatar_url":"https://avatars.githubusercontent.com/u/3326660?v=3","gravatar_id":"","url":"https://api.github.com/users/maciejmiklas","html_url":"https://github.com/maciejmiklas","followers_url":"https://api.github.com/users/maciejmiklas/followers","following_url":"https://api.github.com/users/maciejmiklas/following{/other_user}","gists_url":"https://api.github.com/users/maciejmiklas/gists{/gist_id}","starred_url":"https://api.github.com/users/maciejmiklas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/maciejmiklas/subscriptions","organizations_url":"https://api.github.com/users/maciejmiklas/orgs","repos_url":"https://api.github.com/users/maciejmiklas/repos","events_url":"https://api.github.com/users/maciejmiklas/events{/privacy}","received_events_url":"https://api.github.com/users/maciejmiklas/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2016-05-18T06:59:39Z","updated_at":"2016-05-18T08:49:48Z","closed_at":"2016-05-18T08:49:48Z","body":"There is this main function that calculates year, month and day. For large days it produces large amount of iterations. You can speed it up, by calculating offset for #sec and #year.\r\n\r\nDefine new function:\r\n\r\n```\r\nfunction year_offset(ts)\r\n\tlocal year, offset\r\n\tif ts >= 1735689600 then\r\n\t\tyear, offset = 2025, 1735689600 -- 1.1.2015\r\n\t\r\n\telseif ts >= 1577836800 then\r\n\t\tyear, offset = 2020, 1577836800 -- 1.1.2020\r\n\t\r\n\telseif ts >= 1420070400 then\r\n\t\tyear, offset = 2015, 1420070400 -- 1.1.2015\r\n\t\r\n\telseif ts >= 1262304000 then\r\n\t\tyear, offset = 2010, 1262304000 -- 1.1.2010\r\n\t\r\n\telseif ts >= 946684800 then\r\n\t\tyear, offset = 2000, 946684800 -- 1.1.2000\r\n\t\r\n\telseif ts >= 631152000 then\r\n\t\tyear, offset = 1990, 631152000 -- 1.1.1990\r\n\t\r\n\telseif ts >= 315532800 then\r\n\t\tyear, offset = 1980, 315532800 -- 1.1.1980\r\n\t\r\n\telse\r\n\t\tyear, offset = 1970, 0\r\n\tend\r\n\treturn year, offset\r\nend\r\n```\r\n\r\nand call it here:\r\n\r\n```\r\nlocal function normalise ( year , month , day , hour , min , sec )\r\n    year, offset = year_offset(sec)\r\n    sec = sec - offset\r\n    .....\r\n```\r\n\r\nIn this case we are shifting #sec to know pre-calculated year and instead of starting on 1970 we start on closest year to given #sec "}}