delphi 我如何计算在一个给定的日期满月的百分比?

xienkqul  于 2023-01-13  发布在  其他
关注(0)|答案(1)|浏览(129)

我需要找到一个给定日期满月的百分比,但我不知道如何计算。我试图这样做是错误的,因为今天的百分比约为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;
b1uwtaje

b1uwtaje1#

  • 我想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中可见的月亮部分和自上次新月以来的天数(包括分数)。

相关问题