Skip to content

Commit

Permalink
remove cruft from util.time and prepare for v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
richardhundt committed Apr 5, 2014
1 parent dbf94d6 commit fa210a6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 77 deletions.
3 changes: 3 additions & 0 deletions lib/sys/ffi/posix.shn
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ typedef struct timezone {
time_t time(time_t *tloc);
int gettimeofday(timeval_t *tp, timezone_t *tzp);
size_t strftime(char *, size_t, const char *, tm_t *);
char * strptime(const char *, const char *, tm_t *);


char *asctime(tm_t *);
clock_t clock(void);
Expand All @@ -53,6 +55,7 @@ double difftime(time_t, time_t);
tm_t *getdate(const char *);
tm_t *gmtime(time_t *);
tm_t *localtime(time_t *);
tm_t *localtime_r(const time_t *, tm_t *);
time_t mktime(tm_t *);

int pipe(int *);
Expand Down
87 changes: 11 additions & 76 deletions lib/util/time.shn
Original file line number Diff line number Diff line change
@@ -1,89 +1,24 @@
import ffi, C from "sys.ffi"

class TimeVal
ctype = ffi::typeof('timeval_t')
function self.__apply(time = 0.0)
self = ctype()
if time then
integ, fract = math::modf(tonumber(time))
self.tv_sec = integ
self.tv_usec = fract * 1000000
end
return self
end
get sec()
return self.tv_sec
end
get usec()
return self.tv_usec
end
toDate()
Date(self.sec)
end
ffi::metatype(ctype, self)
end

class TimeZone
ctype = ffi::typeof('timezone_t')
function self.__apply(...)
return ctype(...)
end
get minuteswest()
return self.tz_minuteswest
end
get dsttime()
return self.tz_dsttime
end
ffi::metatype(ctype, self)
end

class TimeSpec
ctype = ffi::typeof('timespec_t')
function self.__apply(...)
return ctype(...)
end
get sec()
return self.tv_sec
end
get nsec()
return self.tv_nsec
end
toDate()
return Date(self.sec)
end
ffi::metatype(ctype, self)
end

class Date
ctype = ffi::typeof('tm_t')

function self.getTimeOfDay()
tv = TimeVal()
tz = TimeZone()
ok = C::gettimeofday(tv, tz)
if ok == 0 then
return tv, tz
end
self(time = C::time(null))
clock = ffi::new('time_t[1]', time)
self.tm = C::localtime(clock)
end

function self.__apply(time = C::time(ffi::new('time_t[1]')))
self = ctype()
if not ffi::istype('time_t*', time) then
temp = ffi::new('time_t[1]')
temp[0] = time
time = temp
end
C::localtime(time, self)
return self
end

format(fmt, len = 256)
local iso8601 = '%Y-%m-%dT%H:%M:%S%z'
format(fmt = iso8601, len = 256)
buf = ffi::new('char[?]', len)
C::strftime(buf, len, fmt, self)
C::strftime(buf, len, fmt, self.tm)
return ffi::string(buf)
end

ffi::metatype(ctype, self)
parse(str is String, fmt = iso8601)
tm = ffi::new('tm_t')
C::strptime(str, fmt, tm)
self.tm = tm
end
end

module Clock
Expand Down
2 changes: 2 additions & 0 deletions src/core/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ local Object

local export = { }

export._VERSION = "0.1.0"

local type, tonumber, tostring = _G.type, _G.tonumber, _G.tostring
local getmetatable, setmetatable = _G.getmetatable, _G.setmetatable
local xpcall = _G.xpcall
Expand Down
4 changes: 3 additions & 1 deletion src/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ local function interactive(env)
end

local function print_version()
io.stdout:write("Shine 0.0.2 -- Copyright (C) 2013-2014 Richard Hundt.\n")
local core = require("core")
local info = "Shine %s -- Copyright (C) 2013-2014 Richard Hundt.\n"
io.stdout:write(string.format(info, core._VERSION))
end

local function print_usage()
Expand Down

0 comments on commit fa210a6

Please sign in to comment.