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

Fetch rl time/date. Open church only on Sunday? Activate virus on Wednesday? Log user intrusions with a rl date/time?

Started by libraryaddict, 24 April 2012 - 06:31 AM
libraryaddict #1
Posted 24 April 2012 - 08:31 AM
No one liked it so I removed it :)/>/>




Spoiler
Spoiler
function time(offset) -- Get the time, offset to set the time off by bla bla hours
  if not offset then error("Please enter a number to offset the time by. Use 0 if you dont want to offset it") end
  local Days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
  local Months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
  local DaysInMonths = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
  local res = http.get("http://nist.time.gov/timezone.cgi?UTC/s/0/")
  local Res = {}
  local DateFormat = ""
  local bla = ""
  local Date = ""
  local Time = ""
  local BlaDate = ""
  local BlaTime = ""
  local BlaDays = ""
  local BlaMonth = ""
  if res then
	while true do
	  bla = res.readLine()
	  if bla then
		Res[#Res+1] = bla
	  else
		break
	  end
	end
	res.close()
	Time = string.sub(Res[49], 63, string.len(Res[49])-4)
	Date = string.sub(Res[50], 53, string.len(Res[50])-4)
	DateFormat = {Time}
	for n=1,3 do
	  if string.find(Date, ", ") then
		local s,len = string.find(Date, "%, ")
		DateFormat[#DateFormat+1] = string.gsub(string.sub(Date, 1, s), "%,", "")
		Date = string.sub(Date, len+1, string.len(Date))
	  end
	end
	DateFormat[#DateFormat+1] = Date
	if string.sub(DateFormat[3], string.len(DateFormat[3])-2, string.len(DateFormat[3])-2) == " " then
	  BlaDate = tonumber(string.sub(DateFormat[3], string.len(DateFormat[3])-1, string.len(DateFormat[3])))
	else
	  BlaDate = tonumber(string.sub(DateFormat[3], string.len(DateFormat[3]), string.len(DateFormat[3])))
	end
	DateFormat[3] = string.gsub(DateFormat[3], BlaDate, "")
	BlaTime = tonumber(string.sub(DateFormat[1], 1, 2))+tonumber(offset)
	repeat
	  if BlaTime > 24 then
		BlaDate = BlaDate+1
		BlaTime = BlaTime-24
	  elseif BlaTime < 0 then
		BlaDate = BlaDate-1
		BlaTime = BlaTime+24
	  end
	until BlaTime > 0 and BlaTime < 24
	if string.len(BlaTime) == 1 then
	  BlaTime = "0"..BlaTime
	end
  end
  DateFormat[4] = tonumber(DateFormat[4])
  BlaMonth = string.gsub(DateFormat[3], " ", "")
  TheDays = BlaDate
  o = 1
  repeat
	if Months[o] == BlaMonth then
	  BlaDate = TheDays+BlaDate
	else
	  BlaDate = BlaDate+DaysInMonths[o]
	end
	o = o+1
  until Months[o] == BlaMonth
  repeat
	if BlaDate > 365 then
	  BlaDate = BlaDate-365
	  DateFormat[4] = DateFormat[4]+1
	end
  until BlaDate < 366
  local o = 1
  local TempNo = BlaDate
  repeat -- Turn this into a 7 day
	TempNo = TempNo-7
  until TempNo < 8
  DateFormat[2] = Days[TempNo]
  repeat -- Get it into a format for the proper month
	BlaDate = BlaDate-DaysInMonths[o]
	o = o+1
  until BlaDate <= DaysInMonths[o]
  BlaMonth = Months[o]
  DateFormat[3] = BlaMonth
  DateFormat[1] = BlaTime..string.sub(DateFormat[1], 3, string.len(DateFormat[1]))
  DateFormat[5] = BlaDate.."/"..o.."/"..DateFormat[4]
  table.insert(DateFormat, 3, BlaDate)
  return unpack(DateFormat)
end

This can probably be done neater but :)/>/>
Its not like you have one.. Right?

Oh and the reason this is bigger then it needs to, I couldn't find a time server I could access and set custom offset hours to.
So I ended up making my own offset thingy.

However as this is fetching from the internet, There will be a lag until it fetches the data or dies trying.

Returns it in a table.

19:13:33
Tuesday
24
April
2012
24/4/2012

So you can use Time,Day,DayNo, Month,Year,FormattedDate = time(0)
print(Time)
print(Day)
print(DayNo)
print(Month)
print(Year)
print(FormattedDate)

or

for _,n in pairs(time(0)) do
print(n)
end
libraryaddict #2
Posted 24 April 2012 - 08:43 AM
Eh.
Someone suggested unix timestamp which I didnt know about >.>
So Im looking at that

Edit: Doesnt do anything special this cant do :
libraryaddict #3
Posted 01 May 2012 - 07:29 AM
Fixed 2 issues pointed out in the server at http://www.computercraft.info/forums2/index.php?/topic/1488-125gaps-cc-server-247/
Iscariah #4
Posted 02 May 2012 - 04:09 AM
No one liked it? That isn't true! I like it dude. :)/>/> Thanks for this, it's a shame nobody gives you some love for it.
kazagistar #5
Posted 02 May 2012 - 06:41 PM
Getting defensive about your code is a common trait, but it can be problematic in cooperative working environments. You shouldn't get too attached to your programs; they are not you. If no one says anything, it might be because people dont understand your code, don't need it for anything at the moment, or can't think of any ways to improve on it, not because they think it is crap.
libraryaddict #6
Posted 02 May 2012 - 09:01 PM
The program is still there.
I dont see a reason to completely remove it.
But its not like I have to make sure its easy as possible for you to get it.
You just need to click a spoiler..
Past the discouraging message
Redd56 #7
Posted 21 March 2018 - 10:31 PM
how do i access the table, it just keeps giving me a blank output…
Lupus590 #8
Posted 22 March 2018 - 09:28 AM
how do i access the table, it just keeps giving me a blank output…

OP hasn't logged in since 2012, I wouldn't expect support here, you may want to post in ask a pro.

Edit: oh, you did http://www.computercraft.info/forums2/index.php?/topic/29443-rl-time-and-date-and-using-it-as-a-basis-for-in-game-events/
Edited on 22 March 2018 - 08:29 AM