PtokaX forum

Development Section => HOW-TO's => Topic started by: TTB on 07 February, 2007, 10:44:55

Title: Juliandate with hours/seconds
Post by: TTB on 07 February, 2007, 10:44:55
Hi,

There is a julianday / juliandate at this moment, and gives a exact number on each day. Is there also a function with a specific number on each second?

Gr.
Title: Re: Juliandate with hours/seconds
Post by: Leun on 07 February, 2007, 13:44:32

Modded the script from VidFamne a bit (upper script), now there's a function for seconds


--VidFamne
--(Modified Julian "minute" number. This restricts the algorithm to 1900 Mar 01 until 2100 Feb 28)
-- Julian to minutes
Jmn = function()
local D = tonumber(os.date("%d"))
local H = tonumber(os.date("%H"))
local minutE = tonumber(os.date("%M"))
local Y = tonumber(os.date("%Y"))
local M = tonumber(os.date("%m"))
if M <= 2 then
M = M + 12
Y=Y-1
end
mn = 1440*(math.floor(Y*365,25) + math.floor((M+1)*30,6) + D -428) + H*60 + minutE
return mn
end



--VidFamne
--(Modified Julian "minute" number. This restricts the algorithm to 1900 Mar 01 until 2100 Feb 28)
-- Julian to seconds
Jsn = function()
local D = tonumber(os.date("%d"))
local H = tonumber(os.date("%H"))
local minutE = tonumber(os.date("%M"))
local secondS = tonumber(os.date("%S"))
local Y = tonumber(os.date("%Y"))
local M = tonumber(os.date("%m"))
SendToAll(M)
if M <= 2 then
M = M + 12
Y=Y-1
end
mn = 1440*(math.floor(Y*365,25) + math.floor((M+1)*30,6) + D -428) + H*3600 + minutE*60 + secondS
return mn
end


Maby anyone has an other function, but thisone works :-)

Greetz,
Title: Re: Juliandate with hours/seconds
Post by: bastya_elvtars on 07 February, 2007, 14:02:01
Code (lua) Select
function JulianDate(DAY, MONTH, YEAR, HOUR, MINUTE, SECOND) -- Written by RabidWombat.
-- HOUR is 24hr format.
  local jy, ja, jm;
  assert(YEAR ~= 0);
  assert(YEAR ~= 1582 or MONTH ~= 10 or DAY < 4 or DAY > 15);
  --The dates 5 through 14 October, 1582, do not exist in the Gregorian system!
  if(YEAR < 0 ) then
    YEAR = YEAR + 1;
  end
  if( MONTH > 2) then
    jy = YEAR;
    jm = MONTH + 1;
  else
    jy = YEAR - 1;
    jm = MONTH + 13;
  end
  local intgr = math.floor( math.floor(365.25*jy) + math.floor(30.6001*jm) + DAY + 1720995 );
  --check for switch to Gregorian calendar
  local gregcal = 15 + 31*( 10 + 12*1582 );
  if(DAY + 31*(MONTH + 12*YEAR) >= gregcal ) then
    ja = math.floor(0.01*jy);
    intgr = intgr + 2 - ja + math.floor(0.25*ja);
  end
  --correct for half-day offset
  local dayfrac = HOUR / 24 - 0.5;
  if( dayfrac < 0.0 ) then
    dayfrac = dayfrac + 1.0;
    intgr = intgr - 1;
  end
  --now set the fraction of a day
  local frac = dayfrac + (MINUTE + SECOND/60.0)/60.0/24.0;
  --round to nearest second
  local jd0 = (intgr + frac)*100000;
  local  jd  = math.floor(jd0);
  if( jd0 - jd > 0.5 ) then jd = jd + 1 end
  return jd/100000;
end

function frac(num) -- returns fraction of a number (RabidWombat)
  return num - math.floor(num);
end


Could be optimized more for 5.1, but haven't yet had time.
Title: Re: Juliandate with hours/seconds
Post by: Leun on 07 February, 2007, 14:09:17
lol thats much better ;D
Title: Re: Juliandate with hours/seconds
Post by: Yahoo on 07 February, 2007, 14:16:38
i am getting the error for the script posted by bastya_elvtars
[18:41] Syntax \scripts\julian.lua:44: '=' expected near 'be'
Title: Re: Juliandate with hours/seconds
Post by: TTB on 07 February, 2007, 14:41:45
Thanx bastya... I think I can work with this.
Title: Re: Juliandate with hours/seconds
Post by: bastya_elvtars on 07 February, 2007, 14:42:29
Quote from: Yahoo on 07 February, 2007, 14:16:38
i am getting the error for the script posted by bastya_elvtars
[18:41] Syntax \scripts\julian.lua:44: '=' expected near 'be'

Try not copying the last line of my post, since it's a comment.
Title: Re: Juliandate with hours/seconds
Post by: Psycho_Chihuahua on 07 February, 2007, 15:13:21
Quote from: bastya_elvtars on 07 February, 2007, 14:42:29
Try not copying the last line of my post, since it's a comment.

That should be obvious though ;)
Title: Re: Juliandate with hours/seconds
Post by: plop on 07 February, 2007, 20:32:43
why so complex when it's build into lua from version 5.0 and up?
--code snipe from a.i. v2 (bsd license)
JulianDate = function(tTime)
if not tTime then
local tTime = os.date("*t")
return os.time({year = tTime.year, month = tTime.month, day = tTime.day,
hour = tTime.hour, min = tTime.min, sec = tTime.sec}
)
end
return os.time({year = tTime.year, month = tTime.month, day = tTime.day,
hour = tTime.hour, min = tTime.min, sec = tTime.sec}
)
end,

CedianDateTable = function(iJdate)
return os.date("*t", iJdate)
end,

CedianDateString = function(iJdate)
return os.date("%c", iJdate)
end,

JulianDiff = function(iThen, iNow)
return os.difftime( (iNow or tMain.JulianDate()) , iThen)
end,

1 note, this isn't compatible with the lua versions posted above as it uses the unix style.
doesn't start from 0:00.00 01-01-0000 but from 00:00.00 01-01-1970.

plop
Title: Re: Juliandate with hours/seconds
Post by: speedX on 07 February, 2007, 21:00:37
Could any please tel me how this works??
Title: Re: Juliandate with hours/seconds
Post by: plop on 07 February, 2007, 21:23:52
Quote from: speedX on 07 February, 2007, 21:00:37
Could any please tel me how this works??

click (http://www.lua.org/pil/22.1.html)

plop
Title: Re: Juliandate with hours/seconds
Post by: bastya_elvtars on 07 February, 2007, 22:27:33
Quote from: plop on 07 February, 2007, 20:32:43
why so complex when it's build into lua from version 5.0 and up?

Because I did not know anything about this before, simply overlooked in the manual. Thanks for the snippet.
Moved to howtos.