utc.lua 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. -- title: utc
  2. -- author: pixelbath
  3. -- desc: renders tstamp() as current UTC date/time. compact version could be more compacted, but it's at least semi-readable this way.
  4. local days = {
  5. { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335},
  6. { 366, 397, 425, 456, 486, 517, 547, 578, 609, 639, 670, 700},
  7. { 731, 762, 790, 821, 851, 882, 912, 943, 974,1004,1035,1065},
  8. {1096,1127,1155,1186,1216,1247,1277,1308,1339,1369,1400,1430},
  9. }
  10. function tstampToDateTime(epoch)
  11. local flr=math.floor
  12. epoch = tonumber(epoch)
  13. local dt = {}
  14. dt.second = epoch%60; epoch=epoch//60
  15. dt.minute = epoch%60; epoch=epoch//60
  16. dt.hour = epoch%24; epoch=epoch//24
  17. local years,month = epoch//(365*4+1)*4,0
  18. epoch = epoch % (365*4+1)
  19. local year
  20. for y = 4,1,-1 do
  21. if epoch >= days[y][1] then
  22. year = y
  23. break
  24. end
  25. end
  26. for m = 12,1,-1 do
  27. if epoch >= days[year][m] then
  28. month = m
  29. break
  30. end
  31. end
  32. dt.year = years+year+1970-1
  33. dt.month = month
  34. dt.day = epoch-days[year][month]+1
  35. return string.format("%04d-%02d-%02d %02d:%02d:%02d", dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second)
  36. end
  37. function tstampToDT(epoch)
  38. local flr,ep,ly,dt=math.floor,epoch//1,365*4+1,{}
  39. dt.s=ep%60;ep=ep//60
  40. dt.m=ep%60;ep=ep//60
  41. dt.h=ep%24;ep=ep//24
  42. local yr,yrs,mo=0,ep//ly*4,0
  43. ep=ep%ly
  44. for y=4,1,-1 do
  45. if ep>=days[y][1]then yr=y;break end
  46. end
  47. for m=12,1,-1 do
  48. if ep>=days[yr][m]then mo=m;break end
  49. end
  50. dt.y=yrs+yr+1970-1
  51. dt.mo=mo
  52. dt.d=ep-days[yr][mo]+1
  53. return string.format("%04d-%02d-%02d %02d:%02d:%02d",dt.y,dt.mo,dt.d,dt.h,dt.m,dt.s)
  54. end
  55. function TIC()
  56. cls(0)
  57. local timestamp = tstamp()
  58. print("regular - 747 chars: "..tstampToDateTime(timestamp), 10, 10, 4,1,1,1)
  59. print("compact - 496 chars: "..tstampToDT(timestamp), 10, 20, 4,1,1,1)
  60. end
  61. -- <PALETTE>
  62. -- 000:1a1c2c5d275db13e53ef7d57ffcd75a7f07038b76425717929366f3b5dc941a6f673eff7f4f4f494b0c2566c86333c57
  63. -- </PALETTE>