|
| 1 | +#include "mainwindow.h" |
| 2 | +#include "ui_mainwindow.h" |
| 3 | +#include "astm.h" |
| 4 | +#include <string> |
| 5 | +#include <math.h> |
| 6 | +#include <ctime> |
| 7 | +#include <chrono> |
| 8 | +#include <QTimer> |
| 9 | + |
| 10 | +MainWindow::MainWindow(QWidget *parent) |
| 11 | + : QMainWindow(parent) |
| 12 | + , ui(new Ui::MainWindow) |
| 13 | +{ |
| 14 | + ui->setupUi(this); |
| 15 | + |
| 16 | + timer = new QTimer(this) ; |
| 17 | + connect( timer, SIGNAL( timeout() ), this, SLOT( on_set_time_clicked() ) ) ; |
| 18 | + |
| 19 | + std::time_t now = std::time(NULL) ; |
| 20 | + std::time_t local = std::mktime(std::localtime(&now)) ; |
| 21 | + std::time_t gmt = std::mktime(std::gmtime(&now)) ; |
| 22 | + |
| 23 | + to = static_cast<int> (local - gmt) * 1000 ; |
| 24 | + |
| 25 | + float h = to * 2.7777777777e-7 ; |
| 26 | + |
| 27 | + ui -> time_zone_h -> setText(QString::number(h)) ; |
| 28 | + |
| 29 | +} |
| 30 | + |
| 31 | +MainWindow::~MainWindow() |
| 32 | +{ |
| 33 | + delete ui; |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +void MainWindow::changed_time() |
| 38 | + { |
| 39 | + |
| 40 | + int UT_h, UT_m, gm_h, gm_m, LST_h, LST_m ; |
| 41 | + double UT_s, seconds, jd, gmst, gm_s, LST, LST_s ; |
| 42 | + |
| 43 | + UT = hour - tz ; |
| 44 | + |
| 45 | + if (UT < 0) |
| 46 | + { |
| 47 | + |
| 48 | + UT += 24 ; |
| 49 | + |
| 50 | + seconds = int( d_d(1970, 1, 1, 0, 0, 0, year, month, day, 0, 0, 0) * 86400 - 86400 ) ; |
| 51 | + time_t now = (time_t)seconds ; |
| 52 | + struct tm *t = localtime(&now); |
| 53 | + |
| 54 | + year_2 = t -> tm_year + 1900 ; |
| 55 | + month_2 = t -> tm_mon + 1 ; |
| 56 | + day_2 = t -> tm_mday ; |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + else if (UT > 24) |
| 61 | + { |
| 62 | + |
| 63 | + UT -= 24 ; |
| 64 | + |
| 65 | + seconds = int( d_d(1970, 1, 1, 0, 0, 0, year, month, day, 0, 0, 0) * 86400 + 86400 ) ; |
| 66 | + time_t now = (time_t)seconds ; |
| 67 | + struct tm *t = localtime(&now); |
| 68 | + |
| 69 | + year_2 = t -> tm_year + 1900 ; |
| 70 | + month_2 = t -> tm_mon + 1 ; |
| 71 | + day_2 = t -> tm_mday ; |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + else |
| 76 | + { |
| 77 | + |
| 78 | + year_2 = year ; |
| 79 | + month_2 = month ; |
| 80 | + day_2 = day ; |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + UT = ceil(UT * 1e7) * 1e-7 ; |
| 85 | + |
| 86 | + UT_h = int(UT) ; |
| 87 | + UT_m = int( (UT - UT_h) * 60 ) ; |
| 88 | + UT_s = (UT - UT_h) * 3600 - UT_m * 60 ; |
| 89 | + |
| 90 | + ui -> uth -> setText( QString::number(UT_h) ) ; |
| 91 | + ui -> utm -> setText( QString::number(UT_m) ) ; |
| 92 | + ui -> uts -> setText( QString::number(UT_s, 'f', 3) ) ; |
| 93 | + |
| 94 | + jd = JD(year_2, month_2, day_2, UT_h, UT_m, UT_s) ; |
| 95 | + ui -> j_day -> setText( QString::number(jd, 'f', 8) ) ; |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + gmst = GMST(year_2, month_2, day_2, UT_h, UT_m, UT_s) ; |
| 100 | + |
| 101 | + gm_h = int(gmst) ; |
| 102 | + gm_m = int( (gmst - gm_h) * 60) ; |
| 103 | + gm_s = (gmst - gm_h) * 3600 - gm_m * 60 ; |
| 104 | + |
| 105 | + ui -> g_h -> setText(QString::number(gm_h)) ; |
| 106 | + ui -> g_m -> setText(QString::number(gm_m)) ; |
| 107 | + ui -> g_s -> setText(QString::number(gm_s, 'f', 3)) ; |
| 108 | + |
| 109 | + LST = fmod(gmst + lo, 24) ; |
| 110 | + |
| 111 | + if (LST < 0) |
| 112 | + LST += 24 ; |
| 113 | + |
| 114 | + LST_h = int(LST) ; |
| 115 | + LST_m = int( (LST - LST_h) * 60) ; |
| 116 | + LST_s = (LST - LST_h) * 3600 - LST_m * 60 ; |
| 117 | + |
| 118 | + ui -> lst_h -> setText(QString::number(LST_h)) ; |
| 119 | + ui -> lst_m -> setText(QString::number(LST_m)) ; |
| 120 | + ui -> lst_s -> setText(QString::number(LST_s, 'f', 3)) ; |
| 121 | + |
| 122 | + } |
| 123 | + |
| 124 | +void MainWindow::on_time_userTimeChanged(const QTime &time) |
| 125 | +{ |
| 126 | + hour = time.hour() + double(time.minute()) / 60 + double(time.second()) / 3600 + double(time.msec()) / 3600000 ; |
| 127 | + |
| 128 | + changed_time() ; |
| 129 | +} |
| 130 | + |
| 131 | +void MainWindow::on_time_zone_h_textChanged(const QString &arg1) |
| 132 | +{ |
| 133 | + if (arg1[0] == "-") |
| 134 | + tz = -abs( arg1.toDouble() ) - ( ui -> time_zone_m -> text().toDouble() ) / 60 ; |
| 135 | + else |
| 136 | + tz = abs( arg1.toDouble() ) + ( ui -> time_zone_m -> text().toDouble() ) / 60 ; |
| 137 | + |
| 138 | + changed_time() ; |
| 139 | +} |
| 140 | + |
| 141 | +void MainWindow::on_time_zone_m_textChanged(const QString &arg1) |
| 142 | +{ |
| 143 | + if (ui -> time_zone_h -> text()[0] == "-") |
| 144 | + tz = -abs( ui -> time_zone_h -> text().toDouble() ) - ( arg1.toDouble() ) / 60 ; |
| 145 | + else |
| 146 | + tz = abs( ui -> time_zone_h -> text().toDouble() ) + ( arg1.toDouble() ) / 60 ; |
| 147 | + |
| 148 | + changed_time() ; |
| 149 | +} |
| 150 | + |
| 151 | +void MainWindow::on_date_userDateChanged(const QDate &date) |
| 152 | +{ |
| 153 | + |
| 154 | + day = date.day() ; |
| 155 | + month = date.month() ; |
| 156 | + year = date.year() ; |
| 157 | + |
| 158 | + changed_time() ; |
| 159 | + |
| 160 | +} |
| 161 | + |
| 162 | +void MainWindow::on_longitude_textChanged(const QString &arg1) |
| 163 | +{ |
| 164 | + lo = arg1.toDouble() * 0.066666666666666 ; // degrees to hours |
| 165 | +} |
| 166 | + |
| 167 | +void MainWindow::on_set_time_clicked() |
| 168 | +{ |
| 169 | + |
| 170 | + using namespace std::chrono ; |
| 171 | + |
| 172 | + auto now = high_resolution_clock::now() ; |
| 173 | + auto millis = duration_cast<milliseconds>(now.time_since_epoch()).count() ; |
| 174 | + |
| 175 | + int s = fmod(millis + to, 864e5) ; |
| 176 | + |
| 177 | + ui -> date -> setDate( QDate::currentDate() ) ; |
| 178 | + ui -> time -> setTime( QTime::fromMSecsSinceStartOfDay(s) ) ; |
| 179 | + |
| 180 | +} |
| 181 | + |
| 182 | +void MainWindow::on_run_clicked() |
| 183 | +{ |
| 184 | + timer -> start(55) ; |
| 185 | +} |
| 186 | + |
| 187 | +void MainWindow::on_stop_clicked() |
| 188 | +{ |
| 189 | + timer -> stop() ; |
| 190 | +} |
0 commit comments