From 073b4c8333742320533a197f553980cc2f626011 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 18 Aug 2014 14:39:08 -0400 Subject: [PATCH] examples/os_date: Initial commit --- examples/os_date.lua | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 examples/os_date.lua diff --git a/examples/os_date.lua b/examples/os_date.lua new file mode 100644 index 0000000..0164780 --- /dev/null +++ b/examples/os_date.lua @@ -0,0 +1,25 @@ +--[[ +Re-implementation of `os.date` from the standard lua library +]] + +local gettime = require "luatz.gettime".gettime +local new_from_timestamp = require "luatz.timetable".new_from_timestamp +local get_tz = require "luatz.tzcache".get_tz + +local function os_date ( format_string , timestamp ) + format_string = format_string or "%c" + timestamp = timestamp or gettime ( ) + if format_string:sub ( 1 , 1 ) == "!" then -- UTC + format_string = format_string:sub ( 2 ) + else -- Localtime + timestamp = get_tz ( ):localise ( timestamp ) + end + local tt = new_from_timestamp ( timestamp ):normalise ( ) + if format_string == "*t" then + return tt + else + return tt:strftime ( format_string ) + end +end + +return os_date -- 2.39.5