Back and forth by UTC in lua

I am having problems converting the lua date to a timestamp, and then having it return to its original date. It does not work with UTC dates, but with UTC.

Currently my sample code is:

local dt1 = os.date( "*t" ); print( dt1.hour ); local dt2 = os.date( "*t", os.time( dt1 ) ); print( dt2.hour ); print( "-=-=-" ); local dt1 = os.date( "!*t" ); print( dt1.hour ); local dt2 = os.date( "!*t", os.time( dt1 ) ); print( dt2.hour ); local dt2 = os.date( "*t", os.time( dt1 ) ); print( dt2.hour ); 

Which gives the result:

 12 12 -=-=- 10 9 11 

So, in the second part, after getting the timestamp using os.time( os.date( "!*t" ) ) ; I do not know how to get the starting date. What am I doing wrong?

+3
date lua utc
source share
3 answers

Working with date tables in Lua

Let dt be the "date table".
For example, the value returned by os.date("*t") is a "date table".


How to normalize the "date table"
For example, after adding 1.5 hours to the current time
local dt = os.date("*t"); dt.min = dt.min + 90
you need to normalize the table fields.

 function normalize_date_table(dt) return os.date("*t", os.time(dt)) end 

This function returns a new date table that is equivalent to its dt argument, regardless of the value of the dt contents: whether it contains local or GMT time.


How to convert Unix time to "local date table"

 dt = os.date("*t", ux_time) 

How to convert Unix time to "GMT date" table

 dt = os.date("!*t", ux_time) 

How to convert a "local date table" to Unix time

 ux_time = os.time(dt) 

How to convert "GMT date table" to Unix time

 -- for this conversion we need precalculated value "zone_diff" local tmp_time = os.time() local d1 = os.date("*t", tmp_time) local d2 = os.date("!*t", tmp_time) d1.isdst = false local zone_diff = os.difftime(os.time(d1), os.time(d2)) -- zone_diff value may be calculated only once (at the beginning of your program) -- now we can perform the conversion (dt -> ux_time): dt.sec = dt.sec + zone_diff ux_time = os.time(dt) 
+5
source share

I found the only solution for this date, time and timezone to work is to use my own c-build method:

For example, to get a string in the desired format, I use this method:

 static int idateformat(lua_State *L) { time_t t0 = (time_t) lua_tonumber(L,1); const char* ptrn = lua_tostring(L,2); const char *tz_value = lua_tostring(L,3); int isinenv = 0; if(getenv("TZ") != NULL){ isinenv = 1; } char old_tz[64]; if (isinenv == 1) { strcpy(old_tz, getenv("TZ")); } setenv("TZ", tz_value, 1); tzset(); char new_tz[64]; strcpy(new_tz, getenv("TZ")); struct tm *lt = localtime(&t0); //"%Y-%m-%d %H:%M:%S" char buffer[256]; strftime(buffer, sizeof(buffer), ptrn, lt); if (isinenv == 1) { setenv("TZ", old_tz, 1); tzset(); } //printf("%ld = %s (TZ=%s)\n", (long)t0, buffer, new_tz); lua_pushstring(L, buffer); return 1; } 

Only so I would suggest.

+1
source share

You are not doing anything wrong.

Lua uses the ISO C and POSIX date and time functions. In particular, os.time uses mktime , which interprets the input structure according to the current time zone setting in the TZ environment variable. Unfortunately, these standards do not provide a function that interprets the input structure in accordance with GMT / UTC.

+1
source share

All Articles