]> git.madduck.net Git - etc/awesome.git/commitdiff

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:

examples/date_arithmetic: Add an example that shows of date arithmetic
authordaurnimator <quae@daurnimator.com>
Sun, 24 Nov 2013 11:04:38 +0000 (06:04 -0500)
committerdaurnimator <quae@daurnimator.com>
Sun, 24 Nov 2013 11:05:56 +0000 (06:05 -0500)
examples/date_arithmetic.lua [new file with mode: 0644]

diff --git a/examples/date_arithmetic.lua b/examples/date_arithmetic.lua
new file mode 100644 (file)
index 0000000..3b1126d
--- /dev/null
@@ -0,0 +1,33 @@
+local luatz = require "luatz"
+
+-- We do this a few times ==> Convert a timestamp to timetable and normalise
+local function ts2tt ( ts )
+       return luatz.timetable.new_from_timestamp ( ts ):normalise ( )
+end
+
+-- Get the current time in UTC
+local utcnow = luatz.time ( )
+local now = ts2tt ( utcnow )
+print ( now , "now (UTC)" )
+
+-- Get a new time object 6 months from now
+local x = now:clone ( )
+x.month = x.month + 6
+x:normalise ( )
+print ( x , "6 months from now" )
+
+-- Find out what time it is in Melbourne at the moment
+local melbourne = luatz.get_tz ( "Australia/Melbourne" )
+local now_in_melbourne = ts2tt ( melbourne:localise ( utcnow ) )
+print ( now_in_melbourne , "Melbourne" )
+
+-- Six months from now in melbourne (so month is incremented; but still the same time)
+local m = now_in_melbourne:clone ( )
+m.month = m.month + 6
+m:normalise ( )
+print ( m , "6 months from now in melbourne" )
+
+-- Convert time back to utc; a daylight savings transition may have taken place!
+-- There may be 2 results, but for we'll ignore the second possibility
+local c1 , c2 = melbourne:utctime ( m:timestamp( ) )
+print ( ts2tt ( c1 ) , "6 months from now in melbourne converted to utc" )