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

An Irl Time System

Started by jay5476, 25 July 2013 - 03:34 AM
jay5476 #1
Posted 25 July 2013 - 05: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
LBPHacker #2
Posted 25 July 2013 - 06: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,}"));
?>
jay5476 #3
Posted 25 July 2013 - 06: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
albrat #4
Posted 25 July 2013 - 07:09 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
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
LBPHacker #5
Posted 25 July 2013 - 07: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
jay5476 #6
Posted 25 July 2013 - 07: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?
LBPHacker #7
Posted 25 July 2013 - 07:57 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?
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