Whitemambaa

DateTime.Lua

May 5th, 2014
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.65 KB | None | 0 0
  1. local modf=math.modf
  2. local floor=math.floor
  3. local concat=table.concat
  4. local format=string.format
  5.  
  6. --Date--
  7. local weekds={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"}
  8. local months={"January","February","March","April","May","June","July","August","September","October","November","December"}
  9. local daysny={31,28,31,30,31,30,31,31,30,31,30,31}
  10. local daysly={31,29,31,30,31,30,31,31,30,31,30,31}
  11. local secs_day=86400
  12.  
  13. local function th(n)
  14.     local f=n%10
  15.     local g=n%100
  16.     if f==1 and g~=11 then
  17.         return n.."st"
  18.     elseif f==2 and g~=12 then
  19.         return n.."nd"
  20.     elseif f==3 and g~=13 then
  21.         return n.."rd"
  22.     else
  23.         return n.."th"
  24.     end
  25. end
  26.  
  27. local function tod(pdy,showsec)
  28.     local hr,phr=modf(pdy*24)
  29.     local mn,pmn=modf(phr*60)
  30.     local sec,ms=modf(pmn*60)
  31.     local hrmn=format("%d:%02i",(hr-1)%12+1,mn)..(hr<12 and "AM" or "PM")
  32.     if showsec then
  33.         return hrmn..format(";%02is",sec)
  34.     else
  35.         return hrmn
  36.     end
  37. end
  38.  
  39. local function Date(Unix,abbr)
  40.     local loldays=Unix/secs_day
  41.     local days=loldays
  42.     local ycount=1970
  43.     local mcount=1
  44.     repeat
  45.         if ycount%400==0 or ycount%100~=0 and ycount%4==0 then
  46.             if days>=daysly[mcount] then
  47.                 days=days-daysly[mcount]
  48.             else break end
  49.         elseif days>=daysny[mcount] then
  50.             days=days-daysny[mcount]
  51.         else break end
  52.         mcount=mcount+1
  53.         if mcount>#months then
  54.             mcount=1
  55.             ycount=ycount+1
  56.         end
  57.     until ycount>1e4 --just in case
  58.     local d,t=modf(days)
  59.     return concat({tod(t,true),weekds[floor(loldays+3)%7+1],months[mcount],th(d+1),ycount}," ")
  60. end
  61. --------
  62.  
  63. print(Date(1340197939))
  64. if tick then
  65.     print(Date(tick()))
  66. elseif os then
  67.     print(Date(os.time()-8*3600))--PST
  68. end
Advertisement
Add Comment
Please, Sign In to add comment