diff --git a/realtime/OpenOS/usr/lib/realtime.lua b/realtime/OpenOS/usr/lib/realtime.lua new file mode 100644 index 0000000..033c9ad --- /dev/null +++ b/realtime/OpenOS/usr/lib/realtime.lua @@ -0,0 +1,22 @@ +local computer = require "computer" +local realtime = {} +realtime.epoch, realtime.uptime, realtime.offset = 0, computer.uptime(), 0 + +function realtime.update(epoch,uptime,offset) + if type(epoch) ~= "number" or type(uptime) ~= "number" then + return false + end + realtime.epoch, realtime.uptime, realtime.offset = epoch, uptime, offset or realtime.offset + return true +end + +function realtime.time(utc) + local ofs = realtime.offset + if utc then + ofs = 0 + end + local ut = computer.uptime() + return (realtime.epoch+(ut-realtime.uptime))+ofs +end + +return realtime diff --git a/realtime/OpenOS/usr/man/realtime b/realtime/OpenOS/usr/man/realtime new file mode 100644 index 0000000..2e65182 --- /dev/null +++ b/realtime/OpenOS/usr/man/realtime @@ -0,0 +1,13 @@ +# realtime + +realtime is a library for OpenOS providing a reasonably accurate current real-world time, given the right input (see realtime-sync and realtime-relay). + +## API + +### realtime.time(*utc*) + +Returns the current time, either respecting the current offset, or if *utc* is set, with no offset. + +### realtime.update(*epoch*, *uptime*, *offset*) + +Updates the currently known real time, with the time as *epoch*, measured at *uptime*. Optionally, an offset can be provided.