Comparing two DateTimeOffsets?
Asked Answered
S

2

0

I have two DateTimeOffset objects, which return like so when logging them:

TIME 1 (db):     3/17/2016 2:25:37 PM +00:00
TIME 2 (client): 3/17/2016 2:09:19 PM -04:00
TIME 2 < TIME 1? False

I don't want to clutter this with code, but comparison is made like this (as per MSDN):

bool didUserLoginBeforeSysUpdate = clientLoginTime < lastSystemUpdateTimeFromDb;

I think my main misunderstanding comes from whether or not it's factoring in the time zone adjustment (+00:00/-04:00) in the comparison. My understanding is that it does NOT and compares the raw times as per this SO post.

However, the timezone part is ignored when comparisons are made. In other words, each date time is converted to GMT / UTC and those are compared.

So, in short, what am I doing wrong? Why isn't TIME 2 showing as being earlier than TIME 1?

EDIT:

    bool didUserLoginBeforeSysUpdate = clientLoginTime.ToUniversalTime() < lastSystemUpdateTimeFromDb.ToUniversalTime();

    var didUserLoginBeforeSysUpdateComparison = clientLoginTime.CompareTo(lastSystemUpdateTimeFromDb);

brkptTIME 1 (db):     3/17/2016 2:25:37 PM +00:00
TIME 2 (client): 3/17/2016 2:09:19 PM -04:00
TIME 1 AFTER UTC CONVERSION(db):     3/17/2016 2:25:37 PM +00:00
TIME 2 AFTER UTC CONVERSION(client): 3/17/2016 6:09:19 PM +00:00
TIME 2(UTC)  < TIME 1(UTC) ? False
COMPARISON RESULT - is TIME 2 earlier than TIME 1?: 1 Key: Earlier = -1, Same = 0, Later = 1

Looks like I am misunderstanding how UTC saves. I thought 3/17/2016 2:09:19 PM -04:00 meant that "Universal time is 2:09:19 pm, and the local time where it was stored is 4 hours earlier. Anyway, I need to ask a different question, thanks for the help guys.

Selfforgetful answered 17/3, 2016 at 14:35 Comment(6)
I think you misunderstood that answer, it says it converts the DateTimes to UTC it doesn't just throw away the time zone adjustment. The adjustment is happening automaticallyForgetful
That's a really confusingly worded post - the "each date time is converted to GMT / UTC and those are compared" is correct, but that contradicts the "the timezone part is ignored" bit...Ionize
TIME 2 is later than TIME 1 and that is also the result you get by doing the comparison, e.g. false. So what are you doing wrong? Nothing, except you seem to believe that TIME 2 is before TIME 1.Guarani
@MartinLiversage: See my edit, I didn't understand how UTC works.Selfforgetful
@VSO: -04:00 means that the time zone is 4 hours after UTC, e.g. when the earth rotates it first becomes midnight in the UTC time zone and then after 4 hours have passed it becomes midnight in -04:00. I.e., that time zone is geographically to the west of the Greenwich meridian.Guarani
@MartinLiversage: Thanks. I understood the concept of time zones and universal time, but I didn't understand the way it was implemented, or wasn't certain.Selfforgetful
E
2

I would convert the date times ToUniversalTime before comparing them, this way it will negate the time zone differences.

bool didUserLoginBeforeSysUpdate = clientLoginTime.ToUniversalTime()  < lastSystemUpdateTimeFromDb.ToUniversalTime();

https://msdn.microsoft.com/en-us/library/system.datetime.touniversaltime(v=vs.110).aspx

Emogene answered 17/3, 2016 at 14:44 Comment(3)
there is no need to convert DTOs to UTC DTs, the >/< operators work as expected.Magnetostriction
The < operator used when comparing two DateTimeOffsets it converts to UTC for you, this is overkillForgetful
Looks like converting them doesn't do anything, they are still the same "absolute" time - that's a misunderstanding on my part, not yours though.Selfforgetful
F
1

Use SomeDateTimeOffset.Compare(OtherDateTimeOffset)- it explicitly converts to UTC before doing the comparison. It returns less than zero if the first is earlier than the second, 0 if they're the same, and greater than zero if the first is later than the second.

Edit because I think I misunderstood your goal; if you mean to compare the literal times regardless of timezone, DateTime.Compare(SomeDateTimeOffset.DateTime, OtherDateTimeObject.DateTime) should do what you want.

Frustum answered 17/3, 2016 at 14:40 Comment(3)
I verified that both comparison return the same result. I appreciate the reply regardless, as I saw the docs and was wondering if I was missing something, but yea - both methods are the same.Selfforgetful
I don't see what is gained from using Compare() instead of < or >. they also take time zones into account.Magnetostriction
I don't think anything is gained. It's just different syntax. They return the same result, and Compare() is way more convoluted because you need to understand what the output int means (at least as far as I can tell from testing, see edit if bored).Selfforgetful

© 2022 - 2024 — McMap. All rights reserved.