summaryrefslogtreecommitdiffstats
path: root/src/calendar.cpp
blob: 9ad8413a2b6c386bc27695b1aed2970eb75348b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Encoding: UTF-8
#include "calendar.h"
#include <cstdio>
#include <ctime>
#include <iomanip>

/*以下为"calendar.h规定之量*/
double day = 86400;
double delta = 1e-11;

/*计算需要用的类的实例化*/
Julian julian;
parameter p;

void radToDMS(const char *message, int lowerBound, int upperBound,
              double radians) {
    // Convert radians to degrees
    double degrees = radians * 180.0 / M_PI;

    while (degrees < lowerBound)
        degrees += (upperBound - lowerBound);
    while (degrees >= upperBound)
        degrees -= (upperBound - lowerBound);

    // Calculate degrees, minutes, and seconds
    int deg = static_cast<int>(degrees);
    double min_sec = (degrees - deg) * 60.0;
    int min = static_cast<int>(min_sec);
    double sec = (min_sec - min) * 60.0;

    // Output the result
    printf("%s: %d°%d'%06.3f''\n", message, deg, min, sec);
}

int main(int argc, char *argv[]) {
    Date date;
    if (argc != 2) {
        printf("Input the time you want to calculate in <YYYY-MM-DD,HH:MM:SS> "
               "format: ");
        scanf("%d-%d-%d,%d:%d:%d", &date.tm_year, &date.tm_mon, &date.tm_mday,
              &date.tm_hour, &date.tm_min, &date.tm_sec);
    } else {
        sscanf(argv[1], "%d-%d-%d,%d:%d:%d", &date.tm_year, &date.tm_mon,
               &date.tm_mday, &date.tm_hour, &date.tm_min, &date.tm_sec);
    }
    date.tm_year -= 1900;
    date.tm_mon -= 1;
    date.tm_isdst = -1;

    time_t time = mktime(&date);
    double t = julian.getJulianKiloYear(time);

    pair<double, double> sun = p.sun_longitude(t);
    radToDMS("Sun longitude: ", 0, 360, sun.first);
    radToDMS("Sun latitude: ", -90, 90, sun.second);
    return 0;
}