Timestamp without using.time()

Asked

Viewed 134 times

1

How to convert a date and time in your timestamp, but without using the os.time() in Lua? What is the expression to calculate? Is it possible?

Example:

data = "01/01/2015"
hora = "10:00:00"
  • timestamp since that origin?

  • Behold luatz.

1 answer

0

Well, according to [ https://en.wikipedia.org/wiki/Timestamp ] There are several Timestamp formats, if you already have the date and time information, as in the question, you can look for a format that fits this description. For example, ISO 8601 [ https://en.wikipedia.org/wiki/ISO_8601 ] defines that a date (day/month/year) should be represented in year-month-day form, you could use the string functions of the moon to separate the typed information and generate that Time Stamp. For example, let’s assume that you implement a split function as described in [ http://lua-users.org/wiki/SplitJoin ], you could generate your Timestamp so:

data = "04/02/2016" --Quatro de Janeiro de 2016
dataTable = split(data,"/")
--timeStamp = 2016-02-04 (Conforme ISO 8601)
timeStamp = dataTable[1] .. "-" ..dataTable[2] .. "-" ..dataTable[3]

The same idea can be applied to add the hours, minutes and seconds. However, if you want a Stamp team as it returns by os.time() (Seconds from a given date), it is strongly recommended to use a library, although it is possible to calculate, in a very rough way it would look like this:

anoZero=1960
ano=2016
mes = 2 + ((ano-anoZero)*12)
dias = 4 + (mes * 30) --Simplificação, o ideal seria calcular o numero de dias para cada mês
horas = 1 + (dias * 24)
minutos=29 + (horas*60)
segundos=minutos * 60 -- Numero de segundos desde 1960-01-01 00:00:00
  • only one detail: "split" is not native method of Lua language, has to be written or then used of some extene module (Penlight, Microlight, etc)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.