]> git.madduck.net Git - etc/awesome.git/blob - examples/date_arithmetic.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

luatz/tzinfo: Add __name field to metatables
[etc/awesome.git] / examples / date_arithmetic.lua
1 local luatz = require "luatz"
2
3 -- We do this a few times ==> Convert a timestamp to timetable and normalise
4 local function ts2tt ( ts )
5         return luatz.timetable.new_from_timestamp ( ts )
6 end
7
8 -- Get the current time in UTC
9 local utcnow = luatz.time ( )
10 local now = ts2tt ( utcnow )
11 print ( now , "now (UTC)" )
12
13 -- Get a new time object 6 months from now
14 local x = now:clone ( )
15 x.month = x.month + 6
16 x:normalise ( )
17 print ( x , "6 months from now" )
18
19 -- Find out what time it is in Melbourne at the moment
20 local melbourne = luatz.get_tz ( "Australia/Melbourne" )
21 local now_in_melbourne = ts2tt ( melbourne:localise ( utcnow ) )
22 print ( now_in_melbourne , "Melbourne" )
23
24 -- Six months from now in melbourne (so month is incremented; but still the same time)
25 local m = now_in_melbourne:clone ( )
26 m.month = m.month + 6
27 m:normalise ( )
28 print ( m , "6 months from now in melbourne" )
29
30 -- Convert time back to utc; a daylight savings transition may have taken place!
31 -- There may be 2 results, but for we'll ignore the second possibility
32 local c1 , c2 = melbourne:utctime ( m:timestamp( ) )
33 print ( ts2tt ( c1 ) , "6 months from now in melbourne converted to utc" )