Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --# Main
- -- App 03 - Date and Time
- -- Bruce Elliott
- -- April 2013
- -- A tutorial on using time and date
- -- os.time() - number argument to os.date or os.difftime
- -- os.date() - string or table with date and time
- -- os.clock() - approx. time in seconds of CPU time used by program
- -- os.difftime() - number of seconds from t2 to t1
- -- os.setlocale() - set locale of the program
- -- Some information from Programming in Lua Chap. 22.1
- function setup()
- time1 = os.clock()
- parameter.action("os.Time", osTime)
- parameter.action("os.Date", osDate)
- parameter.action("os.Date(param)", osDate2)
- parameter.action("os.clock", osClock)
- print("Date and Time functions")
- print(" ")
- print("Press a button to see that action")
- print("Read comments for explanations")
- print("Scroll Output window text up and down")
- end
- function osClock()
- --os.clock and os.difftime
- -- os.clock returns approx. CPU time used
- -- os.difftime returns difference in approx. seconds
- output.clear()
- --
- print("clock 1: "..time1)
- -- Capture end time
- time2 = os.clock()
- print("clock 2: "..time2)
- -- Compute CPU time used
- diftime = os.difftime(time2, time1)
- -- Display results
- print("time2: "..time2.. " minus time1:"..time1)
- print("CPU time used:".. diftime.." seconds")
- end
- function osDate()
- -- os.date( )
- -- Field name, meanings
- -- hour 24 hr format
- -- min
- -- wday 1 is Sunday
- -- day
- -- month
- -- year
- -- sec
- -- yday 1 is January 1
- -- isdst is daylight saving time
- ----
- -- 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.
- -- Programming in Lua
- output.clear()
- -- os.date()
- -- no parameter returns current date time value
- print("os.date Examples")
- daytime = os.date()
- print("daytime = os.date()")
- print("print(daytime)")
- print()
- print("output: ")
- print(daytime)
- -- To produce a date table, we use the format string "*t".
- -- For instance, the following code
- print("")
- print("dval = os.time()")
- print("temp = os.date(\"*t\", dval)")
- -- Use os.time to get date time number
- dval = os.time()
- -- Use os.date to build table temp
- temp = os.date("*t", dval)
- -- creates the table we use to display the os.time() data
- -- We can display the table useing this code
- -- The isdst fiels is boolean and boolean data can not
- -- be concatenated.
- print("")
- print("temp Table Value output")
- for k, v in pairs(temp) do
- -- Check for isdst ~= means not equal
- if k ~= "isdst" then
- print("Key: "..k .." Value: ".. v)
- end
- end
- -- Display last value from table
- if temp["isdst"] == true then
- print("isdst is true")
- else
- print("isdst is false")
- end
- end
- function osTime()
- output.clear()
- -- os.time()
- -- os.time returns a number that represents
- -- date and time information
- -- os.time() no parameter
- -- Store os.time as number
- print("ostime = os.time()")
- ostime = os.time()
- -- Print result
- print("os.time value: "..ostime)
- -- Display os.time as os.date table fields
- osTimeOutput:output(ostime)
- -- os.time(table)
- -- Convert table data to number
- -- table requires at least three fields year, month, day
- -- field order does not appear to matter
- -- os.time{} and os.time({}) both appear to work
- -- try each format and order:
- --newtime = os.time({year = 2013, month = 4, day = 23})
- --newtime = os.time({month = 4, day = 23, year = 2013})
- print("newtime =")
- print("os.time{day=23, year=2013, month=4}")
- newtime = os.time{day = 23, year = 2013, month = 4}
- print("newtime value: "..newtime)
- print("Output os.time data")
- osTimeOutput:output(newtime)
- -- Notice that returned time data may not be accurate
- -- supply hour, min, sec, isdst if important
- -- newtime = os.time{day = 23, year = 2013, month = 4, hour = 10}
- -- osTimeOutput:output(newtime)
- end
- function osDate2()
- -- os.date("*t", dval) produces the table
- -- {year = 1998, month = 9, day = 16,
- -- yday = 259, wday = 4, hour = 23, min = 48,
- -- sec = 10, isdst = false}
- -- The actual values differ based on current date and time
- -- 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).
- -- For other format strings, os.date formats the date as a string,
- -- which is a copy of the format string where specific tags
- -- are replaced by information about time and date.
- -- All tags are represented by a `%´ followed by a letter,
- -- as in the next examples: (from Programming in Lua )
- output.clear()
- print("print(os.date(\"today is \%A, in \%B\")")
- print(os.date("today is %A, in %B"))
- -- example output--> today is Tuesday, in May
- print("print(os.date(\"\%x\", 906000490))")
- print(os.date("%x", 906000490))
- --> 09/16/1998
- -- Here is the Programming in Lua table of format characters:
- -- %a abbreviated weekday name (e.g., Wed)
- -- %A full weekday name (e.g., Wednesday)
- -- %b abbreviated month name (e.g., Sep)
- -- %B full month name (e.g., September)
- -- %c date and time (e.g., 09/16/98 23:48:10)
- -- %d day of the month (16) [01-31]
- -- %H hour, using a 24-hour clock (23) [00-23]
- -- %I hour, using a 12-hour clock (11) [01-12]
- -- %M minute (48) [00-59]
- -- %m month (09) [01-12]
- -- %p either "am" or "pm" (pm)
- -- %S second (10) [00-61]
- -- %w weekday (3) [0-6 = Sunday-Saturday]
- -- %x date (e.g., 09/16/98)
- -- %X time (e.g., 23:48:10)
- -- %Y full year (1998)
- -- %y two-digit year (98) [00-99]
- -- %% the character `%´
- -- These can be used in various combinations
- -- For instance to get two digit year, month, day,
- -- hour, minute, and second as a single string:
- print("thisdate = os.date(\"\%y\%m\%d\%H\%M\%S\")")
- thisdate = os.date("%y%m%d%H%M%S")
- print("Formatted date: ".. thisdate)
- --Here are the codes used
- -- %y two digit year %m two digit month
- -- %d two digit day %H 24 hour hour
- -- %M two digit minute %S two digit sec
- -- Notice that %H formatted the hour in 24 hour format
- -- %I would give 12 hour format
- -- This investigation started because I wanted to create
- -- a unique filename for storing camera shots
- end
- --# osTimeOutput
- osTimeOutput = class()
- function osTimeOutput:output(ostime)
- -- Convert ostime to a table
- -- Convert os.time data to a table
- timeTab = os.date("*t", ostime)
- -- Display table keys and values
- -- Note: The last value is a boolean value. An attempt to
- -- concatenate boolean results in error
- for k, v in pairs(timeTab) do
- if k ~= "isdst" then
- p2 = "Key: "..k .." ".. "Value: "..v
- print(p2)
- end
- end
- -- Display last value from table
- if timeTab["isdst"] == true then
- print("isdst is true")
- else
- print("isdst is false")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement