我需要在给定日期找到满月的百分比,但我不知道如何计算这个。我这样做的尝试是错误的,因为今天的百分比大约是96.9%。有没有人能看出我在delphi代码中做错了什么?
procedure TfrmLag.btnCalcClick(Sender: TObject);
var
whatDate : TDateTime; // Now;
lunarDays : Double; // 29.53058770576
lunarSecs : LongInt; // lunarDays * (24*60*60);
new2000 : TDateTime; // 6.1 2000 18:14
totalSecs : LongInt; // whatDate - new2000
currSecs : LongInt; // totalSecs MOD lunarSecs
currFrac : Double; // currSecs / lunarSecs
currDays : LongInt; // currFrac * lunarDays
perOfFull : Double;
begin
whatDate := Now;
lunarDays := 29.53058770576;
lunarSecs := Round(lunarDays * (24*60*60));
new2000 := EncodeDateTime(2000,1,6,18,14,00,000);
totalSecs := SecondsBetween(whatDate, new2000);
currSecs := totalSecs MOD lunarSecs;
currFrac := currSecs / lunarSecs;
currDays := Round(currFrac*lunarDays);
perOfFull := (100*currFrac);
lb.Items.Add('Date : '+FormatDateTime('dd.mm.yyyy hh:mm:ss',whatDate));
lb.Items.Add('Lunar days : '+IntToStr(lunarSecs));
lb.Items.Add('First full 2000 : '+FormatDateTime('dd.mm.yyyy hh:mm:ss',new2000));
lb.Items.Add('Total seconds : '+IntToStr(totalSecs));
lb.Items.Add('Current seconds : '+IntToStr(currSecs));
lb.Items.Add('Current fraction : '+FloatToStr(currFrac));
lb.Items.Add('Current days : '+IntToStr(currDays));
lb.items.Add('Percent of full : '+FloatToStr(perOfFull));
end;
- 我想
new2000
是2000年的第一个新月吧?如果是这样,那么这段代码应该正确计算。 - 如果
new2000
是满月,您只需要移除cos()
函数中的-1
。
uses
DateUtils;
procedure Calculate();
const
MoonPeriod = 29.53058770576;
var
KnownNewMoon: TDateTime;
NowUTC: TDateTime;
DaysSinceLastNewMoon, NumberOfNewMoons, MoonPart: Extended;
begin
KnownNewMoon := EncodeDateTime(2000,1,6,18,14,00,000);
NowUTC := TTimeZone.Local.ToUniversalTime(Now);
//How many moon periods (new moon -> full moon -> new moon) have passed
//since that known new moon date?
NumberOfNewMoons := (NowUTC - KnownNewMoon)/MoonPeriod;
DaysSinceLastNewMoon := Frac(NumberOfNewMoons)*MoonPeriod;
//The "moon part" is a sine/cosine function that starts at new moon with -0,
//reaches 1 at full moon and goes back to 0 at the next new moon.
//Starting at cos(-Pi) gives a -1 as "new moon value". Add 1 to set this to 0.
//Full moon is cos(0) gives 1. With the 1 added before, we have to divide by 2.
MoonPart := (cos((NumberOfNewMoons*2 - 1) * Pi) + 1)/2;
lb.items.Add('Number/amount of new moons: '+ FormatFloat('0.000000', NumberOfNewMoons));
lb.items.Add('Current moon part/position: '+ FormatFloat('0.000000', MoonPart));
lb.items.Add('Days since last new moon: '+ FormatFloat('0.000000', DaysSinceLastNewMoon));
end;
这将给出MoonPart
中可见的月亮部分以及自上次新月以来的天数(包括分数)。