Jump to content




An Irl Time System


  • You cannot reply to this topic
6 replies to this topic

#1 jay5476

  • Members
  • 289 posts

Posted 25 July 2013 - 04:34 AM

okay guys I have tried myself but countlessly failed to succeed in making a program that can tell real life time I was wondering if there wasa simple code out there for it our someone could possibly try, ty in advanced

#2 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 25 July 2013 - 05:10 AM

HTTP-GET the contents of the following website (set it up for you):
http://artemix.hu/cctime.php

It echoes the time in a serialized table. Unserialize the response and there you have a table with the indexes:
  • h: the current hour
  • m: the current minute
  • s: the current second
It will actually show the Hungarian time (timezone: Europe/Budapest), so if you want to set a custom timezone, choose one of the available timezones from http://php.net/manual/en/timezones.php, and set the timezone parameter of the HTTP request to the timezone you chose. Eg.
http://artemix.hu/cctime.php?timezone=Europe/London

EDIT: In case you want to set up your own webserver, here's the piece of code cctime.php uses:
<?php
if (isset($_GET["timezone"])) date_default_timezone_set($_GET["timezone"]);
echo(date("{[\"\h\"]=H,[\"\m\"]=i,[\"\s\"]=s,}"));
?>


#3 jay5476

  • Members
  • 289 posts

Posted 25 July 2013 - 05:44 AM

So would this code work
table = http.get(http://artemix.hu/cctime.php)
time = textutils.unserialize(table)
for i = 1, #time do
write(time[i]) write(".")
end


#4 albrat

  • Members
  • 162 posts
  • LocationA Chair

Posted 25 July 2013 - 06:09 AM

View Postjay5476, on 25 July 2013 - 05:44 AM, said:

So would this code work
table = http.get(http://artemix.hu/cctime.php)
time = textutils.unserialize(table)
for i = 1, #time do
write(time[i]) write(".")
end
yes, but it would only print the time once.

while true do
table = http.get(http://artemix.hu/cctime.php)
time = textutils.unserialize(table)
for i = 1, #time do
write(time[i]) write(".")
end
sleep(0.4) -- so we dont get a yield error. and update is still going to happen at least once a second.
end


#5 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 25 July 2013 - 06:25 AM

You'll need some help I think...
  • Strings are enclosed by quotation marks, ( " or 0x22 or 34), so "http://artemix.hu/cctime.php"
  • Use locals. That way the code won't mess other codes up. Use locals even if there is no other code to mess up.
  • The serialized table doesn't contain numbers for nothing, they are there to be incremented, not to be fetched in every second.
  • http.get returns a HTTP Response Handle, not a string. Treat it as such.
  • And #time will return 0, since the table has no numberic indexes.
A startup program should look like this (using my website):
if not http then error("no http") end
local httpResponseHandle = http.get("http://artemix.hu/cctime.php?timezone=FOR_GODS_SAKE_SET_THIS_TO_SOMETHING")
if not httpResponseHandle then error("error fetching time") end
local timeTable = textutils.unserialize(httpResponseHandle.readAll())
httpResponseHandle.close()
local hour = timeTable.h
local minute = timeTable.m
local second = timeTable.s

local interval = 1
while true do
    sleep(interval)
    second = second + interval
    if not (second < 60) then
        second = second - 60
        minute = minute + 1
    end
    if not (minute < 60) then
        minute = minute - 60
        hour = hour + 1
    end
    if not (hour < 24) then
        hour = hour - 24
    end
    print(string.format("%02d:%02d:%02d", hour, minute, second))
end


#6 jay5476

  • Members
  • 289 posts

Posted 25 July 2013 - 06:41 AM

Ok, I am not familiar with the http api so i thought I'd try and make a code i thought would work before asking for a code. So by my understanding only at the start the computer will get the time then the computer will handle the calculations?

#7 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 25 July 2013 - 06:57 AM

View Postjay5476, on 25 July 2013 - 06:41 AM, said:

Ok, I am not familiar with the http api so i thought I'd try and make a code i thought would work before asking for a code. So by my understanding only at the start the computer will get the time then the computer will handle the calculations?
Yup. But as I mentioned, the timezone will be Europe/Budapest by default. Replace the FOR_GODS_SAKE_SET_THIS_TO_SOMETHING part in the code with a valid timezone from here: http://php.net/manual/en/timezones.php





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users