Advertisement
br1wr2el3

App 03 - Date and Time

Apr 24th, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.89 KB | None | 0 0
  1. --# Main
  2. -- App 03 - Date and Time
  3.  
  4. -- Bruce Elliott
  5. -- April 2013
  6.  
  7. -- A tutorial on using time and date
  8.  
  9. -- os.time() - number argument to os.date or os.difftime
  10. -- os.date() - string or table with date and time
  11. -- os.clock() - approx. time in seconds of CPU time used by program
  12.  
  13. -- os.difftime() - number of seconds from t2 to t1
  14. -- os.setlocale() - set locale of the program
  15.  
  16. -- Some information from Programming in Lua Chap. 22.1
  17.  
  18. function setup()
  19.  
  20. time1 = os.clock()
  21.  
  22.  
  23. parameter.action("os.Time", osTime)
  24. parameter.action("os.Date", osDate)
  25. parameter.action("os.Date(param)", osDate2)
  26. parameter.action("os.clock", osClock)
  27.  
  28. print("Date and Time functions")
  29. print(" ")
  30. print("Press a button to see that action")
  31. print("Read comments for explanations")
  32. print("Scroll Output window text up and down")
  33.  
  34. end
  35.  
  36. function osClock()
  37.  
  38. --os.clock and os.difftime
  39.  
  40. -- os.clock returns approx. CPU time used
  41. -- os.difftime returns difference in approx. seconds
  42.  
  43. output.clear()
  44.  
  45. --
  46. print("clock 1: "..time1)
  47.  
  48. -- Capture end time
  49. time2 = os.clock()
  50.  
  51. print("clock 2: "..time2)
  52.  
  53. -- Compute CPU time used
  54. diftime = os.difftime(time2, time1)
  55.  
  56. -- Display results
  57. print("time2: "..time2.. " minus time1:"..time1)
  58. print("CPU time used:".. diftime.." seconds")
  59.  
  60. end
  61.  
  62. function osDate()
  63.  
  64. -- os.date( )
  65. -- Field name, meanings
  66. -- hour 24 hr format
  67. -- min
  68. -- wday 1 is Sunday
  69. -- day
  70. -- month
  71. -- year
  72. -- sec
  73. -- yday 1 is January 1
  74. -- isdst is daylight saving time
  75.  
  76.  
  77. ----
  78. -- The date function, despite its name, is a kind of a
  79. -- reverse of the time function: It converts a number
  80. -- representing the date and time back to some higher-level
  81. -- representation. Its first parameter is a format string,
  82. -- describing the representation we want. The second is the
  83. -- numeric date-time; it defaults to the current date and time.
  84. -- Programming in Lua
  85.  
  86. output.clear()
  87.  
  88. -- os.date()
  89. -- no parameter returns current date time value
  90. print("os.date Examples")
  91. daytime = os.date()
  92. print("daytime = os.date()")
  93. print("print(daytime)")
  94. print()
  95. print("output: ")
  96. print(daytime)
  97.  
  98.  
  99. -- To produce a date table, we use the format string "*t".
  100. -- For instance, the following code
  101.  
  102. print("")
  103. print("dval = os.time()")
  104. print("temp = os.date(\"*t\", dval)")
  105. -- Use os.time to get date time number
  106. dval = os.time()
  107. -- Use os.date to build table temp
  108. temp = os.date("*t", dval)
  109.  
  110. -- creates the table we use to display the os.time() data
  111. -- We can display the table useing this code
  112. -- The isdst fiels is boolean and boolean data can not
  113. -- be concatenated.
  114. print("")
  115. print("temp Table Value output")
  116. for k, v in pairs(temp) do
  117. -- Check for isdst ~= means not equal
  118. if k ~= "isdst" then
  119. print("Key: "..k .." Value: ".. v)
  120. end
  121. end
  122.  
  123. -- Display last value from table
  124. if temp["isdst"] == true then
  125. print("isdst is true")
  126. else
  127. print("isdst is false")
  128. end
  129.  
  130. end
  131.  
  132.  
  133. function osTime()
  134.  
  135. output.clear()
  136.  
  137. -- os.time()
  138.  
  139. -- os.time returns a number that represents
  140. -- date and time information
  141.  
  142. -- os.time() no parameter
  143. -- Store os.time as number
  144.  
  145. print("ostime = os.time()")
  146. ostime = os.time()
  147.  
  148. -- Print result
  149. print("os.time value: "..ostime)
  150.  
  151. -- Display os.time as os.date table fields
  152. osTimeOutput:output(ostime)
  153.  
  154. -- os.time(table)
  155. -- Convert table data to number
  156. -- table requires at least three fields year, month, day
  157. -- field order does not appear to matter
  158. -- os.time{} and os.time({}) both appear to work
  159. -- try each format and order:
  160.  
  161. --newtime = os.time({year = 2013, month = 4, day = 23})
  162. --newtime = os.time({month = 4, day = 23, year = 2013})
  163. print("newtime =")
  164. print("os.time{day=23, year=2013, month=4}")
  165. newtime = os.time{day = 23, year = 2013, month = 4}
  166.  
  167. print("newtime value: "..newtime)
  168.  
  169. print("Output os.time data")
  170. osTimeOutput:output(newtime)
  171.  
  172. -- Notice that returned time data may not be accurate
  173. -- supply hour, min, sec, isdst if important
  174. -- newtime = os.time{day = 23, year = 2013, month = 4, hour = 10}
  175. -- osTimeOutput:output(newtime)
  176.  
  177.  
  178. end
  179.  
  180. function osDate2()
  181.  
  182. -- os.date("*t", dval) produces the table
  183. -- {year = 1998, month = 9, day = 16,
  184. -- yday = 259, wday = 4, hour = 23, min = 48,
  185. -- sec = 10, isdst = false}
  186. -- The actual values differ based on current date and time
  187.  
  188. -- Notice that, besides the fields used by os.time, the table
  189. -- created by os.date also gives the week day (wday, 1 is Sunday)
  190. -- and the year day (yday, 1 is January 1).
  191.  
  192. -- For other format strings, os.date formats the date as a string,
  193. -- which is a copy of the format string where specific tags
  194. -- are replaced by information about time and date.
  195. -- All tags are represented by a `%´ followed by a letter,
  196. -- as in the next examples: (from Programming in Lua )
  197.  
  198. output.clear()
  199.  
  200. print("print(os.date(\"today is \%A, in \%B\")")
  201. print(os.date("today is %A, in %B"))
  202. -- example output--> today is Tuesday, in May
  203. print("print(os.date(\"\%x\", 906000490))")
  204. print(os.date("%x", 906000490))
  205. --> 09/16/1998
  206.  
  207. -- Here is the Programming in Lua table of format characters:
  208.  
  209. -- %a abbreviated weekday name (e.g., Wed)
  210. -- %A full weekday name (e.g., Wednesday)
  211. -- %b abbreviated month name (e.g., Sep)
  212. -- %B full month name (e.g., September)
  213. -- %c date and time (e.g., 09/16/98 23:48:10)
  214. -- %d day of the month (16) [01-31]
  215. -- %H hour, using a 24-hour clock (23) [00-23]
  216. -- %I hour, using a 12-hour clock (11) [01-12]
  217. -- %M minute (48) [00-59]
  218. -- %m month (09) [01-12]
  219. -- %p either "am" or "pm" (pm)
  220. -- %S second (10) [00-61]
  221. -- %w weekday (3) [0-6 = Sunday-Saturday]
  222. -- %x date (e.g., 09/16/98)
  223. -- %X time (e.g., 23:48:10)
  224. -- %Y full year (1998)
  225. -- %y two-digit year (98) [00-99]
  226. -- %% the character `%´
  227.  
  228. -- These can be used in various combinations
  229. -- For instance to get two digit year, month, day,
  230. -- hour, minute, and second as a single string:
  231.  
  232. print("thisdate = os.date(\"\%y\%m\%d\%H\%M\%S\")")
  233. thisdate = os.date("%y%m%d%H%M%S")
  234. print("Formatted date: ".. thisdate)
  235.  
  236. --Here are the codes used
  237. -- %y two digit year %m two digit month
  238. -- %d two digit day %H 24 hour hour
  239. -- %M two digit minute %S two digit sec
  240.  
  241. -- Notice that %H formatted the hour in 24 hour format
  242. -- %I would give 12 hour format
  243.  
  244. -- This investigation started because I wanted to create
  245. -- a unique filename for storing camera shots
  246.  
  247. end
  248.  
  249.  
  250.  
  251.  
  252. --# osTimeOutput
  253. osTimeOutput = class()
  254.  
  255. function osTimeOutput:output(ostime)
  256. -- Convert ostime to a table
  257.  
  258. -- Convert os.time data to a table
  259. timeTab = os.date("*t", ostime)
  260.  
  261. -- Display table keys and values
  262. -- Note: The last value is a boolean value. An attempt to
  263. -- concatenate boolean results in error
  264. for k, v in pairs(timeTab) do
  265. if k ~= "isdst" then
  266. p2 = "Key: "..k .." ".. "Value: "..v
  267. print(p2)
  268. end
  269. end
  270.  
  271. -- Display last value from table
  272. if timeTab["isdst"] == true then
  273. print("isdst is true")
  274. else
  275. print("isdst is false")
  276. end
  277. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement