Skip to content

Commit 762da88

Browse files
committed
refactor: use DateTimeOffset methods for Unix timestamp calculations
1 parent 8e1a319 commit 762da88

8 files changed

Lines changed: 17 additions & 24 deletions

File tree

TeslaFi-Import/Program.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -517,10 +517,7 @@ internal static void InsertPos(DateTime date, double latitude, double longitude,
517517

518518
public static DateTime UnixToDateTime(long t)
519519
{
520-
DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
521-
dt = dt.AddMilliseconds(t);
522-
dt = dt.ToLocalTime();
523-
return dt;
520+
return DateTimeOffset.FromUnixTimeMilliseconds(t).LocalDateTime;
524521

525522
}
526523

TeslaLogger/CO2.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ internal int GetData(string country, DateTime dateTime)
7171

7272
Newtonsoft.Json.Linq.JArray unixtimes = j[0]["xAxisValues"];
7373

74-
long unixTimestamp = (long)(dateTime.ToUniversalTime().Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
74+
long unixTimestamp = new DateTimeOffset(dateTime.ToUniversalTime()).ToUnixTimeSeconds();
7575
unixTimestamp *= 1000;
7676

7777
int ix = 0;
@@ -219,7 +219,7 @@ private static void GetImport(string country, DateTime dateTime, ref double co2s
219219

220220
Newtonsoft.Json.Linq.JArray unixtimes = j[0]["xAxisValues"];
221221

222-
long unixTimestamp = (long)(dateTime.ToUniversalTime().Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
222+
long unixTimestamp = new DateTimeOffset(dateTime.ToUniversalTime()).ToUnixTimeSeconds();
223223
unixTimestamp *= 1000;
224224

225225
int ix = 0;

TeslaLogger/Car.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,7 @@ private void HandleState_Online()
11001100
{
11011101
Tools.DebugLog($"charging_state: {charging_state[TeslaAPIState.Key.Value]}");
11021102
// check if charging_state value is not older than 1 minute
1103-
long now = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
1103+
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
11041104
if (long.TryParse(charging_state[TeslaAPIState.Key.ValueLastUpdate].ToString(), out long valueLastUpdate))
11051105
{
11061106
Tools.DebugLog($"charging_state now {now} vlu {valueLastUpdate} diff {now - valueLastUpdate}");
@@ -1121,7 +1121,7 @@ private void HandleState_Online()
11211121
if (GetTeslaAPIState().GetBool("charge_port_door_open", out bool bcharge_port_door_open) && bcharge_port_door_open)
11221122
{
11231123
//Tools.DebugLog($"charge_port_door_open: {charge_port_door_open[TeslaAPIState.Key.Value]}");
1124-
long now = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
1124+
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
11251125
// check if charge_port_door_open value True is not older than 1 minute
11261126
if (long.TryParse(charge_port_door_open[TeslaAPIState.Key.ValueLastUpdate].ToString(), out long valueLastUpdate))
11271127
{

TeslaLogger/DBHelper.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4939,7 +4939,7 @@ internal void InsertCharging(string timestamp, string battery_level, string char
49394939
if (charging_state[TeslaAPIState.Key.Value].ToString() == "Charging")
49404940
{
49414941
// check if charging_state value Charging is not older than 5 minutes
4942-
long now = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
4942+
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
49434943
if (long.TryParse(charging_state[TeslaAPIState.Key.ValueLastUpdate].ToString(), out long valueLastUpdate))
49444944
{
49454945
if (now - valueLastUpdate < 300000)
@@ -5125,10 +5125,7 @@ public static int CalculatePower(int voltage, int phases, int current)
51255125

51265126
public static DateTime UnixToDateTime(long t)
51275127
{
5128-
DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
5129-
dt = dt.AddMilliseconds(t);
5130-
dt = dt.ToLocalTime();
5131-
return dt;
5128+
return DateTimeOffset.FromUnixTimeMilliseconds(t).LocalDateTime;
51325129

51335130
}
51345131

TeslaLogger/TelemetryParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,7 @@ public void InsertLocation(dynamic j, DateTime d, string resultContent, bool for
12941294

12951295
public static long DateTimeToUTC_UnixTimestamp(DateTime d)
12961296
{
1297-
return (long)(d.ToUniversalTime().Subtract(new DateTime(1970, 1, 1))).TotalSeconds * 1000;
1297+
return new DateTimeOffset(d.ToUniversalTime()).ToUnixTimeMilliseconds();
12981298
}
12991299

13001300
void InsertLastLocation(DateTime d, bool loggingPosId = true)

TeslaLogger/TeslaAPIState.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ public bool GetState(string name, out Dictionary<Key, object> state, int maxage
222222
state = storage[name];
223223
if (maxage != 0)
224224
{
225-
long now = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
225+
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
226226
if (long.TryParse(storage[name][Key.Timestamp].ToString(), out long ts) && now - ts > maxage)
227227
{
228228
return false;
@@ -277,7 +277,7 @@ public bool GetBool(string name, out bool value, int maxage = 0)
277277
{
278278
if (maxage != 0)
279279
{
280-
long now = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
280+
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
281281
if (long.TryParse(storage[name][Key.Timestamp].ToString(), out long ts) && now - ts > maxage)
282282
{
283283
value = false;
@@ -310,7 +310,7 @@ public bool GetInt(string name, out int value, int maxage = 0)
310310
{
311311
if (maxage != 0)
312312
{
313-
long now = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
313+
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
314314
if (long.TryParse(storage[name][Key.Timestamp].ToString(), out long ts) && now - ts > maxage)
315315
{
316316
value = int.MinValue;
@@ -343,7 +343,7 @@ public bool GetDouble(string name, out double value, int maxage = 0)
343343
{
344344
if (maxage != 0)
345345
{
346-
long now = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
346+
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
347347
if (long.TryParse(storage[name][Key.Timestamp].ToString(), out long ts) && now - ts > maxage)
348348
{
349349
value = double.NaN;
@@ -376,7 +376,7 @@ public bool GetString(string name, out string value, int maxage = 0)
376376
{
377377
if (maxage != 0)
378378
{
379-
long now = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
379+
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
380380
if (long.TryParse(storage[name][Key.Timestamp].ToString(), out long ts) && now - ts > maxage)
381381
{
382382
value = string.Empty;
@@ -1536,7 +1536,7 @@ public string ToString(bool compareTs = false)
15361536
&& long.TryParse(storage[key][Key.Timestamp].ToString(), out long ts) && ts != 0
15371537
&& long.TryParse(storage[key][Key.ValueLastUpdate].ToString(), out long vlu) && vlu != 0)
15381538
{
1539-
long now = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
1539+
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
15401540
str += string.Concat($"{key} => v:[{storage[key][Key.Value]}] t:{storage[key][Key.Type]} s:{storage[key][Key.Source]} ts:{storage[key][Key.Timestamp]} now:{now} diff:{now - ts}ms vlu:{storage[key][Key.ValueLastUpdate]} now:{now} diff:{now - vlu}ms", Environment.NewLine);
15411541
}
15421542
else
@@ -1557,7 +1557,7 @@ internal long GetTimestampAge(string source)
15571557
maxTS = (long)((maxTS < (long)storage[property][Key.Timestamp]) ? storage[property][Key.Timestamp] : maxTS);
15581558
}
15591559
}
1560-
long now = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
1560+
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
15611561
return maxTS > 0 ? now - maxTS : 0;
15621562
}
15631563

TeslaLogger/Tools.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static void SetThreadEnUS()
6363

6464
public static long ToUnixTime(DateTime dateTime)
6565
{
66-
return (long)(dateTime - new DateTime(1970, 1, 1)).TotalSeconds;
66+
return new DateTimeOffset(dateTime).ToUnixTimeSeconds();
6767
}
6868

6969
public static void DebugLog(MySqlCommand cmd, string prefix = "", [CallerFilePath] string callerFilePath = null, [CallerLineNumber] int callerLineNumber = 0, [CallerMemberName] string callerMemberName = null)

UnitTestsTeslalogger/UnitTestBase.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ public void TestJapanese()
2424
Car c = new Car(0, "", "", 0, "", DateTime.Now, "", "", "", "", "", "", "", null, false);
2525

2626
Tools.SetThreadEnUS();
27-
long unixTimestamp = (long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
28-
unixTimestamp *= 1000;
27+
long unixTimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
2928
c.DbHelper.InsertPos(unixTimestamp.ToString(), 48.456691, 10.030241, 0, 0, 1, 0, 0, 0, 0, 0, "0");
3029
int startid = c.DbHelper.GetMaxPosid(true);
3130
c.DbHelper.StartDriveState(DateTime.Now);

0 commit comments

Comments
 (0)