How to know the last weekend of a month in LUA

Asked

Viewed 92 times

4

How to get the last weekend of the month of March to change your time to daylight saving time.

Code to identify if it is the month of March and if it is a Sunday.

month_now=os.date("%m")
week_day=os.date("%w")
if week_day=="7" then
  print("domingo")
end
if month_now=="03" then
  print("Março")
end
  • I have my doubts whether I should do this to find out about daylight savings, it doesn’t seem to be a reliable criterion. Apart from that, you need to know what the last week of the month is. Is it the last one that starts in the month? Is it the last one that ends in it? What day is used to determine the beginning of the week?

  • Exactly, I need to know the last week of that month. From what I saw there is no such language as we can know the week.

  • Exactly what? I think you didn’t read what I wrote and mainly I asked. Without defining the concept will give you wrong result. Although I reaffirm that you are probably already using the wrong method to set daylight saving time.

  • What is the best method to use?

  • 1

    Daylight saving time is set annually. It makes no sense to ask this question, if you go to Brazil.

1 answer

3

Without getting into the merit of daylight saving time, the function below calculates the day of the last Saturday of March of a given year.

function last(y)
    local t={ day=31, month=3, year=y }
    local w=os.date("*t",os.time(t)).wday
    return 31-(w%7)
end

Browser other questions tagged

You are not signed in. Login or sign up in order to post.