Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Showing the date in local format

Showing the date in local format

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Sat 24 Dec 2005 12:26 AM (UTC)
Message
I noticed an interesting problem in Lua scripts while working on the Chat plugin.

The default for the locale seems to be the United States, because if you start MUSHclient (with Lua as the scripting language) and type:


/print (os.date ("%x"))  --> 12/24/05


Now this might seem OK if you live in the USA, however the date there is MM/DD/YY which is not what we use in Australia and other parts of the world.

In my case if I then type:


/print (os.setlocale ("", "time")) --> English_Australia.1252


Now it has set the locale to Australia. Now getting the date gives a different result:


/print (os.date ("%x"))  --> 24/12/05


This is now DD/MM/YY which is the local convention.

Thus scripters might want to consider putting:


os.setlocale ("", "time")


... at the start of Lua scripts (that display dates/times etc.) to set the locale correctly.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #1 on Sat 24 Dec 2005 12:29 AM (UTC)
Message
Interestingly, once you do that once, it seems to set the locale for the entire executable, not just the current script environment.

For example, reloading the script retains the current locale.

Thus, it would be possible to make a tiny plugin that simply sets the locale, and does nothing else, as that would then affect every world and plugin.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Metsuro   USA  (389 posts)  Bio
Date Reply #2 on Sat 24 Dec 2005 02:13 AM (UTC)
Message
Donno if i should post this here but ya... I am nosey and wanted to test it out heh...

/print (os.date ("%x"))

Error number: 0
Event: Run-time error
Description: [string "Command line"]:1: attempt to index global `os' (a nil value)
stack traceback:
[string "Command line"]:1: in main chunk
Called by: Immediate execution

just wondering why that does that?

Everything turns around in the end
Top

Posted by Jestre   (13 posts)  Bio
Date Reply #3 on Sat 24 Dec 2005 04:51 AM (UTC)
Message
Os calls in lua are disabled by default. You'll need to go to File > Global Preferences > Lua tab and enable it.
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #4 on Mon 26 Dec 2005 01:08 AM (UTC)
Message
Yes, in the Global Preferences, all calls to the os table are disabled by default.

You could uncomment the line:


os = nil


However, that removes the safety check it provides, by not allowing plugins to do things like remove files.

A safer approach is to save the "safe" functions, like this:


do
  local a, b, c, d, e = os.date, os.time, os.setlocale, os.clock, os.difftime
  os = {}  -- make new table
  os.date, os.time, os.setlocale, os.clock, os.difftime = a, b, c, d, e 
end


This basically saves the addresses of the harmless routines, creats a new "os" table, thus making the old one inaccessible, and then puts the old functions back into the new table.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


17,651 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.