]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/clock.cxx
don't be afraid of the comma operator!
[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     : _is_serviceable(true),
20       _gmt_time_sec(0),
21       _offset_sec(0),
22       _indicated_sec(0),
23       _indicated_min(0),
24       _indicated_hour(0),
25       _local_hour(0),
26       _standstill_offset(0),
27       name("clock"),
28       num(0)
29 {
30     _indicated_string[0] = '\0';
31
32     int i;
33     for ( i = 0; i < node->nChildren(); ++i ) {
34         SGPropertyNode *child = node->getChild(i);
35         string cname = child->getName();
36         string cval = child->getStringValue();
37         if ( cname == "name" ) {
38             name = cval;
39         } else if ( cname == "number" ) {
40             num = child->getIntValue();
41         } else {
42             SG_LOG( SG_INSTR, SG_WARN, "Error in clock config logic" );
43             if ( name.length() ) {
44                 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
45             }
46         }
47     }
48 }
49
50 Clock::Clock ()
51     : _is_serviceable(true),
52       _gmt_time_sec(0),
53       _offset_sec(0),
54       _indicated_sec(0),
55       _standstill_offset(0)
56 {
57     _indicated_string[0] = '\0';
58     _indicated_short_string[0] = '\0';
59 }
60
61 Clock::~Clock ()
62 {
63 }
64
65 void
66 Clock::init ()
67 {
68     string branch;
69     branch = "/instrumentation/" + name;
70
71     SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
72     _serviceable_node = node->getChild("serviceable", 0, true);
73     _offset_node = node->getChild("offset-sec", 0, true);
74     _sec_node = node->getChild("indicated-sec", 0, true);
75     _min_node = node->getChild("indicated-min", 0, true);
76     _hour_node = node->getChild("indicated-hour", 0, true);
77     _lhour_node = node->getChild("local-hour", 0, true);
78     _string_node = node->getChild("indicated-string", 0, true);
79     _string_node1 = node->getChild("indicated-short-string", 0, true);
80     _string_node2 = node->getChild("local-short-string", 0, true);
81 }
82
83 void
84 Clock::update (double delta_time_sec)
85 {
86     if (!_serviceable_node->getBoolValue()) {
87         if (_is_serviceable) {
88             _string_node->setStringValue("");
89             _is_serviceable = false;
90         }
91         return;
92     }
93
94     struct tm *t = globals->get_time_params()->getGmt();
95     int hour = t->tm_hour;
96     int min = t->tm_min;
97     int sec = t->tm_sec;
98
99     // compute local time zone hour
100     int tzoffset_hours = globals->get_time_params()->get_local_offset() / 3600;
101     int lhour = hour + tzoffset_hours;
102     if ( lhour < 0 ) { lhour += 24; }
103     if ( lhour >= 24 ) { lhour -= 24; }
104
105     long gmt = (hour * 60 + min) * 60 + sec;
106     int offset = _offset_node->getLongValue();
107
108     if (!_is_serviceable) {
109         _standstill_offset -= gmt - _gmt_time_sec;
110     } else if (_gmt_time_sec == gmt && _offset_sec == offset)
111         return;
112
113     _gmt_time_sec = gmt;
114     _offset_sec = offset;
115
116     _indicated_sec = _gmt_time_sec + offset + _standstill_offset;
117     _sec_node->setLongValue(_indicated_sec);
118
119     sec += offset;
120     while (sec < 0) {
121         sec += 60;
122         min--;
123     }
124     while (sec >= 60) {
125         sec -= 60;
126         min++;
127     }
128     while (min < 0) {
129         min += 60;
130         hour--;
131     }
132     while (min >= 60) {
133         min -= 60;
134         hour++;
135     }
136     while (hour < 0)
137         hour += 24;
138     while (hour >= 24)
139         hour -= 24;
140
141     sprintf(_indicated_string, "%02d:%02d:%02d", hour, min, sec);
142     _string_node->setStringValue(_indicated_string);
143     sprintf(_indicated_short_string, "%02d:%02d", hour, min);
144     _string_node1->setStringValue(_indicated_short_string);
145     sprintf(_local_short_string, "%02d:%02d", lhour, min);
146     _string_node2->setStringValue(_local_short_string);
147     _is_serviceable = true;
148
149     _indicated_min = min;
150     _min_node->setLongValue(_indicated_min);
151     _indicated_hour = hour;
152     _hour_node->setLongValue(_indicated_hour);
153     _local_hour = lhour;
154     _lhour_node->setLongValue(_local_hour);
155
156     
157 }
158
159
160 // end of clock.cxx