TimeT: - SecondsSince2000 default "01. Jan 2000 00:00:00 UTC" have to be preset by TimeT.UtcNowToSecondsSince2000 or TimeT.UtcAnyToSecondsSince2000 or by TimeT.SecondsSince2000

This commit is contained in:
Thomas Wiedebusch
2024-05-02 10:57:07 +02:00
parent 3f8ed10235
commit 01655f9c33
3 changed files with 78 additions and 18 deletions
@@ -9,8 +9,19 @@ namespace Xylem.Common.Hardware.WaterMeter.Genesis.Registers.DataTypes
public class TimeT
{
/// <summary>
/// Seconds since 01. Jan 2000
/// 1. Get seconds since 01. Jan 2000 00:00:00 UTC previously set by any call by
/// <see cref="UtcNowToSecondsSince2000"/> or <see cref="UtcAnyToSecondsSince2000"/>
/// or directly by this routine delivering the seconds e.g. read from meter,
/// 2. Set UTC seconds to analyze the corresponding time in UTC and use the ToString to
/// read the converted result.
/// </summary>
/// <remarks date="2023-Jul-19" author="Thomas Wiedebusch">
/// - Used <see cref="TimeT"/> to calculate time in UTC based on 01. Jan 2000.
/// </remarks>
/// <remarks date="2024-May-02" author="Thomas Wiedebusch">
/// - Setting of seconds and conversion to corresponding UTC sine 2000 or return seconds
/// from set DataTimeUtc.
/// </remarks>
public Int32 SecondsSince2000
{
set
@@ -18,25 +29,74 @@ namespace Xylem.Common.Hardware.WaterMeter.Genesis.Registers.DataTypes
var secondsToTimeSpan = TimeSpan.FromSeconds(value);
DateTimeUtc = _fixedDateTime2000.Add(secondsToTimeSpan);
}
// DateTimeUtc has to be set in advance with seconds or by the call
// of UtcNowToSecondsSince2000 or UtcAnyToSecondsSince2000
get => UtcToSecondsSince2000();
}
/// <summary>
/// Get actual seconds (UTC now) since 01. Jan 2000 00:00:00 UTC.
/// </summary>
/// <remarks date="2024-May-02" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
public Int32 UtcNowToSecondsSince2000
{
get
{
DateTimeUtc = DateTime.UtcNow;
var timeSince2000Utc = DateTimeUtc - _fixedDateTime2000;
var secondsSince2000 = Convert.ToInt32(timeSince2000Utc.TotalSeconds);
return secondsSince2000;
return UtcToSecondsSince2000();
}
}
/// <summary>
/// The date and time of 01.Jan 2000 00:00:00
/// Common routine to convert given UTC timestamp to seconds since 01. Jan 2000 00:00:00 UTC.
/// </summary>
private readonly DateTime _fixedDateTime2000 = new DateTime(2000, 1, 1, 0, 0, 0);
/// <remarks date="2024-May-02" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
private Int32 UtcToSecondsSince2000()
{
var timeSince2000Utc = DateTimeUtc - _fixedDateTime2000;
var secondsSince2000 = Convert.ToInt32(timeSince2000Utc.TotalSeconds);
return secondsSince2000;
}
/// <summary>
/// Builds the seconds for a given timestamp based on 01.Jan.2000 00:00:00 UTC
/// </summary>
/// <param name="dateTimeUtc">time stamp dateTime for conversion</param>
/// <remarks date="2023-Jul-19" author="Thomas Wiedebusch">
/// - Optional input of dateTime to convert this to the time in UTC based on 01. Jan 2000.
/// </remarks>
public Int32 UtcAnyToSecondsSince2000(DateTime dateTimeUtc)
{
try
{
// limit dateTime to 01. Jan 2000 00:00:00 UTC
DateTimeUtc = dateTimeUtc < _fixedDateTime2000 ? _fixedDateTime2000 : dateTimeUtc;
return UtcToSecondsSince2000();
}
catch (Exception)
{
return 0;
}
}
/// <summary>
/// The date and time of 01.Jan 2000 00:00:00 UTC
/// </summary>
private static readonly DateTime _fixedDateTime2000 = new DateTime(2000, 1, 1, 0, 0, 0);
/// <summary>
/// Converted date and time to UTC using the FixedDateTime2000
/// and the SecondsSince2000
/// </summary>
public DateTime DateTimeUtc;
public DateTime DateTimeUtc
{
private set;
get;
} = _fixedDateTime2000;
/// <summary>
/// Get string of UTC date and time universal language with 24 hours format