Time units

Version: 5.x

英文原文地址: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) 这个构造函数,TimeMilliseconds 属性也会被计算。

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

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

可以使用一个 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));

NEST 中的时间单位的更多相关文章

  1. NEST 中的距离单位

    Distance units Version: 5.x 英文原文地址:Distance units 当我们需要指定距离时(地理位置查询),可以使用一个双精度的数字来表示,它的默认单位是米(meters ...

  2. FFmpeg中的时间基(time_base), AV_TIME_BASE

    AV_TIME_BASE 经常在FFmpeg的代码中看到一个奇怪的单位 AV_TIME_BASE ,比如 AVFormatContext 结构体中就有这样一个字段: duration ,它在FFmpe ...

  3. C程序中对时间的处理——time库函数详解

    包含文件:<sys/time.h> <time.h> 一.在C语言中有time_t, tm, timeval等几种类型的时间 1.time_t time_t实际上是长整数类型, ...

  4. linux在shell中获取时间

    linux在shell中获取时间 获得当天的日期 date +%Y-%m-%d 输出: 2011-07-28 将当前日期赋值给DATE变量DATE=$(date +%Y%m%d) 有时候我们需要使用今 ...

  5. uC/OS-II中的时间 (转)

    时间是一个非常重要的概念,我们和朋友出去游玩需要约定一个时间,做事情也需要花费一段时间,总之,我们的生活离不开时间.操作系统也一样,也需要一个时间来规范其任务的执行. 我们生活中,时间的最小单位是秒, ...

  6. Java中日期时间API小结

    Java中为处理日期和时间提供了大量的API,确实有把一件简单的事情搞复杂的嫌疑,各种类:Date Time Timestamp Calendar...,但是如果能够看到时间处理的本质就可以轻松hol ...

  7. 了解 Python 语言中的时间处理

    python 语言对于时间的处理继承了 C语言的传统,时间值是以秒为单位的浮点数,记录的是从1970年1月1日零点到现在的秒数,这个秒数可以转换成我们日常可阅读形式的日期和时间:我们下面首先来看一下p ...

  8. UE3中的时间

    为了管理时间,Unreal将游戏运行时间片分隔为"Ticks".一个Tick是关卡中所有Actors更新的最小时间单位.一个tick一般是10ms-100ms(CPU性能越好,游戏 ...

  9. FineReport中日期时间函数使用总结

    说明:凡函数中以日期作为参数因子的,其中日期的形式都必须是yy/mm/dd.而且必须用英文环境下双引号(" ")引用. DATE DATE(year,month,day):返回一个 ...

随机推荐

  1. 自学Python2.6-深浅拷贝

    Python 深浅拷贝 一.深浅拷贝- 数字.字符串 对于 数字 和 字符串 而言,赋值.浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址. import copy n1=123 n2=n1 # # ...

  2. SSH简单项目

    这是我学习SSH整合时的一个测试项目,代码比较简单 整个项目实现从数据库中取数据,在页面上显示.项目的结构如下: (1)数据库设计 数据库使用的是student数据库中的一个数据库表grade,表的内 ...

  3. spark-submit参数说明--on YARN

    示例: spark-submit [--option value] <application jar> [application arguments] 参数名称 含义 --master M ...

  4. DWR3.0 服务器推送及解惑

    前言:在慕课网上学习一下服务器推送给客户端技术,代码亲测过,没毛病,今天整理记录一下: 一.环境搭建 直接上图,简单粗暴,myeclipse上file->new->WebProject 二 ...

  5. 【java】缓冲字符字节输入输出流:java.io.BufferedReader、java.io.BufferedWriter、java.io.BufferedInputStream、java.io.BufferedOutputStream

    BufferedReader最重要,因为有个方法public String readLine() package System输入输出; import java.io.BufferedReader; ...

  6. 【十八】php文件下载源码

    index.php <!DOCTYPE html> <html> <head> <title></title> <meta chars ...

  7. iOS OC Swift3.0 TableView 中tableviewcell的线左边不到边界

    Swift 3.0 func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt index ...

  8. VS源码编译QuaZip(Windows下)

    最近写个Qt demo,想要使用压缩和解压多个文件的功能,并不使用额外进程.网上参考了很多资料,发现只有QuaZip比较适合我的需求.但是QuaZip只提供源码,因此需要自己来编译. QuaZip简介 ...

  9. 改造 Combo Select支持服务器端模糊搜索

    项目中使用了 combo select,为缺省的select增加模糊搜索的功能,一直运行得很好. 1    碰到的问题 但最近碰到一个大数据量的select:初始化加载的数据项有2000多个.我们采用 ...

  10. ES6 Proxy和Reflect(下)

    construct() construct方法用于拦截new命令. var handler = { construct (target, args) { return new target(...ar ...