Advertisement
RDS_YES

dateToEpoch

Dec 29th, 2024
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.19 KB | None | 0 0
  1. local year = 2025
  2. local month = 1
  3. local day = 1
  4. local hour = 0
  5. local minute = 0
  6. local second = 0
  7.  
  8. local function is_leap_year(year)
  9.     if (year % 4 == 0 and year % 100 ~= 0) or (year % 400 == 0) then
  10.         return true
  11.     else
  12.         return false
  13.     end
  14. end
  15.  
  16. local function days_in_month(month, year)
  17.     local days_in_months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
  18.     if month == 2 and is_leap_year(year) then
  19.         return 29
  20.     else
  21.         return days_in_months[month]
  22.     end
  23. end
  24.  
  25. local function days_since_epoch(year, month, day, hour, minute, second)
  26.     local days = 0
  27.  
  28.     for y = 1970, year - 1 do
  29.         if is_leap_year(y) then
  30.             days = days + 366
  31.         else
  32.             days = days + 365
  33.         end
  34.     end
  35.  
  36.     for m = 1, month - 1 do
  37.         days = days + days_in_month(m, year)
  38.     end
  39.  
  40.     days = days + (day - 1)
  41.  
  42.     local total_seconds = days * 86400
  43.  
  44.     total_seconds = total_seconds + (hour * 3600)
  45.     total_seconds = total_seconds + (minute * 60)
  46.     total_seconds = total_seconds + second
  47.  
  48.     return total_seconds
  49. end
  50.  
  51. local epochTime = days_since_epoch(year, month, day, hour, minute, second)
  52. print(epochTime)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement