Skip to content

Commit

Permalink
Merge pull request #816 from WildernessLabs/utcnow
Browse files Browse the repository at this point in the history
Update DateTime.Now to UtcNow
  • Loading branch information
adrianstevens authored Oct 26, 2023
2 parents a042819 + 6d75cb2 commit f9536fa
Show file tree
Hide file tree
Showing 15 changed files with 28 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class IdealPidController : PidControllerBase
/// <returns></returns>
public override float CalculateControlOutput()
{
var now = DateTime.Now;
var now = DateTime.UtcNow;

// time delta (how long since last calculation)
var dt = now - _lastUpdateTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public abstract class PidControllerBase : IPidController
/// </summary>
public PidControllerBase()
{
_lastUpdateTime = DateTime.Now;
_lastUpdateTime = DateTime.UtcNow;
_lastError = 0;
_integral = 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ public class StandardPidController : PidControllerBase
/// <returns></returns>
public override float CalculateControlOutput()
{
// init vars
float control = 0.0f;
var now = DateTime.Now;
var now = DateTime.UtcNow;

// time delta (how long since last calculation)
var dt = now - _lastUpdateTime;
// seconds is better than ticks to bring our calculations into perspective
//var seconds = (float)(dt.Ticks / 10000 / 1000);
var seconds = ((float)dt.Ticks / 10000f / 1000f);
var seconds = (dt.Ticks / 10000f / 1000f);

// if no time has passed, don't make any changes.
if (dt.Ticks <= 0.0) return _lastControlOutputValue;
Expand All @@ -53,7 +51,7 @@ public override float CalculateControlOutput()
var derivative = (DerivativeComponent * 60) * diff;

// add the appropriate corrections
control = ProportionalComponent * (error + integral + derivative);
var control = ProportionalComponent * (error + integral + derivative);

//
//Resolver.Log.Info("PID Control (preclamp): " + control.ToString("N4"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected void UpdateEvents(bool state)
{
if (state)
{
ButtonPressStart = DateTime.Now;
ButtonPressStart = DateTime.UtcNow;
RaisePressStarted();
}
else
Expand All @@ -101,7 +101,7 @@ protected void UpdateEvents(bool state)
return;
}

TimeSpan pressDuration = DateTime.Now - ButtonPressStart;
TimeSpan pressDuration = DateTime.UtcNow - ButtonPressStart;
ButtonPressStart = DateTime.MaxValue;

if (LongClickedThreshold > TimeSpan.Zero && pressDuration > LongClickedThreshold)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public LinearHallEffectTachometer(IDigitalInterruptPort inputPort, CircuitTermin

private void InputPortChanged(object sender, DigitalPortResult e)
{
var time = DateTime.Now;
var time = DateTime.UtcNow;

// if it's the very first read, set the time and bail out
if (numberOfReads == 0 && revolutionTimeStart == DateTime.MinValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1544,15 +1544,15 @@ public virtual async Task ShowBuffered()

isUpdating = true;

var timeSinceLastUpdate = DateTime.Now - lastUpdated;
var timeSinceLastUpdate = DateTime.UtcNow - lastUpdated;

if (timeSinceLastUpdate < DelayBetweenFrames)
{
await Task.Delay(DelayBetweenFrames - timeSinceLastUpdate);
}

await Task.Run(() => display.Show());
lastUpdated = DateTime.Now;
lastUpdated = DateTime.UtcNow;

if (isUpdateRequested)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class SnakeGame

public bool PlaySound { get; private set; }

readonly Random rand = new Random((int)DateTime.Now.Ticks);
readonly Random rand = new Random((int)DateTime.UtcNow.Ticks);

enum CellType : byte
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ public void Update(bool state)
{
if (state == true && State == false)
{ // save our press start time (for long press event)
buttonPressStart = DateTime.Now;
buttonPressStart = DateTime.UtcNow;

RaisePressStarted();
}
else if (state == false && State == true)
{ // calculate the press duration
TimeSpan pressDuration = DateTime.Now - buttonPressStart;
TimeSpan pressDuration = DateTime.UtcNow - buttonPressStart;

// reset press start time
buttonPressStart = DateTime.MaxValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public void Step(int steps)
{
double lastStepTime = 0;

startTime = DateTime.Now;
startTime = DateTime.UtcNow;

var isClockwise = steps >= 0;
this.steps = Math.Abs(steps);
Expand All @@ -160,7 +160,7 @@ public void Step(int steps)

while (currentStep < this.steps)
{
double elapsedMicroseconds = (DateTime.Now - startTime).TotalMilliseconds * 1000;
double elapsedMicroseconds = (DateTime.UtcNow - startTime).TotalMilliseconds * 1000;

if (elapsedMicroseconds - lastStepTime >= stepMicrosecondsDelay)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ private void SerialMessagePort_MessageReceived(object sender, SerialMessageData
Conditions = newConditions;
RequestPending = false;

if (UpdateInterval == null || DateTime.Now - lastUpdate >= UpdateInterval)
if (UpdateInterval == null || DateTime.UtcNow - lastUpdate >= UpdateInterval)
{
lastUpdate = DateTime.Now;
lastUpdate = DateTime.UtcNow;
if (!IsSampling)
RaiseEventsAndNotify(changeResult); // Only raise events directly if periodic sampling is not enabled, as sampling will also raise the event.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public virtual void MeasureDistance()
Thread.Sleep(1); //smallest amount of time we can wait

// Start Clock
tickStart = DateTime.Now.Ticks;
tickStart = DateTime.UtcNow.Ticks;
// Trigger device to measure distance via sonic pulse
triggerPort.State = false;
}
Expand All @@ -94,12 +94,12 @@ private void OnEchoPortChanged(object sender, DigitalPortResult e)
{
if (e.New.State)
{
tickStart = DateTime.Now.Ticks;
tickStart = DateTime.UtcNow.Ticks;
return;
}

// Calculate Difference
var elapsed = DateTime.Now.Ticks - tickStart;
var elapsed = DateTime.UtcNow.Ticks - tickStart;

// Return elapsed ticks
// x10 for ticks to micro sec
Expand All @@ -110,9 +110,6 @@ private void OnEchoPortChanged(object sender, DigitalPortResult e)
var newDistance = new Length(curDis, Length.UnitType.Centimeters);
Distance = newDistance;

//debug - remove
Resolver.Log.Info($"{elapsed}, {curDis}, {Distance}, {DateTime.Now.Ticks}");

//restore this before publishing to hide false results
// if (CurrentDistance < MinimumDistance || CurrentDistance > MaximumDistance)
// CurrentDistance = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,17 @@ private void SerialMessagePort_MessageReceived(object sender, SerialMessageData

Length.UnitType units = GetUnitsForSensor(sensorType);

ChangeResult<Length> changeResult = new ChangeResult<Length>()
ChangeResult<Length> changeResult = new()
{
New = new Length(value, units),
Old = Distance,
};

Distance = changeResult.New;

if (updateInterval == null || DateTime.Now - lastUpdate >= updateInterval)
if (updateInterval == null || DateTime.UtcNow - lastUpdate >= updateInterval)
{
lastUpdate = DateTime.Now;
lastUpdate = DateTime.UtcNow;
RaiseEventsAndNotify(changeResult);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ public void Update(bool state)
{
if (state == true && State == false)
{
buttonPressStart = DateTime.Now;
buttonPressStart = DateTime.UtcNow;

RaisePressStarted();
}
else if (state == false && State == true)
{
TimeSpan pressDuration = DateTime.Now - buttonPressStart;
TimeSpan pressDuration = DateTime.UtcNow - buttonPressStart;

buttonPressStart = DateTime.MaxValue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ protected override async Task<Speed> ReadSensor()
StopUpdating();
}

if (samples?.Count > 0 && (DateTime.Now - samples?.Peek().New.Time > NoWindTimeout))
if (samples?.Count > 0 && (DateTime.UtcNow - samples?.Peek().New.Time > NoWindTimeout))
{ //we've exceeded the no wind interval time
samples?.Clear(); //will force a zero reading
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ private void RainSensorPortChanged(object sender, DigitalPortResult e)
Old = RainDepth - DepthPerClick, //last reading, ClickCount will always be at least 1
};

if (DateTime.Now - lastUpdated >= UpdateInterval)
if (DateTime.UtcNow - lastUpdated >= UpdateInterval)
{
lastUpdated = DateTime.Now;
lastUpdated = DateTime.UtcNow;
RaiseEventsAndNotify(changeResult);
}
}
Expand Down

0 comments on commit f9536fa

Please sign in to comment.