Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions TeslaFi-Import/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -517,10 +517,7 @@ internal static void InsertPos(DateTime date, double latitude, double longitude,

public static DateTime UnixToDateTime(long t)
{
DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
dt = dt.AddMilliseconds(t);
dt = dt.ToLocalTime();
return dt;
return DateTimeOffset.FromUnixTimeMilliseconds(t).LocalDateTime;

}

Expand Down
4 changes: 2 additions & 2 deletions TeslaLogger/CO2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ internal int GetData(string country, DateTime dateTime)

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

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

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

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

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

int ix = 0;
Expand Down
4 changes: 2 additions & 2 deletions TeslaLogger/Car.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,7 @@ private void HandleState_Online()
{
Tools.DebugLog($"charging_state: {charging_state[TeslaAPIState.Key.Value]}");
// check if charging_state value is not older than 1 minute
long now = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
if (long.TryParse(charging_state[TeslaAPIState.Key.ValueLastUpdate].ToString(), out long valueLastUpdate))
{
Tools.DebugLog($"charging_state now {now} vlu {valueLastUpdate} diff {now - valueLastUpdate}");
Expand All @@ -1121,7 +1121,7 @@ private void HandleState_Online()
if (GetTeslaAPIState().GetBool("charge_port_door_open", out bool bcharge_port_door_open) && bcharge_port_door_open)
{
//Tools.DebugLog($"charge_port_door_open: {charge_port_door_open[TeslaAPIState.Key.Value]}");
long now = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
// check if charge_port_door_open value True is not older than 1 minute
if (long.TryParse(charge_port_door_open[TeslaAPIState.Key.ValueLastUpdate].ToString(), out long valueLastUpdate))
{
Expand Down
7 changes: 2 additions & 5 deletions TeslaLogger/DBHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4939,7 +4939,7 @@ internal void InsertCharging(string timestamp, string battery_level, string char
if (charging_state[TeslaAPIState.Key.Value].ToString() == "Charging")
{
// check if charging_state value Charging is not older than 5 minutes
long now = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
if (long.TryParse(charging_state[TeslaAPIState.Key.ValueLastUpdate].ToString(), out long valueLastUpdate))
{
if (now - valueLastUpdate < 300000)
Expand Down Expand Up @@ -5125,10 +5125,7 @@ public static int CalculatePower(int voltage, int phases, int current)

public static DateTime UnixToDateTime(long t)
{
DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
dt = dt.AddMilliseconds(t);
dt = dt.ToLocalTime();
return dt;
return DateTimeOffset.FromUnixTimeMilliseconds(t).LocalDateTime;

}

Expand Down
2 changes: 1 addition & 1 deletion TeslaLogger/TelemetryParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1294,7 +1294,7 @@ public void InsertLocation(dynamic j, DateTime d, string resultContent, bool for

public static long DateTimeToUTC_UnixTimestamp(DateTime d)
{
return (long)(d.ToUniversalTime().Subtract(new DateTime(1970, 1, 1))).TotalSeconds * 1000;
return new DateTimeOffset(d.ToUniversalTime()).ToUnixTimeMilliseconds();
}

void InsertLastLocation(DateTime d, bool loggingPosId = true)
Expand Down
14 changes: 7 additions & 7 deletions TeslaLogger/TeslaAPIState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public bool GetState(string name, out Dictionary<Key, object> state, int maxage
state = storage[name];
if (maxage != 0)
{
long now = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
if (long.TryParse(storage[name][Key.Timestamp].ToString(), out long ts) && now - ts > maxage)
{
return false;
Expand Down Expand Up @@ -277,7 +277,7 @@ public bool GetBool(string name, out bool value, int maxage = 0)
{
if (maxage != 0)
{
long now = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
if (long.TryParse(storage[name][Key.Timestamp].ToString(), out long ts) && now - ts > maxage)
{
value = false;
Expand Down Expand Up @@ -310,7 +310,7 @@ public bool GetInt(string name, out int value, int maxage = 0)
{
if (maxage != 0)
{
long now = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
if (long.TryParse(storage[name][Key.Timestamp].ToString(), out long ts) && now - ts > maxage)
{
value = int.MinValue;
Expand Down Expand Up @@ -343,7 +343,7 @@ public bool GetDouble(string name, out double value, int maxage = 0)
{
if (maxage != 0)
{
long now = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
if (long.TryParse(storage[name][Key.Timestamp].ToString(), out long ts) && now - ts > maxage)
{
value = double.NaN;
Expand Down Expand Up @@ -376,7 +376,7 @@ public bool GetString(string name, out string value, int maxage = 0)
{
if (maxage != 0)
{
long now = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
if (long.TryParse(storage[name][Key.Timestamp].ToString(), out long ts) && now - ts > maxage)
{
value = string.Empty;
Expand Down Expand Up @@ -1536,7 +1536,7 @@ public string ToString(bool compareTs = false)
&& long.TryParse(storage[key][Key.Timestamp].ToString(), out long ts) && ts != 0
&& long.TryParse(storage[key][Key.ValueLastUpdate].ToString(), out long vlu) && vlu != 0)
{
long now = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
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);
}
else
Expand All @@ -1557,7 +1557,7 @@ internal long GetTimestampAge(string source)
maxTS = (long)((maxTS < (long)storage[property][Key.Timestamp]) ? storage[property][Key.Timestamp] : maxTS);
}
}
long now = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
return maxTS > 0 ? now - maxTS : 0;
}

Expand Down
2 changes: 1 addition & 1 deletion TeslaLogger/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static void SetThreadEnUS()

public static long ToUnixTime(DateTime dateTime)
{
return (long)(dateTime - new DateTime(1970, 1, 1)).TotalSeconds;
return new DateTimeOffset(dateTime).ToUnixTimeSeconds();
}

public static void DebugLog(MySqlCommand cmd, string prefix = "", [CallerFilePath] string callerFilePath = null, [CallerLineNumber] int callerLineNumber = 0, [CallerMemberName] string callerMemberName = null)
Expand Down
3 changes: 1 addition & 2 deletions UnitTestsTeslalogger/UnitTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public void TestJapanese()
Car c = new Car(0, "", "", 0, "", DateTime.Now, "", "", "", "", "", "", "", null, false);

Tools.SetThreadEnUS();
long unixTimestamp = (long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
unixTimestamp *= 1000;
long unixTimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
c.DbHelper.InsertPos(unixTimestamp.ToString(), 48.456691, 10.030241, 0, 0, 1, 0, 0, 0, 0, 0, "0");
int startid = c.DbHelper.GetMaxPosid(true);
c.DbHelper.StartDriveState(DateTime.Now);
Expand Down