]> git.madduck.net Git - etc/awesome.git/blob - examples/os_date.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:

README: Add feature list, Usage section
[etc/awesome.git] / examples / os_date.lua
1 --[[
2 Re-implementation of `os.date` from the standard lua library
3 ]]
4
5 local gettime = require "luatz.gettime".gettime
6 local new_from_timestamp = require "luatz.timetable".new_from_timestamp
7 local get_tz = require "luatz.tzcache".get_tz
8
9 local function os_date ( format_string , timestamp )
10         format_string = format_string or "%c"
11         timestamp = timestamp or gettime ( )
12         if format_string:sub ( 1 , 1 ) == "!" then -- UTC
13                 format_string = format_string:sub ( 2 )
14         else -- Localtime
15                 timestamp = get_tz ( ):localise ( timestamp )
16         end
17         local tt = new_from_timestamp ( timestamp )
18         if format_string == "*t" then
19                 return tt
20         else
21                 return tt:strftime ( format_string )
22         end
23 end
24
25 return os_date