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
+8 -8
View File
@@ -565,8 +565,8 @@ namespace Xylem.Common.Ui.GenesisToolBox
_currentGenesis.ReLogin();
var dt = DateTime.UtcNow;
var msg = $@"{dt:yyyy-MM-dd HH:mm:ss}";
LogText($@"PC date time [UTC]: {msg}");
var msg = $@"{dt:yyyy-MM-dd HH:mm:ss} UTC";
LogText($@"PC date time: {msg}");
CalculateMeterDateTime(RegisterConverter.ByteArrayToValue<Int32>(
_currentGenesis.ReadRegister("SYSTEM_CalendarSeconds")));
@@ -591,7 +591,7 @@ namespace Xylem.Common.Ui.GenesisToolBox
var meterDateTime = new TimeT { SecondsSince2000 = secSince2000 };
var msg = meterDateTime.ToString();
Invoke(new Action(() => { tbxDateTime.Text = msg; }));
LogText($@"Meter date time [UTC]: {msg}");
LogText($@"Meter date time: {msg}");
}
/// <summary>
@@ -861,10 +861,10 @@ namespace Xylem.Common.Ui.GenesisToolBox
LogText("File is empty", logFileName);
}
foreach (var rawBat in rawDic)
{
//foreach (var rawBat in rawDic)
//{
}
//}
}
var sb = new StringBuilder();
@@ -1472,9 +1472,9 @@ namespace Xylem.Common.Ui.GenesisToolBox
LogText($"Station: {ddlLocation.Items[ddlLocation.SelectedIndex]}", filename);
var dt = DateTime.UtcNow;
msg = $@"{dt:yyyy-MM-dd HH:mm:ss}";
msg = $@"{dt:yyyy-MM-dd HH:mm:ss} UTC";
LogText($@"PC date time [UTC]: {msg}", filename);
LogText($@"PC date time: {msg}", filename);
_currentGenesis.ReLogin();
@@ -331,7 +331,7 @@ namespace Xylem.ServiceFwUpdate.Ui.ServiceFwUpdateSw
/// - Compare registers reactivated based on new interface (configuration.json) with new
/// "StaticType": “approximate”.
/// </remarks>
/// <remarks date="2024-Apr-29" author="Thomas Wiedebusch">
/// <remarks date="2024-May-02" author="Thomas Wiedebusch">
/// - Power correction for FW update to new version starting with R1.3.x for EMEA and
/// R2.1.x for NA applied.
/// </remarks>
@@ -2005,10 +2005,10 @@ namespace Xylem.ServiceFwUpdate.Ui.ServiceFwUpdateSw
Boolean retVal;
try
{
// get the time in UTC to program the time for the Cordonel in seconds since 01. Jan 2000
// get the time in UTC to program the time for the Cordonel in seconds since 01. Jan 2000 00:00:00 UTC
var teaTime = new TimeT();
// build byte array
var rawSeconds = RegisterConverter.ValueToByteArray(teaTime.SecondsSince2000);
var rawSeconds = RegisterConverter.ValueToByteArray(teaTime.UtcNowToSecondsSince2000);
retVal = _currentGenesis.WriteRegister("SYSTEM_CalendarSeconds", rawSeconds);
LogSuccessText(Resources.StrDateTimeSetupSucceeded + " " + teaTime);
}