Skip to content

Commit 8f56c33

Browse files
Add files via upload
1 parent 90b234a commit 8f56c33

8 files changed

Lines changed: 1335 additions & 0 deletions

File tree

Astime.pro

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
QT += core gui
2+
3+
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
4+
5+
CONFIG += c++11
6+
7+
# The following define makes your compiler emit warnings if you use
8+
# any Qt feature that has been marked deprecated (the exact warnings
9+
# depend on your compiler). Please consult the documentation of the
10+
# deprecated API in order to know how to port your code away from it.
11+
DEFINES += QT_DEPRECATED_WARNINGS
12+
13+
# You can also make your code fail to compile if it uses deprecated APIs.
14+
# In order to do so, uncomment the following line.
15+
# You can also select to disable deprecated APIs only up to a certain version of Qt.
16+
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
17+
18+
SOURCES += \
19+
main.cpp \
20+
mainwindow.cpp \
21+
astm.cpp
22+
23+
HEADERS += \
24+
mainwindow.h \
25+
astm.h
26+
27+
FORMS += \
28+
mainwindow.ui
29+
30+
# Default rules for deployment.
31+
qnx: target.path = /tmp/$${TARGET}/bin
32+
else: unix:!android: target.path = /opt/$${TARGET}/bin
33+
!isEmpty(target.path): INSTALLS += target

Astime.pro.user

Lines changed: 317 additions & 0 deletions
Large diffs are not rendered by default.

astm.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include "astm.h"
2+
#include <ctime>
3+
#include <math.h>
4+
5+
double JD(int y, int m, int d, int h, int mi, double s)
6+
{
7+
8+
double jd, dd ;
9+
10+
dd = d_d(2000, 1, 1, 12, 0, 0, y, m, d, h, mi, s) ;
11+
12+
jd = 2451545.0 + dd ;
13+
14+
return jd ;
15+
16+
}
17+
18+
double d_d(int y0, int m0, int d0, int h0, int mi0, float s0, int y, int m, int d, int h, int mi, double s)
19+
{
20+
21+
struct std::tm a = { s0, mi0, h0, d0, m0 - 1, y0 - 1900 } ;
22+
23+
struct std::tm b = { s, mi, h, d, m - 1, y - 1900 } ;
24+
25+
std::time_t x = std::mktime(&a);
26+
27+
std::time_t z = std::mktime(&b);
28+
29+
long double difference = std::difftime(z, x) * 0.000011574074074074073 ;
30+
31+
return difference ;
32+
33+
}
34+
35+
double GMST(int y, int m, int d, int h, int mi, double s)
36+
{
37+
38+
double D, T, gmst ;
39+
40+
D = JD(y, m, d, 0, 0, 0) - 2451545 ;
41+
T = D / 36525 ;
42+
43+
gmst = 24110.54841 + 8640184.812866 * T + 0.093104 * T * T
44+
- 0.00000632 * T * T * T ;
45+
46+
gmst *= 0.0002777777777777778 ;
47+
gmst += ( h + mi * 0.0166666666666 + s * 0.0002777777777777 ) * 1.0027379093382884 ;
48+
gmst = fmod(gmst, 24.0) ;
49+
50+
return gmst ;
51+
52+
}
53+

astm.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef ASTM_H
2+
#define ASTM_H
3+
4+
double JD(int, int, int, int, int, double) ; // Julian day for gregorian callendar. Fuck people who don't use it
5+
6+
double d_d(int, int, int, int, int, float, int, int, int, int, int, double) ; // difference between 2 dates (initial: year, month,
7+
// day, hour, minute, second
8+
// final: year, month etc.)
9+
// Also works backwards i.e. first year 2000 final year 1840
10+
11+
double GMST(int, int, int, int, int, double) ; // Greenwich medium siderial time. Input current year, month,
12+
// day, hour, minute, second
13+
14+
15+
#endif /* ASTM_H */

main.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "mainwindow.h"
2+
3+
#include <QApplication>
4+
5+
int main(int argc, char *argv[])
6+
{
7+
QApplication a(argc, argv);
8+
MainWindow w;
9+
w.show();
10+
return a.exec();
11+
}

mainwindow.cpp

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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+
}

mainwindow.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#ifndef MAINWINDOW_H
2+
#define MAINWINDOW_H
3+
4+
#include <QMainWindow>
5+
#include <QTime>
6+
7+
QT_BEGIN_NAMESPACE
8+
namespace Ui { class MainWindow; }
9+
QT_END_NAMESPACE
10+
11+
class MainWindow : public QMainWindow
12+
{
13+
Q_OBJECT
14+
15+
public:
16+
MainWindow(QWidget *parent = nullptr);
17+
~MainWindow();
18+
19+
private slots:
20+
void on_time_userTimeChanged(const QTime &time);
21+
22+
void on_time_zone_h_textChanged(const QString &arg1);
23+
24+
void on_time_zone_m_textChanged(const QString &arg1);
25+
26+
void changed_time() ;
27+
28+
void on_date_userDateChanged(const QDate &date);
29+
30+
void on_longitude_textChanged(const QString &arg1);
31+
32+
void on_set_time_clicked();
33+
34+
void on_run_clicked();
35+
36+
void on_stop_clicked();
37+
38+
private:
39+
Ui::MainWindow *ui;
40+
41+
int year = 2020, month = 1, day = 1 ;
42+
int year_2, month_2, day_2 ;
43+
double hour, UT ;
44+
double tz ; // time zone
45+
double lo ; // longitude
46+
QTimer *timer ;
47+
int to ; // time offset from UTC
48+
49+
};
50+
#endif // MAINWINDOW_H

0 commit comments

Comments
 (0)