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