nebula> RETURN DATE({year:-123, month:12, day:3});
+------------------------------------+
| date({year:-(123),month:12,day:3}) |
+------------------------------------+
| -123-12-03 |
+------------------------------------+
nebula> RETURN DATE("23333");
+---------------+
| date("23333") |
+---------------+
| 23333-01-01 |
+---------------+
TIME
TIME包含时间,但是不包含日期。Nebula Graph 检索和显示TIME的格式为hh:mm:ss.msmsmsususus。支持的范围是00:00:00.000000到23:59:59.999999。
time()支持的属性名称包括hour、minute和second。
DATETIME
DATETIME包含日期和时间。Nebula Graph 检索和显示DATETIME的格式为YYYY-MM-DDThh:mm:ss.msmsmsususus。支持的范围是-32768-01-01T00:00:00.000000到32767-12-31T23:59:59.999999。
datetime()支持的属性名称包括year、month、day、hour、minute和second。
TIMESTAMP
TIMESTAMP包含日期和时间。支持的范围是 UTC 时间的1970-01-01T00:00:01到2262-04-11T23:47:16。
TIMESTAMP还有以下特点:
以时间戳形式存储和显示。例如1615974839,表示2021-03-17T17:53:59。
查询TIMESTAMP的方式包括时间戳和timestamp()函数。
插入TIMESTAMP的方式包括时间戳、timestamp()函数和now()函数。
timestamp()函数支持传入空值获取当前时区的时间戳,还接受 string 类型的参数。
# 传入当前时间。
nebula> RETURN timestamp();
+-------------+
| timestamp() |
+-------------+
| 1625469277 |
+-------------+
# 传入指定时间。
nebula> RETURN timestamp("2022-01-05T06:18:43");
+----------------------------------+
| timestamp("2022-01-05T06:18:43") |
+----------------------------------+
| 1641363523 |
+----------------------------------+
历史版本兼容性
在 Nebula Graph 3.0.0 版本前,传入 timestamp() 函数的时间字符串可包含毫秒和微秒;从 3.0.0 版本起,传入 timestamp() 函数的时间字符串不支持包含毫秒和微秒。
DURATION
DURATION是一段连续的时间,由years、months、days、hours、minutes、seconds六个Key自由组合成的Map类型数据表示。例如duration({years: 12, months: 5, days: 14, hours: 16, minutes: 12, seconds: 70})。
DURATION还有以下特点:
不支持为DURATION类型数据创建索引。
可以用于对指定时间进行计算。
创建 Tag,名称为date1,包含DATE、TIME和DATETIME三种类型。
nebula> CREATE TAG IF NOT EXISTS date1(p1 date, p2 time, p3 datetime);
插入点,名称为test1。
nebula> INSERT VERTEX date1(p1, p2, p3) VALUES "test1":(date("2021-03-17"), time("17:53:59"), datetime("2017-03-04T22:30:40.003000[Asia/Shanghai]"));
获取test1的属性p1的月份。
nebula> CREATE TAG INDEX IF NOT EXISTS date1_index ON date1(p1);
nebula> REBUILD TAG INDEX date1_index;
nebula> MATCH (v:date1) RETURN v.date1.p1.month;
+------------------+
| v.date1.p1.month |
+------------------+
| 3 |
+------------------+
创建 Tag,名称为school,包含TIMESTAMP类型。
nebula> CREATE TAG IF NOT EXISTS school(name string , found_time timestamp);
插入点,名称为DUT,存储时间为"1988-03-01T08:00:00"。
# 时间戳形式插入,1988-03-01T08:00:00 对应的时间戳为 573177600,转换为 UTC 时间为 573206400。
nebula> INSERT VERTEX school(name, found_time) VALUES "DUT":("DUT", 573206400);
# 日期和时间格式插入。
nebula> INSERT VERTEX school(name, found_time) VALUES "DUT":("DUT", timestamp("1988-03-01T08:00:00"));
插入点,名称为dut,用now()或timestamp()函数存储时间。
# 用 now() 函数存储时间
nebula> INSERT VERTEX school(name, found_time) VALUES "dut":("dut", now());
# 用 timestamp() 函数存储时间
nebula> INSERT VERTEX school(name, found_time) VALUES "dut":("dut", timestamp());
还可以使用WITH语句设置具体日期时间或进行计算,例如:
nebula> WITH time({hour: 12, minute: 31, second: 14, millisecond:111, microsecond: 222}) AS d RETURN d;
+-----------------+
| d |
+-----------------+
| 12:31:14.111222 |
+-----------------+
nebula> WITH date({year: 1984, month: 10, day: 11}) AS x RETURN x + 1;
+------------+
| (x+1) |
+------------+
| 1984-10-12 |
+------------+
nebula> WITH date('1984-10-11') as x, duration({years: 12, days: 14, hours: 99, minutes: 12}) as d \
RETURN x + d AS sum, x - d AS diff;
+------------+------------+
| sum | diff |
+------------+------------+
| 1996-10-29 | 1972-09-23 |
+------------+------------+