NEST 中的时间单位

英文原文地址:Time units

与 Elasticsearch 交互,我们会遇到需要设定时间段的情况(例如:timeout 参数)。为了指定时间段,我们可以使用一个表示时间的整数(以毫秒为单位),也可以使用一个时间值(例如:2d 表示 2 天)。

NEST 使用一个 Time 类型来满足上述需求,有好几种方式来构造 Time 对象。

Constructor

最直接的方式当然是构造函数了,参数可以是列表中的任意一种:

字符串

double 类型的毫秒值

因子和间隔的组合(其实就是数字和单位)

TimeSpan 对象

var unitString = new Time("2d"); var unitComposed = new Time(2, Nest.TimeUnit.Day); var unitTimeSpan = new Time(TimeSpan.FromDays(2)); var unitMilliseconds = new Time(1000 * 60 * 60 * 24 * 2);

Time 对象序列化后的结果是一个由因子和间隔构成的字符串,如:上面这段代码中的变量序列化的结果都是 2d

即使没有使用 Time(double ms) 这个构造函数,Time 的 Milliseconds 属性也会被计算。

unitMilliseconds.Milliseconds.Should().Be(1000*60*60*24*2); unitComposed.Milliseconds.Should().Be(1000*60*60*24*2); unitTimeSpan.Milliseconds.Should().Be(1000*60*60*24*2); unitString.Milliseconds.Should().Be(1000*60*60*24*2); Implicit conversion

NEST 提供了从 string TimeSpan double 到 Time 的隐式转换,提高了易用性。

Time oneAndHalfYear = "1.5y"; Time fourteenDays = TimeSpan.FromDays(14); Time twoDays = 1000*60*60*24*2; Expect("1.5y").WhenSerializing(oneAndHalfYear); Expect("14d").WhenSerializing(fourteenDays); Expect("2d").WhenSerializing(twoDays); Equality and Comparison

处理 年和月 的时候,是无法精确计算毫秒数的,Milliseconds 的值为 -1 。这是因为年和月不是固定的值,举个栗子

一月(不是一月份)可能有 30 天、31 天、28 天、29 天

一年可能有 365 天、366 天

Time oneAndHalfYear = "1.5y"; oneAndHalfYear.Milliseconds.Should().Be(-1);

你可以将两个 Time 对象做比较

Time twoDays = 1000*60*60*24*2; oneAndHalfYear.Should().BeGreaterThan(fourteenDays); (oneAndHalfYear > fourteenDays).Should().BeTrue(); (oneAndHalfYear >= fourteenDays).Should().BeTrue(); (twoDays != null).Should().BeTrue(); (twoDays >= new Time("2d")).Should().BeTrue(); twoDays.Should().BeLessThan(fourteenDays); (twoDays < fourteenDays).Should().BeTrue(); (twoDays <= fourteenDays).Should().BeTrue(); (twoDays <= new Time("2d")).Should().BeTrue();

判定相等性

twoDays.Should().Be(new Time("2d")); (twoDays == new Time("2d")).Should().BeTrue(); (twoDays != new Time("2.1d")).Should().BeTrue(); (new Time("2.1d") == new Time(TimeSpan.FromDays(2.1))).Should().BeTrue();

相等的精确度为 0.1 纳秒

Time oneNanosecond = new Time(1, Nest.TimeUnit.Nanoseconds); Time onePointNoughtNineNanoseconds = "1.09nanos"; Time onePointOneNanoseconds = "1.1nanos"; (oneNanosecond == onePointNoughtNineNanoseconds).Should().BeTrue(); (oneNanosecond == onePointOneNanoseconds).Should().BeFalse(); Special Time Values

Elasticsearch 有两个特殊的时间值

0 通过 Time.Zero 表示

-1 通过 Time.MinusOne 表示

下面的对象都等于 Time.MinusOne

Time.MinusOne.Should().Be(Time.MinusOne); new Time("-1").Should().Be(Time.MinusOne); new Time(-1).Should().Be(Time.MinusOne); ((Time) (-1)).Should().Be(Time.MinusOne); ((Time) "-1").Should().Be(Time.MinusOne); ((Time) (-1)).Should().Be((Time) "-1");

下面的对象都等于 Time.Zero

Time.Zero.Should().Be(Time.Zero); new Time("0").Should().Be(Time.Zero); new Time(0).Should().Be(Time.Zero); ((Time) 0).Should().Be(Time.Zero); ((Time) "0").Should().Be(Time.Zero); ((Time) 0).Should().Be((Time) "0");

特殊的时间值 0 和 -1 可以和其他 Time 值做比较,尽管这看起来很荒谬。

var twoDays = new Time(2, Nest.TimeUnit.Day); Time.MinusOne.Should().BeLessThan(Time.Zero); Time.Zero.Should().BeGreaterThan(Time.MinusOne); Time.Zero.Should().BeLessThan(twoDays); Time.MinusOne.Should().BeLessThan(twoDays);

如果需要构造一个 -1ms 或者 0ms 的时间,使用接收因子和间隔的构造函数,或者指定一个 ms 字符串

(new Time(-1, Nest.TimeUnit.Millisecond) == new Time("-1ms")).Should().BeTrue(); (new Time(0, Nest.TimeUnit.Millisecond) == new Time("0ms")).Should().BeTrue(); Units of Time

可以使用一个 DateInterval 和 Time 构成的共用体 Union<DateInterval, Time> 来指定一个 Time 的单位,它支持 Time 和 DateInterval 的隐式转换

Expect("month").WhenSerializing<Union<DateInterval, Time>>(DateInterval.Month); Expect("day").WhenSerializing<Union<DateInterval, Time>>(DateInterval.Day); Expect("hour").WhenSerializing<Union<DateInterval, Time>>(DateInterval.Hour); Expect("minute").WhenSerializing<Union<DateInterval, Time>>(DateInterval.Minute); Expect("quarter").WhenSerializing<Union<DateInterval, Time>>(DateInterval.Quarter); Expect("second").WhenSerializing<Union<DateInterval, Time>>(DateInterval.Second); Expect("week").WhenSerializing<Union<DateInterval, Time>>(DateInterval.Week); Expect("year").WhenSerializing<Union<DateInterval, Time>>(DateInterval.Year); Expect("2d").WhenSerializing<Union<DateInterval, Time>>((Time)"2d"); Expect("11664m").WhenSerializing<Union<DateInterval, Time>>((Time)TimeSpan.FromDays(8.1));

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpwxpj.html