Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
Shuudoushi

Real life time (at least server time) in OC

Question

Description:

I've been racking my brain for the past few days trying my best to figure out a way of getting the real life time in OC.

function os.dateRL(format)
  if not fs.get("/").isReadonly() then
    local time = io.open("/tmp/.time", "w")
      time:write()
        time:close()
        os.sleep(0.01)
      return os.date(format, fs.lastModified("/tmp/.time") / 1002.7)
  else
    return os.date(format)
  end
end

That is the closest I've gotten thus far. The issue with this bit of code is that it thinks it's 3 days behind what it really is, and would require constant update to stay on target-ish...

 

There is no deadline on this, it's mostly a vanity project for the OS I've been working on, but it would still greatly increase the accuracy of the user logs of my login system, as right now I'm just using 'os.date("%F %X")'.

Link to post
Share on other sites

5 answers to this question

Recommended Posts

  • 0

I think os.date might be calculating things incorrectly, as it's designed for the minecraft world, not the real world. You could use another implementation top get a better value. (Also, while not an ideal option, if you have an internet you could use a web api (I've done that with computer craft))

Link to post
Share on other sites
  • 0
local fs = require("filesystem")

function os.dateRL(format)
  if not fs.get("/").isReadOnly() then
    local time = io.open("/tmp/.time", "w")
      time:write()
        time:close()
        os.sleep(0.01)
      return os.date(format, fs.lastModified("/tmp/.time") / 1002.7)
  else
    return os.date(format)
  end
end

It works.

Just require filesystem and "only" in isReadOnly starts with an uppercase letter

Link to post
Share on other sites
  • 0

That code is a snippet from where the function lives, which is in /boot/02_os.lua, that's why filesystem wasn't required. And as I said in the OP, the outputted date is off a bit, not sure why... This code is mostly being tested on a server, of which I know the timezone in which the server is in and the local time of said server. Also, without that bit of math, the year gets spit out as 45000 something...

Link to post
Share on other sites
  • 0

You might be able to convert this implementation to lua. Also, why are you checking if "/" is readonly rather than checking /tmp ?

It was faster to type out at the time, if I can get it working as the wiki has lead me to believe it should, it will just be looking to see if /tmp is read-only.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.