unspecified cast when trying to format date/time

Mar 4, 2015 at 1:51 PM
hello,

the following code
        using (Lua luaEngine = new Lua()) {
                LuaGlobal lg = luaEngine.CreateEnvironment();
                LuaChunk chk = luaEngine.CompileChunk("print(os.date(\"%Y-%m-%d\", os.time()));", "test.lua", null);
                lg.DoChunk(chk);
        }
returns an "unspecified cast" exception on line 70 of LuaChunk.cs.

Ultimately os.time() should be replaced by a DateTime?. What do I do wrong ?

thank you in advance.

thierry
Mar 4, 2015 at 2:23 PM
I solve the problem by editing Lua.Libraries.cs, replacing (int) by (Int64):

line 928
                dt = time == null ? DateTime.UtcNow : dtUnixStartTime.AddSeconds((Int64)time).ToUniversalTime();
line 934
                dt = time == null ? DateTime.Now : dtUnixStartTime.AddSeconds((Int64)time).ToLocalTime();
Mar 4, 2015 at 2:39 PM
but the problem remains when the lua code is:
    print(os.date(\"%Y-%m-%d\", obj.SomeDate);
where obj.SomeDate is a .Net DateTime. It seems that a DateTime can't be unboxed to a long.

from here I can't see another way that using some reflection.
Mar 4, 2015 at 3:04 PM
right now I fixed the problem as follow:
            public static object date(string format, object time)
        {
            // Unix timestamp is seconds past epoch. Epoch date for time_t is 00:00:00 UTC, January 1, 1970.
            DateTime dt;

                if (time is DateTime) {
                    //convert then box to run following code
                    time = (Int64)((DateTime)time).Subtract(dtUnixStartTime).TotalSeconds;
                }

            var firstC = string.IsNullOrEmpty(format) ? (char?)null : format[0];
            if (firstC == '!')
            {
                // Date and time expressed as coordinated universal time (UTC).
                dt = time == null ? DateTime.UtcNow : dtUnixStartTime.AddSeconds((Int64)time).ToUniversalTime();
                format = format.Substring(1);
            }
            else
            {
                // Date and time adjusted to the local time zone.
                dt = time == null ? DateTime.Now : dtUnixStartTime.AddSeconds((Int64)time).ToLocalTime();
            }

            if (String.Compare(format, "*t", false) == 0)
            {
                LuaTable lt = new LuaTable();
                lt["year"] = dt.Year;
                lt["month"] = dt.Month;
                lt["day"] = dt.Day;
                lt["hour"] = dt.Hour;
                lt["min"] = dt.Minute;
                lt["sec"] = dt.Second;
                lt["wday"] = (int)dt.DayOfWeek;
                lt["yday"] = dt.DayOfYear;
                lt["isdst"] = (dt.Kind == DateTimeKind.Local ? true : false);
                return lt;
            }
            else
                return AT.MIN.Tools.ToStrFTime(dt, format);
        } // func date
Coordinator
Mar 5, 2015 at 10:30 AM
Your are right.
I have know idea, why I let this code through.

Fix on github

Is this ok?
And do you need a release version?
Mar 5, 2015 at 11:54 AM
thank you, the update is just fine.

No need for release, I'm plugged on git.

Thank you, nice work.