我有一个带有 DateRanges 谓词的简单 NEST 查询:
DateTimeOffset dt;
...
.DateRange(r => r
.Field("Time")
.LessThanOrEquals(dt)
根据 this question ,简单地将 DateTimeOffset
转换为 DateTime
可能会导致数据丢失。 LessThanOrEquals
和其他谓词方法接受 Nest.DateMath
类型的对象,该类型具有以下运算符:
public static implicit operator DateMath(DateTime dateTime);
public static implicit operator DateMath(string dateMath);
此外DateRangeQueryDescriptor
还有一个额外的方法:
public DateRangeQueryDescriptor<T> TimeZone(string timeZone);
但我不确定如何使用它。
问题:
我如何使用 DateTimeOffset
对象作为 NEST
中的 DateRange
谓词的值,并确保发送到 Elastic 的日期是正确的?
最佳答案
显然,我错了。
根据 this和 this ,所有日期都存储或必须存储为 UTC,因此范围参数也应作为 UTC 传递。
Internally, dates are converted to UTC (if the time-zone is specified) and stored as a long number representing milliseconds-since-the-epoch.
关于c# - 在 NEST DateRange 谓词中使用 DateTimeOffset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48008777/