I am working on an application that receives alerts from a server. While processing the alerts, I calculate the last time the an alert was sent by doing the following:
TimeSpan? alertDiffTimespan = DateTime.UtcNow - serverEventInfo.lastAlert
An accurate timespan is important because it allows me to figure out whether a server is active or not.
As of late, I noticed that sometimes serverEventInfo.lastAlert
is correct (i.e. being formatted as DateTime.UtcNow) and other times it is an hour ahead/behind the current time (i.e. DateTime.Now). I have tried the following:
TimeSpan? alertDiffTimeSpan = DateTime.UtcNow - serverEventInfo.lastAlert.Value.ToUniversalTime()
but then it means that all the values coming in that are set as DateTime.UtcNow
are formatted incorrectly but the ones set as DateTime.Now
are correct.
I have tried determining the DateTime.Kind as well, however it keeps flagging as Unspecified each time, so no distinction.
Is there anything I can do to standardise the incoming DateTime values within my application so that no matter whether a UtcNow
or a Now
value comes in, I can still process it correctly and get a correct alertDiffTimeSpan
?
The dates are in format {0:MM/dd/yyyy hh:mm:ss tt}
and set as follows String.Format("{0:MM/dd/yyyy hh:mm:ss tt}", DateTime.Now)
or String.Format("{0:MM/dd/yyyy hh:mm:ss tt}", DateTime.UtcNow)
on the server.