This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
DannySMc's profile picture

Real time with timezone support

Started by DannySMc, 16 June 2015 - 12:42 PM
DannySMc #1
Posted 16 June 2015 - 02:42 PM
Hey guys,

It isn't much as it is part of a bigger API, but I know a lot of people would love to have a real time and date, so I made one, this is part of a bigger API, but I have adapted it to be used on its own.

The functions are:

irltimezones()
This will give you a list of usable timezones, just give this to the irltime / irltimeasync function as an argument and it will get that time, if you leave it blank by default it will get Europe/London, which is UK GMT 0.

irltimeasync(timezone)
This just used http.request instead so you can use os.pullEvent() to get the time.

irltime(timezone)
This stops the program and waits for the time!

irltimeauto()
This will use your IP address to get your timezone and get it back automatically, NOTE: if you are on a server, it is the server that sends the request not your client.

locate()
Gets your position, so country, region, regionname, timeszone, an example is below.


http://pastebin.com/t51fFS3r

Have a go guys :D/>/>/>

It returns a table so you can use

tablename["date"] for full date or tablename
["time"] for time or

tablename["datetime"] for the whole date or you can get the day, month, year, hours, minutes, seconds, just output the table and you can see it :D/>/>/>

Here is what it looks like when it comes back:
{
	datetime="2015-06-17 10:42:17",
	date="2015-06-17",
        time="10:42:17",
	year="2015",
	month="06",
	day="17",
	hours="10",
	minutes="42",
	seconds="17",
}

Example of locate() code:
{
    country=United Kingdom, 
    countrycode=GB, 
    timezone=Europe/London, 
    city=Bedford, 
    regionname=England, 
    region=ENG
}
So yeah any questions leave them here or PM me :D/>/>/>
Edited on 17 June 2015 - 03:38 PM
Creator #2
Posted 16 June 2015 - 03:02 PM
The idea is very nice :P/>

The description about the table it return is a bit unclear. Maybe I actually got it. ;)/>

I believe there was something similar some time ago.
DannySMc #3
Posted 16 June 2015 - 03:39 PM
The idea is very nice :P/>/>

The description about the table it return is a bit unclear. Maybe I actually got it. ;)/>/>

I believe there was something similar some time ago.

Sorry not very good at explaining:(((

And yeah not seen one but I made one for a player on my server :P/>
Creator #4
Posted 16 June 2015 - 03:52 PM
The idea is very nice :P/>/>

The description about the table it return is a bit unclear. Maybe I actually got it. ;)/>/>

I believe there was something similar some time ago.

Sorry not very good at explaining:(((

And yeah not seen one but I made one for a player on my server :P/>

That's ok. In contrast, you are very good at coding ;)/>
DannySMc #5
Posted 16 June 2015 - 04:19 PM
The idea is very nice :P/>/>/>

The description about the table it return is a bit unclear. Maybe I actually got it. ;)/>/>/>

I believe there was something similar some time ago.

Sorry not very good at explaining:(((

And yeah not seen one but I made one for a player on my server :P/>/>

That's ok. In contrast, you are very good at coding ;)/>/>

Well thank you very much:D
biggest yikes #6
Posted 17 June 2015 - 04:57 PM
Is there a function to get the time zone the user is currently in?
Edited on 17 June 2015 - 02:57 PM
DannySMc #7
Posted 17 June 2015 - 05:10 PM
Is there a function to get the time zone the user is currently in?

You have literally guessed what I am doing now :P/> I am currently using a IP tracer to be able to get your current time zone, without having to specify, this will be in the next update.
Creator #8
Posted 17 June 2015 - 05:26 PM
This exists in the tool I was talking about.
DannySMc #9
Posted 17 June 2015 - 05:35 PM
Hey there I have implemented this now :D/>
Check original post, it is just irltimeauto() :P/>

Just updated the pastebin link :P/>

Just to say I have included the timezone finder which gets your country, timezone, region etc, the irltimeauto() gets your timezone automatically and then returns the time, locate() just gets the whereabouts of where the ip address is coming from.
Creeper9207 #10
Posted 26 June 2015 - 01:56 AM
i actually made a location api for this :L ya beat me to it XD
Geforce Fan #11
Posted 26 June 2015 - 05:36 AM
I've done something a little bit similar, more relating to the function that can get the time. Since this is in my OS, it's being connstantly ran as a daemon and any program can fetch the time.
I would be a little weary on what you do with timezones, this video made me not want to mess with them.
Code
local function getRealTime(dif,forward)
  if forward then
	dif = dif.."+hours+from+now"
  else
	dif=dif.."+hours+ago"
  end
  local url = "http://chronic.herokuapp.com/utc/"
  --^ this is the best thing. ever. returns in plain text, and does not need a developer key. It makes my life so much easier.

  local utc = http.get(url..dif)

  if not utc then return false end
  utc = utc.readAll()
  if not utc then return false end
  --print(utc)
  --get these into seperate variables
  local year = utc:sub(1,utc:find("-")-1)
  --print(utc:find"-")
  --print(year)
  local month = utc:sub(utc:find"-"+1,utc:find("-",utc:find"-"+1)-1)
  month=month:gsub(" ","")
  --print(month.."e")
  local day = utc:sub(utc:find("-",utc:find"-"+1)+1,utc:find(" "))
  day=day:gsub(" ","")
  --print(day.."e")
  local clocktime = utc:sub(utc:find" "+1,utc:find(" ",utc:find" "+1)-1)
  local pm
  if tonumber(clocktime:sub(1,2)) >= 13 and (not osStuff.miliTime) then
	clocktime= tostring(tonumber(clocktime:sub(1,2))-12)..string.sub(clocktime,3,#clocktime)
	pm=true
  end
  clocktime=clocktime:gsub(" ","")
  --print(clocktime.."e")
  return year,month,day,clocktime,pm
end
local function addsecond(clocktime)
  local seconds = clocktime:sub(#clocktime-1,#clocktime)
  seconds:gsub(":"," ")
  --print(seconds)
  seconds =seconds+1
  if seconds > 59 then
	--instead of doing any further time calculations, just ask the server again. So, this means the server is asked every minute for the time.
	return false
  end

  local small
  if seconds <10 then
	seconds = tostring(seconds)
	small = true
	seconds = "0"..seconds
  end
  --print("c "..clocktime:sub(1,#clocktime-2))
  return clocktime:sub(1,#clocktime-2)..seconds
end
local year,month,day,clocktime,pm
local function timekeeping()
  while true do
	if not clocktime then --if the server went down, we'll keep trying every second... hopefully we're not the cause of its downtime, or it's not going back up anytime soon, lol.
	  year,month,day,clocktime,pm=getRealTime(osStuff.settings.timeDif,osStuff.settings.timeDifPlus)
	else
	  local t = addsecond(clocktime)
	  if t then
		clocktime=t
	  else
		year,month,day,clocktime,pm=getRealTime(osStuff.settings.timeDif,osStuff.settings.timeDifPlus)
	  end
	  --print("Year "..year.." on the "..day.." of "..month..", "..clocktime)
	  os.queueEvent"clocktick"
	end
	sleep(1)
  end  
end
function osStuff.getTime() return year,month,day,clocktime end
function osStuff.seperateTime(_d)
  return string.sub(_d,1,string.find(_d,",")-1),string.sub(_d,string.find(_d,",")+2,string.find(_d,",",string.find(_d,",")+1)-1),string.sub(_d,string.find(_d,",",string.find(_d,",")+1)+2,#_d)
end

It certainly helps with console logs, as I am able to keep track of when things happened in the console. Further more, I use it to put time stamps on when files where created, opened, or modified. What this code does is fetch the time every time it rolls over from :59, then start counting up again. This means I never have to deal with adding hours or anything.
One I get a settings app, I plan to simply ask the user what time it is now to figure out their time zone. It does not yet handle daylight savings.
Edited on 26 June 2015 - 03:38 AM
DannySMc #12
Posted 26 June 2015 - 08:54 AM
I've done something a little bit similar, more relating to the function that can get the time. Since this is in my OS, it's being connstantly ran as a daemon and any program can fetch the time.
I would be a little weary on what you do with timezones, this video made me not want to mess with them.
Code
local function getRealTime(dif,forward)
  if forward then
	dif = dif.."+hours+from+now"
  else
	dif=dif.."+hours+ago"
  end
  local url = "http://chronic.herokuapp.com/utc/"
  --^ this is the best thing. ever. returns in plain text, and does not need a developer key. It makes my life so much easier.

  local utc = http.get(url..dif)

  if not utc then return false end
  utc = utc.readAll()
  if not utc then return false end
  --print(utc)
  --get these into seperate variables
  local year = utc:sub(1,utc:find("-")-1)
  --print(utc:find"-")
  --print(year)
  local month = utc:sub(utc:find"-"+1,utc:find("-",utc:find"-"+1)-1)
  month=month:gsub(" ","")
  --print(month.."e")
  local day = utc:sub(utc:find("-",utc:find"-"+1)+1,utc:find(" "))
  day=day:gsub(" ","")
  --print(day.."e")
  local clocktime = utc:sub(utc:find" "+1,utc:find(" ",utc:find" "+1)-1)
  local pm
  if tonumber(clocktime:sub(1,2)) >= 13 and (not osStuff.miliTime) then
	clocktime= tostring(tonumber(clocktime:sub(1,2))-12)..string.sub(clocktime,3,#clocktime)
	pm=true
  end
  clocktime=clocktime:gsub(" ","")
  --print(clocktime.."e")
  return year,month,day,clocktime,pm
end
local function addsecond(clocktime)
  local seconds = clocktime:sub(#clocktime-1,#clocktime)
  seconds:gsub(":"," ")
  --print(seconds)
  seconds =seconds+1
  if seconds > 59 then
	--instead of doing any further time calculations, just ask the server again. So, this means the server is asked every minute for the time.
	return false
  end

  local small
  if seconds <10 then
	seconds = tostring(seconds)
	small = true
	seconds = "0"..seconds
  end
  --print("c "..clocktime:sub(1,#clocktime-2))
  return clocktime:sub(1,#clocktime-2)..seconds
end
local year,month,day,clocktime,pm
local function timekeeping()
  while true do
	if not clocktime then --if the server went down, we'll keep trying every second... hopefully we're not the cause of its downtime, or it's not going back up anytime soon, lol.
	  year,month,day,clocktime,pm=getRealTime(osStuff.settings.timeDif,osStuff.settings.timeDifPlus)
	else
	  local t = addsecond(clocktime)
	  if t then
		clocktime=t
	  else
		year,month,day,clocktime,pm=getRealTime(osStuff.settings.timeDif,osStuff.settings.timeDifPlus)
	  end
	  --print("Year "..year.." on the "..day.." of "..month..", "..clocktime)
	  os.queueEvent"clocktick"
	end
	sleep(1)
  end  
end
function osStuff.getTime() return year,month,day,clocktime end
function osStuff.seperateTime(_d)
  return string.sub(_d,1,string.find(_d,",")-1),string.sub(_d,string.find(_d,",")+2,string.find(_d,",",string.find(_d,",")+1)-1),string.sub(_d,string.find(_d,",",string.find(_d,",")+1)+2,#_d)
end

It certainly helps with console logs, as I am able to keep track of when things happened in the console. Further more, I use it to put time stamps on when files where created, opened, or modified. What this code does is fetch the time every time it rolls over from :59, then start counting up again. This means I never have to deal with adding hours or anything.
One I get a settings app, I plan to simply ask the user what time it is now to figure out their time zone. It does not yet handle daylight savings.

Thanks for that.. Err this uses timezones and supports daylight savings, its pretty simple, it sets the timezone in PHP, then gets the current time, splitting it up into bits so you can use it with whatever you need instead of having just one time string…

I have no use for this currently, but when I get the hang of multitasking I will probably bring it out a new OS. Who knows what I am gonna call it xD

i actually made a location api for this :L ya beat me to it XD

Yeah I saw, Yours came out before mine as a player on my server was obsessed with it… xD But yeah I liked the idea and another player on my server asked if we could have an API for it, so I made one :P/> Posted it here ;)/> It's quite useful, my API uses the same location API that yours does to get your timezone automatically.