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. Django项目创建02

    Django项目创建(ubuntu环境) 1.    创建项目目录,我是在root下创建了一个workspace文件夹:mkdir workspace  然后cd到该目录下 命令:django-adm ...

  2. [.Net跨平台]部署DTCMS到Jexus遇到的问题及解决思路---部署

    上一篇我们环境已经准备完成,此时可以部署了,我们就以dtcms作为例子,http://bbs.dtcms.net/forum.php?mod=viewthread&tid=2420&e ...

  3. 实现我博客旁边的线条效果 html canvas-nest.js 源码

    canvas-nest.js 这个js文件可以用来实现炫酷的线条与鼠标进行交互的功能,具体效果如图所示 js具体源码如下: /** * Copyright (c) 2016 hustcc * Lice ...

  4. linhaifeng

    http://www.cnblogs.com/linhaifeng/p/7278389.html http://blog.51cto.com/egon09

  5. iOS 轮播中遇到的问题(暂停、重新启动)

    一. 轮播的优化或者用Collection来实现 二.Timer  问题 我们可以这样来使用一个Timer [NSTimer scheduledTimerWithTimeInterval:1.0 ta ...

  6. Python2/3的中、英文字符编码与解码输出: UnicodeDecodeError: 'ascii' codec can't decode/encode

    摘要:Python中文虐我千百遍,我待Python如初恋.本文主要介绍在Python2/3交互模式下,通过对中文.英文的处理输出,理解Python的字符编码与解码问题(以点破面). 前言:字符串的编码 ...

  7. button的用法

    C# 如何去掉button按钮的边框线? 设置FlatStyle为Flat,并且设置FlatAppearance下的BorderSize为0.

  8. Qt--自定义Delegate

    这是Model/View中的最后一篇了,Qt官方显然弱化了Controller在MVC中的作用,提供了一个简化版的Delegate:甚至在Model/View框架的使用中,提供了默认的委托,让这个控制 ...

  9. bzoj 3996: [TJOI2015]线性代数

    Description 给出一个N*N的矩阵B和一个1*N的矩阵C.求出一个1*N的01矩阵A.使得 D=(A*B-C)*A^T最大.其中A^T为A的转置.输出D Input 第一行输入一个整数N,接 ...

  10. WPF后台写ControlTemplate总结

    这段时间写ControlTemplate的时候发现绑定的时候有些问题需要总结: 实例ControlTemplate如下: <UserControl x:Class="ArcGISWpf ...