How to compare two DateTimeOffSet?

I have a variable of type DateTimeOffSet. I would like to filter out all projects created after January 1, 2010.

So, I wrote the following query:

var _date = new DateTimeOffset(2010, 01, 01, 0, 0, 0, new TimeSpan(-7, 0, 0)); var projects = _repository.Find<Project> (x => x.CompanyId = CompId && x.CreatedOn > _date) .ToList(); 

But when I look at the database, this is the type of values ​​that I see:

 2001-01-25 05:21:46.4370000 -08:00 2005-06-17 00:00:00.0000000 -07:00 

Obviously, some of the values ​​have -08: 00 , while others have -07: 00 . So my previous request still matters? When I look at the result, filtering is done as I expect. The only concern is that the meaning of this biased part, maybe the result is good by chance.

I am not familiar with how DayeTimeOffSet works.

+5
source share
2 answers

So, is my previous query still relevant?

Yes. When you compare two DateTimeOffset values, it compares the "absolute" time. The documentation says this in terms of the UtcDateTime property. For example, from the op_GreaterThan documentation :

true if the UtcDateTime value on the left is greater than the UtcDateTime value on the right; otherwise false .

So, as long as you want this behavior (which I think is), you should be fine. (Admittedly, we don’t know where the query is executed - if it is LINQ to SQL or EF, then you will rely on the fact that you implement the same semantics, but I think that a reasonable expectation.)

+8
source

DateTimeOffset supports an offset from GMT at the time and place where the value was created. This means that "2001-01-25 05: 21: 46.4370000 -08: 00" was recorded at the place and time of year (relative to DST), which was 8 hours behind GMT, and "2005-06-17 00: 00 : 00.0000000 -07: 00 "was recorded at the place and time of the year, which was 7 hours behind GMT.

However, when performing comparisons, part of the time zone is ignored. In other words, each time of the date is converted to GMT / UTC, and they are compared.

+5
source

Source: https://habr.com/ru/post/1201846/


All Articles