tieske / date Goto Github PK
View Code? Open in Web Editor NEWDate & Time module for Lua 5.x
Home Page: https://tieske.github.io/date/
License: MIT License
Date & Time module for Lua 5.x
Home Page: https://tieske.github.io/date/
License: MIT License
Consider code below:
local d = date(2011, 1, 1):setisoweeknumber(20, 1)
print(d)
For some reason the resulting year is 2010, not 2011 as expected! It's easy to work around this simply by doing :setisoyear(2011, 20, 1)
. Maybe you need to do something similar at the method?
Hi.
First day of week can be sunday or monday. Are possible define it on library (or object)?
Maybe interesting have a minimal support to day of week and months.
...
local now = date(false, 'pt-br' or { table with values });
print( now:fmt(%A") ) -> "Terça-feira" instead "Tuesday"
...
$ lua-5.1 -v
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
$ lua-5.1 test.lua
20170101 2017-01-01T00:00:00
20170201 2017-02-01T00:00:00
20173131 2019-07-31T00:00:00
$ cat test.lua
date = require 'date'
t = {
'20170101',
'20170201',
'20173131' --- proposital 'invalid' date
}
for i=1, #t do
x = t[i];
print( x, date(x):fmt('${iso}') );
end
According to the documentation https://tieske.github.io/date/
Section 7.2. Supported MetaMethods
The a < b operation.
Returns true if date value of a is later than date value of b. a > b is equivalent to b < a.
Luckily it is implemented the intuitive way, but the documentation seems to be incorrect.
local A = date(2021, 7, 1)
local B = date(2021, 7, 2)
print(A < B)
prints true
which is correct.
and now while writing this issue I might understand what is going on
the issue is the word "later" in the description. For me "later" means "after", a day in the future, like in the sentence "at a later point in time".
I think the word used should be "older".
The a < b operation.
Returns true if date value of a is older than date value of b. a > b is equivalent to b < a.
Would you agree?
Thank you
marco
I think there is an ambiguity in the standard library that makes it impossible to handle user-supplied dates with time zones.
The docs don't say whether date("some date")
parses to a date that is always in UTC or whether it parses to a date that is always local. Sometimes the date
function appears to return UTC dates when given a time zone:
th> date("2016-01-05T23:14:33Z")
Tue Jan 05 2016 23:14:33.000000000
th> date("2016-01-05T18:14:33-5")
Tue Jan 05 2016 23:14:33.000000000
th> date("2016-01-05T18:14:33 EST")
Tue Jan 05 2016 23:14:33.000000000
But sometimes, the output of date()
is clearly local:
th> date()
Tue Jan 05 2016 12:51:42.000000000
th> date("2016-01-05 12:51")
Tue Jan 05 2016 12:51:00.000000000
Perhaps I can make my code always use UTC dates by calling :toutc()
on the output of date()
. I thought this would make the issue go away: if the user supplied a local date, then it would be converted to UTC. Unfortunately, this doesn't work. This library doesn't store the actual time zone in the date object, so calling :toutc()
mutates dates that were already UTC:
th> date()
Tue Jan 05 2016 12:38:27.000000000
th> date():toutc()
Tue Jan 05 2016 17:38:29.000000000
th> date():toutc():toutc()
Tue Jan 05 2016 22:38:31.000000000
th> date():toutc():toutc():toutc()
Wed Jan 06 2016 03:38:32.000000000
This creates a problem. If I'm trying to parse a user-supplied date, I can't tell whether they specified a time zone or not. This means I can't know whether I need to call :toutc()
on the result. And because :toutc()
changes the time value, getting this wrong will cause me to incorrectly handle that date. Oy.
What should I do here?
I could not get this to work under Lua 5.2
I changed the references to 'arg' (which no longer seems to exist) to select(n,..)
eg
function date:__call(...)
local n = select("#",...)
if n > 1 then return (date_from(...))
elseif n == 0 then return (date_getdobj(false))
else local o, r = date_getdobj(select(1,...)); return r and o:copy() or o end
end
Hi. I tried to install luadate in my torch framework (luajit)
require 'date'
/opt/torch/share/lua/5.1/luarocks/loader.lua:113: error loading module 'date' from file '/opt/torch/share/lua/5.1/date.lua':
/opt/torch/share/lua/5.1/date.lua:338: invalid escape sequence near '"^(%d+)[/'
stack traceback:
[C]: in function 'loader'
/opt/torch/share/lua/5.1/luarocks/loader.lua:113: in function </opt/torch/share/lua/5.1/luarocks/loader.lua:110>
[C]: in function 'f'
[string "local f = function() return require 'date' en..."]:1: in main chunk
[C]: in function 'xpcall'
/opt/torch/share/lua/5.1/trepl/init.lua:553: in function </opt/torch/share/lua/5.1/trepl/init.lua:489>
http://rocks.moonscript.org is now the main module host for Lua modules. The current date module isn't owned by anyone. If you have an account on moonrocks I can move ownership to you so it's easy to upload new versions.
Integer seconds, offset is parsed:
> pcall(p, '2023-08-22T24:40:23-02:00')
true Wed Aug 23 2023 02:40:23.000000000
Decimal seconds, offset is not parsed:
> pcall(p, '2023-08-22T24:40:23.0-02:00')
true Wed Aug 23 2023 00:40:23.000000000
Hi Tieske,
We are looking for a library at Lua, which can parse duration at ISO8601.
For example, PT10S is 10 seconds. Is it luadate possible to parse duration? Thanks.
--- source
local date = dofile('date.lua')
x = '2018-31-02';
print( date(x):fmt('${iso}') );
--- output ---
$ lua-5.1 test.date.lua
2020-07-02T00:00:00
when create a date by date(true)
, set the utc
field to be true
.
when create a local date, set the utc
field to be false
.
tolocal
method check this field first, if utc
field is false
, tolocal
does nothing and return self, if utc
field is true
, tolocal
convert the date and time value to local time, and then set the utc
field of return value to be false
.
toutc
method check this field first, too. if utc
field is true
, toutc
does nothing and return self, if utc
field is false
, toutc
convert the date and time value to utc time, and then set the utc
field of return value to be true
.
...
code contains usage of deprecated arg.n
methods.
Hi.
lua-5.4.4 (vanilla) / gcc version 11.2.0 / cygwin 64-bit
When compile lua-5.4.4 with CFLAGS: -std=c99 -DLUA_COMPAT_APIINTCASTS=1 -DLUA_32BITS=1
$ ./lua -e "date=require 'date'; io.write( _VERSION, '\n', os.date(), '\n', date():fmt('%Y-%m-%dT%T'), '\n\n');"
Lua 5.4
Thu Mar 24 14:26:51 2022
2009-09-10T00:07:51
$ ./lua -e "date=require 'date'; io.write( _VERSION, '\n', os.date(), '\n', date():fmt('%Y-%m-%dT%T'), '\n\n');"
Lua 5.4
Thu Mar 24 14:31:12 2022
2022-03-24T14:31:12
At https://github.com/Tieske/date/blob/master/src/date.lua#L346
The setz is sending 2 parameters and only can receive one.
How do I get the timestamp from a date object?
Hi,
First of all, congratulations for this excellent module which greatly simplifies the use of dates in Lua.
I'm writing a timestamp -> iso8601 converter using this module and I'm having a problem when the clock switches from summer time to winter time.
I am in France and on October 29, 2023 we switch to winter time.
I wrote this little piece of code:
local date = require('date')
local ts1 = 1698537600 -- 2023-10-29T00:00:00Z or 2023-10-29T02:00:00+02:00
local ts2 = 1698541200 -- 2023-10-29T01:00:00Z or 2023-10-29T02:00:00+01:00
local d1, d2 = date(ts1), date(ts2)
local d1, d2 = date(ts1), date(ts2)
print("ts1 iso8601 UTC :", d1:fmt("${iso}Z"))
print("ts2 iso8601 UTC :", d2:fmt("${iso}Z"))
local d1local, d2local = date(ts1):tolocal(), date(ts2):tolocal()
print("ts1 iso8601 local:", d1local:fmt("${iso}") .. d1local:fmt("%z"))
print("ts2 iso8601 local:", d2local:fmt("${iso}") .. d2local:fmt("%z"))
And I get the following display::
ts1 iso8601 UTC : 2023-10-29T00:00:00Z --> good
ts2 iso8601 UTC : 2023-10-29T01:00:00Z --> good
ts1 iso8601 local: 2023-10-29T02:00:00+0100 --> bad
ts2 iso8601 local: 2023-10-29T02:00:00+0100 --> good
Is there a problem in the module or is it a problem using the module on my side?
Hey,
I was doing some research, but I still can't find out, how could I print the local hours in Miami, FL. GMT-4000
Because unfortunately, the server where I am running on located in Central Europe, in this case, I would just somehow set the hours to -6 I guess (local time is Budapest).
But I have no clue how to do that. Right now it just gets the current time. Also for some strange reason, the seconds ends without zero, so if the current seconds are 8 for example, it returns 20:25:8 instead of 20:25:08, but if that cant' be solved, I can live with that.
a = date()
h, m, s = a:gettime()
print(h.. ":"..m..":"..s)
Could you put me to the right way to get the correct timezone?
Hi! this library support SQL-like date/time/timestamp types?
Thanks!
l.lua
date = require "date"
macros = {}
function macros.dateFunc(dataObj, format)
local finds = { 'a', 'A', '[^%%]d', 'D', 'F', 'g', 'G', 'h', 'i', 'j', 'l', '[^%%]m', '[^%%]M', 'n', 'N', 'o', '[^%%]s', '[^%%]w', '[^%%]W', '[^%%]y', '[^%%]Y' }
local replaces = { '%%p', '%%p', '%%d', '%%a', '%%B', '%%I', '%%H', '%%I', '%%M', '%%d', '%%A', '%%m', '%%b', '%%m', '%%u', '%%G', '%%S', '%%w', '%%W', '%%y', '%%Y' }
for i = 1, #finds, 1 do
format = string.gsub(format, finds[i], replaces[i])
end
return date(dataObj):fmt(format)
end
return macros
test/l.test.lua
package.path = package.path .. ';../l.lua';
transfer = require('l.lua')
luaunit = require('luaunit')
TestDate = {}
function TestDate:testString()
local format = 'a A d D F g G h i j l m M n N o s w W y Y'
local expected = 'PM PM 19 Wed July 12 12 12 30 19 Wednesday 07 Jul 07 3 2017 15 3 29 17 2017'
luaunit.assertEquals(transfer.dateFunc("2017-7-19 12:30:15", format), expected)
end
-- end of table TestDate
os.exit(luaunit.LuaUnit.run())
I run lua ltest.lua
Error:
lua: error loading module 'date' from file '../l.lua':
../l.lua:8: too many C levels (limit is 200) in function at line 4 near 'i'
stack traceback:
[C]: in ?
[C]: in function 'require'
../l.lua:1: in main chunk
[C]: in function 'require'
../l.lua:1: in main chunk
[C]: in function 'require'
../l.lua:1: in main chunk
[C]: in function 'require'
../l.lua:1: in main chunk
[C]: in function 'require'
...
[C]: in function 'require'
../l.lua:1: in main chunk
[C]: in function 'require'
../l.lua:1: in main chunk
[C]: in function 'require'
../l.lua:1: in main chunk
[C]: in function 'require'
../l.lua:1: in main chunk
[C]: in function 'require'
ltest.lua:3: in main chunk
[C]: in ?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.