[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Saving current date and time

Saving current date and time

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


Posted by KaiserBear   (9 posts)  [Biography] bio
Date Sat 03 Oct 2015 09:17 AM (UTC)
Message
I'm trying to make a trigger that would save all aspects of the current date and time, but I don't know how. I looked at time.os but it doesn't really make sense to me. Can someone show/tell me how to do this step by step? I'd be looking to fill the following variables:

currentDay
currentMonth
currentYear

currentHour
currentMinute
currentSeconds
amOrPm (or, adding +12 to currentHour when the time is pm - to convert to military time)

All of these would be numbers, not the word date, ideally. So January 2nd, 2001 would have the currentMonth = 1, and currentDay = 2, etc. If I had to have the day/month that's fine, I could probably figure a workaround to convert that into a number. My problem is how to pull the data into the above variables, and I don't even know where to start right now.
[Go to top] top

Posted by Fiendish   USA  (2,514 posts)  [Biography] bio   Global Moderator
Date Reply #1 on Sat 03 Oct 2015 10:09 AM (UTC)

Amended on Sat 03 Oct 2015 10:21 AM (UTC) by Fiendish

Message
To help you out, I'll quote selectively from http://www.lua.org/pil/22.1.html

Quote:

22.1 – Date and Time

Two functions, time and date, do all date and time queries in Lua.

The time function, when called without arguments, returns the current date and time, coded as a number. (In most systems, that number is the number of seconds since some epoch.)

...

The date function, despite its name, is a kind of a reverse of the time function: It converts a number representing the date and time back to some higher-level representation. Its first parameter is a format string, describing the representation we want. The second is the numeric date-time; it defaults to the current date and time.

To produce a date table, we use the format string "*t". For instance, the following code

date_table = os.date("*t", 906000490)

produces the table

{year = 1998, month = 9, day = 16, yday = 259, wday = 4,
hour = 23, min = 48, sec = 10, isdst = false}

...

date tables have the following significant fields:
year a full year
month 01-12
day 01-31
hour 01-31
min 00-59
sec 00-59
isdst a boolean, true if daylight saving

...

Notice that, besides the fields used by os.time, the table created by os.date also gives the week day (wday, 1 is Sunday) and the year day (yday, 1 is January 1).


So you either want to do...


current_time = os.time()
date_table = os.date("*t", current_time)


or more simply, just


date_table = os.date("*t")


which is a shortcut for the former, but the former allows you to save a timestamp for later.

Then, for example, your "currentMonth" would be

currentMonth = date_table.month



You can verify for yourself the output of os.date with the following simple printing code...

for k,v in pairs(os.date("*t")) do
  print(k.." , "..tostring(v)) 
end

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by KaiserBear   (9 posts)  [Biography] bio
Date Reply #2 on Sat 03 Oct 2015 10:32 AM (UTC)
Message
That works perfectly, thanks. I haven't messed with tables before with Lua, this helped a lot. I really appreciate the help.
[Go to top] 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.


9,525 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]