• R/O
  • HTTP
  • SSH
  • HTTPS

yubeshi: Commit

source codes and tests


Commit MetaInfo

Revisionbff98ae058fcc2ee250200af743408549afb186f (tree)
Time2011-02-23 14:52:54
Authorkimikage <kimikage_ceo@hotm...>
Commiterkimikage

Log Message

GPS時刻の追加

Change Summary

Incremental Difference

--- /dev/null
+++ b/Yubeshi/GpsTime.cs
@@ -0,0 +1,218 @@
1+/*
2+ * Yubeshi GPS Parser
3+ *
4+ * This software is distributed under a zlib-style license.
5+ * See license.txt for more information.
6+ */
7+
8+using System;
9+using System.Collections.Generic;
10+using System.Text;
11+
12+namespace Yubeshi
13+{
14+ public struct GpsTime : IComparable
15+ {
16+ #region fields
17+ public static readonly DateTime Origin;
18+
19+ private int week;
20+ private decimal timeOfWeek;
21+ private bool rounded;
22+
23+ private static readonly DateTime[] leapTiming;
24+ private static int cumulativeLeapSecond;
25+
26+ // 1e7 * 60 * 60 * 24 * 7
27+ private const long ticksOfWeek = 6048000000000;
28+
29+ #endregion
30+
31+ #region constructors
32+
33+ public GpsTime(decimal timeOfWeek)
34+ : this(GpsTime.WeekOfToday, timeOfWeek, true)
35+ {
36+ }
37+
38+ public GpsTime(int week, decimal timeOfWeek)
39+ : this(week, timeOfWeek, week < 1024)
40+ {
41+ }
42+
43+ public GpsTime(int week, decimal timeOfWeek, bool rounded)
44+ {
45+ if (week < 0)
46+ {
47+ throw new ArgumentException("'week' must be non-negative.");
48+ }
49+ this.week = rounded ? week + 1024 : week;
50+ this.timeOfWeek = timeOfWeek;
51+ this.rounded = rounded;
52+ }
53+
54+ public GpsTime(DateTime time)
55+ {
56+ DateTime utc = time.ToUniversalTime();
57+ long ticks = (time - Origin).Ticks;
58+ if (ticks < 0)
59+ {
60+ throw new ArgumentException(
61+ "GpsTime represents times since 00:00:00, 6 Jan. 1980.");
62+ }
63+ for (int i = 0; i < leapTiming.Length; ++i)
64+ {
65+ if (utc < leapTiming[i])
66+ {
67+ break;
68+ }
69+ ticks += 10000000; // 1 / 100e-9
70+ }
71+
72+ rounded = false;
73+ week = (int)(ticks / ticksOfWeek);
74+ timeOfWeek = (ticks % ticksOfWeek) * 100e-9m;
75+ }
76+
77+ static GpsTime()
78+ {
79+ Origin = new DateTime(1980, 1, 6, 0, 0, 0);
80+ leapTiming = new DateTime[]{
81+ new DateTime(1981, 7, 1, 0, 0, 0, DateTimeKind.Utc),
82+ new DateTime(1982, 7, 1, 0, 0, 0, DateTimeKind.Utc),
83+ new DateTime(1983, 7, 1, 0, 0, 0, DateTimeKind.Utc),
84+ new DateTime(1985, 7, 1, 0, 0, 0, DateTimeKind.Utc),
85+ new DateTime(1988, 1, 1, 0, 0, 0, DateTimeKind.Utc),
86+ new DateTime(1990, 1, 1, 0, 0, 0, DateTimeKind.Utc),
87+ new DateTime(1991, 1, 1, 0, 0, 0, DateTimeKind.Utc),
88+ new DateTime(1992, 7, 1, 0, 0, 0, DateTimeKind.Utc),
89+ new DateTime(1993, 7, 1, 0, 0, 0, DateTimeKind.Utc),
90+ new DateTime(1994, 7, 1, 0, 0, 0, DateTimeKind.Utc),
91+ new DateTime(1996, 1, 1, 0, 0, 0, DateTimeKind.Utc),
92+ new DateTime(1997, 7, 1, 0, 0, 0, DateTimeKind.Utc),
93+ new DateTime(1999, 1, 1, 0, 0, 0, DateTimeKind.Utc),
94+ new DateTime(2006, 1, 1, 0, 0, 0, DateTimeKind.Utc),
95+ new DateTime(2009, 1, 1, 0, 0, 0, DateTimeKind.Utc)
96+ };
97+
98+ }
99+ #endregion
100+
101+ #region properties
102+ public int Week
103+ {
104+ get
105+ {
106+ return week;
107+ }
108+ }
109+
110+ public decimal TimeOfWeek
111+ {
112+ get
113+ {
114+ return timeOfWeek;
115+ }
116+ }
117+
118+ public bool Rounded
119+ {
120+ get
121+ {
122+ return rounded;
123+ }
124+ }
125+
126+ public static int CumulativeLeapSecond
127+ {
128+ get
129+ {
130+ return cumulativeLeapSecond;
131+ }
132+ set
133+ {
134+ cumulativeLeapSecond = value;
135+ }
136+ }
137+
138+ public static GpsTime Now
139+ {
140+ get
141+ {
142+ return new GpsTime(DateTime.UtcNow);
143+ }
144+ }
145+
146+ public static int WeekOfToday
147+ {
148+ get
149+ {
150+ return Now.Week;
151+ }
152+ }
153+ #endregion
154+
155+ #region public methods
156+
157+ public int CompareTo(object value)
158+ {
159+ GpsTime otherTime;
160+ if (value is GpsTime)
161+ {
162+ otherTime = (GpsTime)value;
163+ }
164+ else if(value is DateTime)
165+ {
166+ otherTime = new GpsTime((DateTime)value);
167+ }
168+ else
169+ {
170+ throw new ArgumentException("'value' is not a time.");
171+ }
172+
173+ if (rounded || otherTime.rounded)
174+ {
175+ throw new ArgumentException("values must be absolute time.");
176+ }
177+
178+ if (week == otherTime.week)
179+ {
180+ return timeOfWeek.CompareTo(otherTime.timeOfWeek);
181+ }
182+ return week.CompareTo(otherTime.week);
183+ }
184+
185+ public DateTime ToLocalTime()
186+ {
187+ return ToUtc().ToLocalTime();
188+ }
189+
190+ public DateTime ToUtc()
191+ {
192+ long ticks = week * ticksOfWeek + (long)(timeOfWeek * 1e7m);
193+ ticks += Origin.Ticks;
194+ DateTime time = new DateTime(ticks, DateTimeKind.Utc);
195+
196+ TimeSpan oneSecond = new TimeSpan(0, 0, 0, 1);
197+ for (int i = 0; i < leapTiming.Length; ++i)
198+ {
199+ if (time < leapTiming[i])
200+ {
201+ break;
202+ }
203+ time -= oneSecond;
204+ }
205+ return time;
206+ }
207+
208+ public int LeapSecond
209+ {
210+ get
211+ {
212+ return 0;
213+ }
214+ }
215+
216+ #endregion
217+ }
218+}
--- a/Yubeshi/Yubeshi.csproj
+++ b/Yubeshi/Yubeshi.csproj
@@ -43,6 +43,7 @@
4343 <Compile Include="GeodeticCoordinate.cs" />
4444 <Compile Include="EcefCoordinate.cs" />
4545 <Compile Include="EcefVelocity.cs" />
46+ <Compile Include="GpsTime.cs" />
4647 <Compile Include="Gps\TelemetryWord.cs" />
4748 <Compile Include="Gps\Word.cs" />
4849 <Compile Include="Gps\Subframe.cs" />
@@ -81,6 +82,12 @@
8182 <Compile Include="Ubx\RxmAlm.cs" />
8283 <Compile Include="Ubx\CfgRate.cs" />
8384 <Compile Include="Ubx\AidAlm.cs" />
85+ <Compile Include="Ubx\MonHw2.cs" />
86+ <Compile Include="Ubx\InfDebug.cs" />
87+ <Compile Include="Ubx\InfError.cs" />
88+ <Compile Include="Ubx\InfWarning.cs" />
89+ <Compile Include="Ubx\InfNotice.cs" />
90+ <Compile Include="Ubx\InfTest.cs" />
8491 <Compile Include="UnknownPacket.cs" />
8592 <Compile Include="EnuVelocity.cs" />
8693 <Compile Include="YumaAlmanacReader.cs" />
--- /dev/null
+++ b/YubeshiTest/GpsTimeTest.cs
@@ -0,0 +1,68 @@
1+/*
2+ * Yubeshi GPS Parser
3+ *
4+ * This software is distributed under a zlib-style license.
5+ * See license.txt for more information.
6+ */
7+
8+using System;
9+using System.Collections.Generic;
10+using System.Text;
11+using NUnit.Framework;
12+using Yubeshi;
13+
14+namespace YubeshiTest
15+{
16+ class GpsTimeTest
17+ {
18+ private static readonly DateTime utc =
19+ new DateTime(2010, 1, 23, 4, 56, 7, 890, DateTimeKind.Utc);
20+ private static readonly DateTime utcWrapAround =
21+ new DateTime(1999, 8, 21, 23, 59, 47, DateTimeKind.Utc);
22+
23+ [Test]
24+ public void LeapSecond()
25+ {
26+ TimeSpan span1 = new DateTime(1991, 1, 1) -
27+ new DateTime(1990, 12, 31);
28+ TimeSpan span2 = new DateTime(1992, 1, 1) -
29+ new DateTime(1991, 12, 31);
30+ // DateTime does not support leap seconds
31+ Assert.AreEqual(span1.Ticks, span2.Ticks);
32+ }
33+
34+ [Test]
35+ public void FromUTC()
36+ {
37+ GpsTime gps = new GpsTime(utc);
38+ Assert.AreEqual(false, gps.Rounded);
39+ Assert.AreEqual(1567, gps.Week);
40+ Assert.AreEqual((4 * 60 + 56) * 60 + 7.89m,
41+ gps.TimeOfWeek % (60m * 60m * 24m) - 15m);
42+
43+ GpsTime gpsWrapAround = new GpsTime(utcWrapAround);
44+ Assert.AreEqual(false, gpsWrapAround.Rounded);
45+ Assert.AreEqual(1024, gpsWrapAround.Week);
46+ Assert.AreEqual(0, gpsWrapAround.TimeOfWeek);
47+ }
48+
49+ [Test]
50+ public void ToUtc()
51+ {
52+ GpsTime gps = new GpsTime(utc);
53+ Assert.AreEqual(utc, gps.ToUtc());
54+
55+ GpsTime gpsWrapAround = new GpsTime(1024, 0m);
56+ Assert.AreEqual(utcWrapAround, gpsWrapAround.ToUtc());
57+ }
58+
59+ [Test]
60+ public void Now()
61+ {
62+ GpsTime now = GpsTime.Now;
63+ TimeSpan dt = DateTime.Now - now.ToLocalTime();
64+ Assert.True(dt < new TimeSpan(0, 0, 0, 0, 100));
65+ }
66+
67+ }
68+}
--- a/YubeshiTest/YubeshiTest.csproj
+++ b/YubeshiTest/YubeshiTest.csproj
@@ -47,6 +47,7 @@
4747 <Compile Include="EcefCoordinateTest.cs" />
4848 <Compile Include="GeodeticCoordinateTest.cs" />
4949 <Compile Include="GpsTest\WordTest.cs" />
50+ <Compile Include="GpsTimeTest.cs" />
5051 <Compile Include="NmeaTest\PacketTest.cs" />
5152 <Compile Include="NmeaTest\ParserTest.cs" />
5253 <Compile Include="NmeaTest\SamplePackets.cs" />
@@ -60,6 +61,7 @@
6061 <Compile Include="UbxTest\RxmPacketTest.cs" />
6162 <Compile Include="DegreeTest.cs" />
6263 <Compile Include="UbxTest\UnknownPacketTest.cs" />
64+ <Compile Include="UbxTest\InfPacketTest.cs" />
6365 <Compile Include="YumaAlmanacReaderTest.cs" />
6466 </ItemGroup>
6567 <ItemGroup>
Show on old repository browser