]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/clock.cxx
RNAV: use runway departure end for previous wpt.
[flightgear.git] / src / Instrumentation / clock.cxx
1 // clock.cxx - an electric-powered turn indicator.
2 // Written by Melchior FRANZ, started 2003.
3 //
4 // This file is in the Public Domain and comes with no warranty.
5 //
6 // $Id$
7
8 #ifdef HAVE_CONFIG_H
9 #  include <config.h>
10 #endif
11
12 #include <cstdio>
13
14 #include "clock.hxx"
15 #include <simgear/timing/sg_time.hxx>
16 #include <Main/fg_props.hxx>
17 #include <Main/util.hxx>
18
19
20 Clock::Clock(SGPropertyNode *node) :
21     _name(node->getStringValue("name", "clock")),
22     _num(node->getIntValue("number", 0)),
23     _is_serviceable(true),
24     _gmt_time_sec(0),
25     _offset_sec(0),
26     _indicated_sec(0),
27     _indicated_min(0),
28     _indicated_hour(0),
29     _local_hour(0),
30     _standstill_offset(0)
31 {
32     _indicated_string[0] = '\0';
33 }
34
35 Clock::~Clock ()
36 {
37 }
38
39 void
40 Clock::init ()
41 {
42     std::string branch;
43     branch = "/instrumentation/" + _name;
44
45     SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
46     _serviceable_node = node->getChild("serviceable", 0, true);
47     _offset_node = node->getChild("offset-sec", 0, true);
48     _sec_node = node->getChild("indicated-sec", 0, true);
49     _min_node = node->getChild("indicated-min", 0, true);
50     _hour_node = node->getChild("indicated-hour", 0, true);
51     _lhour_node = node->getChild("local-hour", 0, true);
52     _string_node = node->getChild("indicated-string", 0, true);
53     _string_node1 = node->getChild("indicated-short-string", 0, true);
54     _string_node2 = node->getChild("local-short-string", 0, true);
55 }
56
57 void
58 Clock::update (double delta_time_sec)
59 {
60     if (!_serviceable_node->getBoolValue()) {
61         if (_is_serviceable) {
62             _string_node->setStringValue("");
63             _is_serviceable = false;
64         }
65         return;
66     }
67
68     struct tm *t = globals->get_time_params()->getGmt();
69     int hour = t->tm_hour;
70     int min = t->tm_min;
71     int sec = t->tm_sec;
72
73     // compute local time zone hour
74     int tzoffset_hours = globals->get_time_params()->get_local_offset() / 3600;
75     int lhour = hour + tzoffset_hours;
76     if (lhour < 0)
77         lhour += 24;
78     if (lhour >= 24)
79         lhour -= 24;
80
81     long gmt = (hour * 60 + min) * 60 + sec;
82     int offset = _offset_node->getLongValue();
83
84     if (!_is_serviceable) {
85         _standstill_offset -= gmt - _gmt_time_sec;
86     } else if (_gmt_time_sec == gmt && _offset_sec == offset)
87         return;
88
89     _gmt_time_sec = gmt;
90     _offset_sec = offset;
91
92     _indicated_sec = _gmt_time_sec + offset + _standstill_offset;
93     _sec_node->setLongValue(_indicated_sec);
94
95     sec += offset;
96     while (sec < 0) {
97         sec += 60;
98         min--;
99     }
100     while (sec >= 60) {
101         sec -= 60;
102         min++;
103     }
104     while (min < 0) {
105         min += 60;
106         hour--;
107     }
108     while (min >= 60) {
109         min -= 60;
110         hour++;
111     }
112     while (hour < 0)
113         hour += 24;
114     while (hour >= 24)
115         hour -= 24;
116
117     sprintf(_indicated_string, "%02d:%02d:%02d", hour, min, sec);
118     _string_node->setStringValue(_indicated_string);
119     sprintf(_indicated_short_string, "%02d:%02d", hour, min);
120     _string_node1->setStringValue(_indicated_short_string);
121     sprintf(_local_short_string, "%02d:%02d", lhour, min);
122     _string_node2->setStringValue(_local_short_string);
123     _is_serviceable = true;
124
125     _indicated_min = min;
126     _min_node->setLongValue(_indicated_min);
127     _indicated_hour = hour;
128     _hour_node->setLongValue(_indicated_hour);
129     _local_hour = lhour;
130     _lhour_node->setLongValue(_local_hour);
131 }
132
133
134 // end of clock.cxx