c日出日落时间

Fer*_*Fer 5 c time

在我的C应用程序中,我想计算给定日期,纬度和经度的日出/日落时间.我一直在网上搜索,但我找不到工作样本.

我试图实现这个示例:http: //souptonuts.sourceforge.net/code/sunrise.c.html

但是这个样本没有正常工作.

是否有一个简单的C源代码或方法,我可以在我的应用程序中轻松实现?

编辑:
我在此链接上实现代码,但它给了我错误的日落/日出值.我也在这里尝试了Saul的链接,但它也给了我错误的结果.
我有41N,28E的位置.当我尝试这些代码时,两个样本都表示日出值大约是10:13而日落是23:24.但正确的值是06:06,20:13.
我无法理解这个问题.

小智 8

根据日期,纬度和经度计算日出/日落时间的十个简单步骤

  1. 首先计算一年中的哪一天

    N1 =楼层(275*月/ 9)N2 =楼层((月+ 9)/ 12)N3 =(1 +楼层((年 - 4*楼(年/ 4)+ 2)/ 3))N = N1 - (N2*N3)+天 - 30

  2. 将经度转换为小时值并计算大致时间

    lngHour =经度/ 15

    如果需要上升时间:t = N +((6 - lngHour)/ 24)如果需要设定时间:t = N +((18 - lngHour)/ 24)

  3. 计算太阳的平均异常

    M =(0.9856*t) - 3.289

  4. 计算太阳的真实经度

    L = M +(1.916*sin(M))+(0.020*sin(2*M))+ 282.634注:L可能需要通过加/减360调整到[0,360]范围内

5A.计算太阳的正确提升

RA = atan(0.91764 * tan(L))
NOTE: RA potentially needs to be adjusted into the range [0,360) by adding/subtracting 360
Run Code Online (Sandbox Code Playgroud)

5B.右提升值需要与L在同一象限

Lquadrant  = (floor( L/90)) * 90
RAquadrant = (floor(RA/90)) * 90
RA = RA + (Lquadrant - RAquadrant)
Run Code Online (Sandbox Code Playgroud)

5C.正确的提升值需要转换成小时

RA = RA / 15
Run Code Online (Sandbox Code Playgroud)
  1. 计算太阳的赤纬

    sinDec = 0.39782*sin(L)cosDec = cos(asin(sinDec))

7A.计算太阳的当地小时角

cosH = (cos(zenith) - (sinDec * sin(latitude))) / (cosDec * cos(latitude))

if (cosH >  1) 
  the sun never rises on this location (on the specified date)
if (cosH < -1)
  the sun never sets on this location (on the specified date)
Run Code Online (Sandbox Code Playgroud)

7B.完成计算H并转换为小时

if if rising time is desired:
  H = 360 - acos(cosH)
if setting time is desired:
  H = acos(cosH)

H = H / 15
Run Code Online (Sandbox Code Playgroud)
  1. 计算当地平均上升/设定时间

    T = H + RA - (0.06571*t) - 6.622

  2. 调整回UTC

    UT = T - lngHour注意:UT可能需要通过添加/减去24来调整到[0,24]范围内

  3. 将UT值转换为纬度/经度的本地时区

    localT = UT + localOffset


sco*_*ski 5

使用本指南(最初由@BenjaminMonate 发布,我假设与@Geetha 使用的指南相同),我构建了这个似乎可以正常工作的 C 函数。

#include <math.h>
#define PI 3.1415926
#define ZENITH -.83

float calculateSunrise(int year,int month,int day,float lat, float lng,int localOffset, int daylightSavings) {
    /*
    localOffset will be <0 for western hemisphere and >0 for eastern hemisphere
    daylightSavings should be 1 if it is in effect during the summer otherwise it should be 0
    */
    //1. first calculate the day of the year
    float N1 = floor(275 * month / 9);
    float N2 = floor((month + 9) / 12);
    float N3 = (1 + floor((year - 4 * floor(year / 4) + 2) / 3));
    float N = N1 - (N2 * N3) + day - 30;

    //2. convert the longitude to hour value and calculate an approximate time
    float lngHour = lng / 15.0;      
    float t = N + ((6 - lngHour) / 24);   //if rising time is desired:
    //float t = N + ((18 - lngHour) / 24)   //if setting time is desired:

    //3. calculate the Sun's mean anomaly   
    float M = (0.9856 * t) - 3.289;

    //4. calculate the Sun's true longitude
    float L = fmod(M + (1.916 * sin((PI/180)*M)) + (0.020 * sin(2 *(PI/180) * M)) + 282.634,360.0);

    //5a. calculate the Sun's right ascension      
    float RA = fmod(180/PI*atan(0.91764 * tan((PI/180)*L)),360.0);

    //5b. right ascension value needs to be in the same quadrant as L   
    float Lquadrant  = floor( L/90) * 90;
    float RAquadrant = floor(RA/90) * 90;
    RA = RA + (Lquadrant - RAquadrant);

    //5c. right ascension value needs to be converted into hours   
    RA = RA / 15;

    //6. calculate the Sun's declination
    float sinDec = 0.39782 * sin((PI/180)*L);
    float cosDec = cos(asin(sinDec));

    //7a. calculate the Sun's local hour angle
    float cosH = (sin((PI/180)*ZENITH) - (sinDec * sin((PI/180)*lat))) / (cosDec * cos((PI/180)*lat));
    /*   
    if (cosH >  1) 
    the sun never rises on this location (on the specified date)
    if (cosH < -1)
    the sun never sets on this location (on the specified date)
    */

    //7b. finish calculating H and convert into hours
    float H = 360 - (180/PI)*acos(cosH);   //   if if rising time is desired:
    //float H = acos(cosH) //   if setting time is desired:      
    H = H / 15;

    //8. calculate local mean time of rising/setting      
    float T = H + RA - (0.06571 * t) - 6.622;

    //9. adjust back to UTC
    float UT = fmod(T - lngHour,24.0);

    //10. convert UT value to local time zone of latitude/longitude
    return UT + localOffset + daylightSavings;

    }

void printSunrise() {
    float localT = calculateSunrise(/*args*/);
    double hours;
    float minutes = modf(localT,&hours)*60;
    printf("%.0f:%.0f",hours,minutes);
    }
Run Code Online (Sandbox Code Playgroud)


ues*_*esp 1

示例代码似乎可以在 VC++ 2010 中运行,但有一些细微的变化:

  • 将其编译为 C++ 文件而不是 C 文件。
  • 删除#include <sys/time.h>线。
  • 在文件顶部添加#define _USE_MATH_DEFINES以便定义 M_PI。
  • 将调用%T中的两个更改为.strftime()%X

现在您有了一个工作示例,您可以调试工作版本和您的版本,以查看计算在哪里开始不同并缩小问题范围。要么单步执行程序,要么printf()像示例一样自由使用临时调用。

如果您需要特定帮助,您必须发布您的代码(整个文件的链接或您需要帮助的特定片段)。