Weather message plus current time

All discussions & material related to Command's Lua interface

Moderators: RoryAndersonCDT, michaelm75au, angster, MOD_Command

Post Reply
DavidRN
Posts: 91
Joined: Tue Jan 17, 2023 8:41 pm

Weather message plus current time

Post by DavidRN »

Hi

Getting there (slowly) with Lua, Ive setup weather with special message (as below) and current (random) forecast with hourly trigger this works fine, I would like to add the current time to the message.

How do I do that?

Do I add this ...

ScenEdit_CurrentTime ()

Code: Select all

local now = ScenEdit_CurrentTime()
local elapsed = now - timeFromLastTiggered
if elapsed > 60*5 then
-- been more than 5 minutes, set the lastTriggered time to now
timeFromLastTiggered = now
end

Weather with special message and hourly trigger

Code: Select all

------Set Weather and set it on a regular time trigger with a low % chance of firing

----- Temperature Range = 22C to 42C (71.6F to 107.6F)
local temp= math.random( 23, 25 )

------- Rain Range = 0 to 50
local rain= math.random( 0, 10 )

------- Cloud Range = 0.0 to1.0
local cloud= math.random( 2.0, 5.0 ) / 10.0

------- Sea State Range = 0 to 9
local sea= math.random( 3,5)

------- Set Ranges
ScenEdit_SetWeather( temp, rain, cloud, sea )


------- Special Message that can go with random weather


ScenEdit_SpecialMessage('Blue', '<P><FONT size=2 face="Courier New">ALL<BR>AUSTRALIAN GOVERNMENT BUREAU OF METEOROLOGY TASMANIA<BR>COASTAL WATERS FORECAST BASS STRAIT AND APPROACHES<BR><BR>TEMPERATURE: ' .. temp .. ' DEGREES CELSIUS<BR>RAIN STATE: ' .. rain .. '<BR>CLOUD LEVEL: ' .. cloud .. '<BR>SEA STATE: ' .. sea)

Assisstance would be appricated

DB
musurca
Posts: 168
Joined: Wed Jul 15, 2020 10:06 pm
Contact:

Re: Weather message plus current time

Post by musurca »

The built-in Lua function os.date(format, time) is your friend here. It produces a readable date in a custom format from a epoch time in seconds (which is what ScenEdit_CurrentTime() returns).

Try the following in the Lua Script Editor:

Code: Select all

local current_time = ScenEdit_CurrentTime()
local date_text = os.date("!%m/%d/%Y %H:%M:%S", current_time)
print(date_text)
The output should be something like this, depending on the current scenario time:

02/10/2023 21:26:21

The Lua documentation will give you more information about all of the acceptable tags for the format string. You'll also usually want to start with the character "!", as this forces the date to be printed in Coordinated Universal Time, not the user locale's time.

There's one major caveat: os.date doesn't handle negative epoch times, which is any date prior to Jan 1 1970. If your scenario takes place prior to that date, you'll need to use two somewhat undocumented CMO functions instead: EpochToUTC_Date(time) and EpochToUTC_Time(time). They are employed as follows:

Code: Select all

local current_time = ScenEdit_CurrentTime()
local date = EpochToUTC_Date(current_time)
local time = EpochToUTC_Time(current_time)
print(date.." "..time)
That output will look something like this, depending on your locale:

02:10:2023 21:26:21

Since the separator characters may be different on each user's system, I use a wrapper function to sanitize the results:

Code: Select all

function EpochToUTC(etime, date_sep, time_sep)
    local date = EpochToUTC_Date(etime)
    local time = EpochToUTC_Time(etime)

    -- separator characters sometimes change between
    -- locales, so let's replace with our custom separators
    local date_tbl_str = string.gsub(date, "%D", date_sep)
    local time_tbl_str = string.gsub(time, "%D", time_sep)
    
    return {
        Date=date_tbl_str,
        Time=time_tbl_str
    }
end

local current_time = ScenEdit_CurrentTime()
local date_time = EpochToUTC(current_time, "/", ":")
print(date_time.Date.." "..date_time.Time)
Output:

02/10/2023 21:26:21
Post Reply

Return to “Lua Legion”