Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

os.date issues with KopiLua #4

Open
agladysh opened this Issue Jun 25, 2012 · 4 comments

Comments

Projects
None yet
3 participants

(I'm posting this on behalf of @gfoot who run the tests and will be able to provide details.)

Running lua-TestMore against KopiLua (http://www.ppl-pilot.com/kopilua.aspx).

RunTestMoreTest("308-os.t") (KopiLuaTest.Tests.TestMore.RunTestMoreTest("308-
os.t"))
      not ok 3 - function date
      #     Failed test (308-os.t at line 43)
      #          got: 2012
      #     expected: 1970
      not ok 4
      #     Failed test (308-os.t at line 44)
      #          got: 6
      #     expected: 1
      not ok 5
      #     Failed test (308-os.t at line 45)
      #          got: 23
      #     expected: 1
      not ok 6
      #     Failed test (308-os.t at line 46)
      #          got: 21
      #     expected: 0
      not ok 7
      #     Failed test (308-os.t at line 47)
      #          got: 53
      #     expected: 0
      not ok 8
      #     Failed test (308-os.t at line 48)
      #          got: 34
      #     expected: 0
      not ok 9
      #     Failed test (308-os.t at line 49)
      #          got: 6
      #     expected: 5
      not ok 10
      #     Failed test (308-os.t at line 50)
      #          got: 175
      #     expected: 1

I'm not sure if these are implementation bugs or lua-TestMore issues (need to study the code more closely), reporting anyway, just in case. Feel free to return this issue back to me to do the homework. :-)

I can confirm that this is a KopiLua issue - the second parameter to os.date was not passed. My latest version does not have the problem.

gfoot commented Jul 20, 2012

It was a while ago that I was looking at this, but I think there were two issues here - one was that the second argument was not respected, but the other is that lua-TestMore assumes that date values are seconds-since-1970, when the Lua documentation doesn't make any such guarantee. So although KopiLua doesn't implement the functions properly, the test suite is also being too picky.

That's a good point. But the Lua authors are Unix people, and they always assume the world starts at 1970 ;)

My implementation now looks like this:

private static int os_date (lua_State L)
{
    CharPtr s = luaL_optstring (L, 1, "%c");
    int tm = luaL_optint (L, 2, -1);
    DateTime stm;
        if (tm > -1) {
        stm = new DateTime (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
           stm.AddSeconds (tm);
    } else {
        stm = DateTime.Now;
    }
    if (s[0] == '!') {  /* UTC? */
        stm = stm.ToUniversalTime();
        s.inc ();  /* skip `!' */
    }
       if (strcmp(s, "*t") == 0) {
       lua_createtable(L, 0, 9);  /* 9 = number of fields */
      setfield(L, "sec", stm.Second);
      setfield(L, "min", stm.Minute);
      setfield(L, "hour", stm.Hour);
      setfield(L, "day", stm.Day);
      setfield(L, "month", stm.Month);
      setfield(L, "year", stm.Year);
      setfield(L, "wday", ((int)(stm.DayOfWeek))+1); // enum starts at zero
      setfield(L, "yday", stm.DayOfYear);
      setboolfield(L, "isdst", stm.IsDaylightSavingTime() ? 1 : 0);
     }
    else {
      luaL_error(L, "strftime not implemented yet"); // todo: implement this - mjf
    }             
}

That +1 for getting day of week is important ;)

strftime would be useful, I suppose, but in the CLR world there are always other ways to do that....

steve d.

Sorry about the formatting - I'll fork KopiLua so I can pass changes back to the maintainer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment