NTP Analyzer  0.8.2
Analyze the operation of time servers
Reading.cs
Go to the documentation of this file.
1 //
2 // Copyright (c) 2013-2017 Carsten Sonne Larsen <cs@innolan.net>
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 using System;
23 
24 namespace Ntp.Analyzer.Objects
25 {
26  public abstract class Reading : PersistentObject
27  {
28  protected Reading(
29  int id,
30  DateTime time,
31  int zone,
32  Host host)
33  : base(id)
34  {
35  var adjusted = new DateTime(
36  time.Year, time.Month, time.Day,
37  time.Hour, time.Minute, time.Second,
38  time.Millisecond,
39  DateTimeKind.Utc);
40  Time = adjusted.AddMinutes(zone);
41  Host = host;
42  }
43 
44  protected Reading(
45  Host host,
46  ReadingBulk bulk
47  )
48  {
49  Time = bulk?.Time ?? DateTime.UtcNow;
50  Host = host;
51  }
52 
53  public Host Host { get; }
54 
55  public DateTime Time { get; }
56 
57  public DateTime UtcTime
58  {
59  get
60  {
61  switch (Time.Kind)
62  {
63  case DateTimeKind.Utc:
64  case DateTimeKind.Unspecified:
65  return Time;
66  case DateTimeKind.Local:
67  return Time.Subtract(TimeZoneInfo.Local.GetUtcOffset(DateTime.UtcNow));
68  default:
69  return Time;
70  }
71  }
72  }
73 
74  public DateTime LocalTime
75  {
76  get
77  {
78  switch (Time.Kind)
79  {
80  case DateTimeKind.Utc:
81  case DateTimeKind.Unspecified:
82  return UtcTime.Add(TimeZoneInfo.Local.GetUtcOffset(DateTime.UtcNow));
83  case DateTimeKind.Local:
84  return Time;
85  default:
86  return UtcTime.Add(TimeZoneInfo.Local.GetUtcOffset(DateTime.UtcNow));
87  }
88  }
89  }
90 
91  public int UtcOffset
92  {
93  get
94  {
95  switch (Time.Kind)
96  {
97  case DateTimeKind.Utc:
98  case DateTimeKind.Unspecified:
99  return 0;
100  case DateTimeKind.Local:
101  return Convert.ToInt32(TimeZoneInfo.Local.GetUtcOffset(DateTime.UtcNow).TotalMinutes);
102  default:
103  return 0;
104  }
105  }
106  }
107 
108  public DateTime RoundedUtcTime => new DateTime(
109  UtcTime.Year, UtcTime.Month, UtcTime.Day,
110  UtcTime.Hour, UtcTime.Minute, 0,
111  DateTimeKind.Utc);
112 
113  public DateTime RoundedLocalTime => new DateTime(
114  LocalTime.Year, LocalTime.Month, LocalTime.Day,
115  LocalTime.Hour, LocalTime.Minute, 0,
116  DateTimeKind.Local);
117  }
118 }
Reading(int id, DateTime time, int zone, Host host)
Definition: Reading.cs:28
Reading(Host host, ReadingBulk bulk)
Definition: Reading.cs:44