musurca
Posts: 128
Joined: 7/16/2020 Status: offline
|
quote:
if TimeVar < 0 then local a = os.date("*t",TimeVar) -- build timestamp table -- you need to manually format it from the table msgtime = string.format("%d.%d.%d", a.month, a.day, a.year-52) end Okay, thank you, Paul! FYI, the corrected script you posted also doesn't work (at least on my end) because if you pass a negative TimeVar < -43200 to os.date(), you get no return value--just the same error message that I posted before: ERROR: [string "chunk"]:35: time result cannot be represented in this installation. I realized it would probably be quicker for me to just partially reimplement os.date() instead of griping about it, so I cooked up a function that should work for Unix epoch times from Jan 1 1900 through Dec 31 2099. But this really seems like overkill, and hopefully there's a simpler solution related to your script suggestion, or else the Lua Date/Time library as you offered. If not, maybe there should be something like this in the CMO API to avoid time-related bugs, if it's not a huge pain to implement? (apologies for the formatting--using the "code" header seems to break the forum CSS for me) function EpochTimeToUTC(etime, date_sep, time_sep) date_sep = date_sep or "." time_sep = time_sep or "." function isLeapYear(y) if y % 4 == 0 then if y % 100 == 0 then if y % 400 == 0 then return true end return false end return true end return false end local days_in_month_common = {31,28,31,30,31,30,31,31,30,31,30,31} local days_in_month_leap = {31,29,31,30,31,30,31,31,30,31,30,31} --start from midnight Jan 1, 1900, or -2208988800 epoch seconds --note that 1900 was NOT a leap year due to Gregorian rules. --However 1904 was--so it's okay to start here. local EPOCH_20TH_CENTURY = -2208988800 local SECONDS_TO_HOURS = 60*60 local SECONDS_TO_DAYS = SECONDS_TO_HOURS*24 local SECONDS_TO_COMMON_YEAR = SECONDS_TO_DAYS*365 local SECONDS_TO_LEAP_YEAR = SECONDS_TO_DAYS*366 local SECONDS_IN_LEAP_SEQ = SECONDS_TO_COMMON_YEAR*3+SECONDS_TO_LEAP_YEAR etime = etime - EPOCH_20TH_CENTURY local leap_seqs = math.floor(etime / SECONDS_IN_LEAP_SEQ) local remainder_secs = etime % SECONDS_IN_LEAP_SEQ local remainder_years = math.floor(remainder_secs / SECONDS_TO_COMMON_YEAR) remainder_secs = remainder_secs % SECONDS_TO_COMMON_YEAR local remainder_days = math.floor(remainder_secs / SECONDS_TO_DAYS) remainder_secs = remainder_secs % SECONDS_TO_DAYS local year = 1900+leap_seqs*4+remainder_years local month_table if isLeapYear(year) then month_table = days_in_month_leap else month_table = days_in_month_common end local month = 1 local day = 1 while remainder_days > 0 do if remainder_days > month_table[month] then remainder_days = remainder_days - month_table[month] month = month + 1 else day = day + remainder_days remainder_days = 0 end end local hour = math.floor(remainder_secs / SECONDS_TO_HOURS) remainder_secs = remainder_secs % SECONDS_TO_HOURS local minute = math.floor(remainder_secs / 60) local second = remainder_secs % 60 if hour < 10 then hour = "0"..tostring(hour) end if minute < 10 then minute = "0"..tostring(minute) end if second < 10 then second = "0"..tostring(second) end return { Date=month..date_sep..day..date_sep..year, Time=hour..time_sep..minute..time_sep..second } end
< Message edited by musurca -- 3/22/2021 8:22:47 PM >
|