NEST 中的时间单位

Time units

Version: 5.xhtml

英文原文地址:Time unitsc#

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

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

Constructor

最直接的方式固然是构造函数了,参数能够是列表中的任意一种:ide

  • 字符串
  • 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) 这个构造函数,TimeMilliseconds 属性也会被计算。ui

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 doubleTime 的隐式转换,提升了易用性。code

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 。这是由于年和月不是固定的值,举个栗子htm

  • 一月(不是一月份)可能有 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

能够使用一个 DateIntervalTime 构成的共用体 Union<DateInterval, Time> 来指定一个 Time 的单位,它支持 TimeDateInterval 的隐式转换

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));
相关文章
相关标签/搜索