Q语言 - 时间数据

q 语言有许多不同的方式来表示和处理时间数据,例如时间和日期.

日期

kdb +中的日期在内部存储为自参考日期为01Jan2000以来的整数天数.此日期之后的日期在内部存储为正数,之前的日期作为负数引用.

默认情况下,日期以"YYYY.MM"格式写入. DD"

q)x:2015.01.22      / This is how we write 22nd Jan 2015

q)`int$x            / Number of days since 2000.01.01
5500i

q)`year$x           / Extracting year from the date
2015i

q)x.year            / Another way of extracting year
2015i

q)`mm$x             / Extracting month from the date
1i

q)x.mm              / Another way of extracting month
1i

q)`dd$x             / Extracting day from the date
22i

q)x.dd              / Another way of extracting day
22i

算术和逻辑运算可以直接在日期执行.

q)x+1        / Add one day
2015.01.23

q)x-7        / Subtract 7 days
2015.01.15

2000年1月1日落在了星期六.因此,整个历史中的任何星期六或将来除以7时,将产生0的余数,星期日给出1,星期一收益率为2.

             Day               mod 7
           Saturday              0
           Sunday                1
           Monday                2
           Tuesday               3
           Wednesday             4
           Thursday              5
           Friday                6

时间

时间内部存储为午夜中风以来的整数毫秒数.时间以HH格式写出:MM:SS.MSS

q)tt1: 03:30:00.000     / tt1 store the time 03:30 AM

q)tt1
03:30:00.000

q)`int$tt1              / Number of milliseconds in 3.5 hours
12600000i

q)`hh$tt1               / Extract the hour component from time
3i

q)tt1.hh
3i

q)`mm$tt1               / Extract the minute component from time
30i

q)tt1.mm
30i

q)`ss$tt1               / Extract the second component from time
0i

q)tt1.ss
0i

如同日期一样,可以执行算术运算直接按时.

日期时间

日期时间是日期和时间的组合,以ISO标准格式的"T"分隔.日期时间值存储从2000年1月1日午夜开始的小数日计数.

q)dt:2012.12.20T04:54:59:000      / 04:54.59 AM on 20thDec2012

q)type dt
-15h

q)dt
2012.12.20T04:54:59.000
9
q)`float$dt
4737.205

可以通过转换为浮动来获得基本的小数日计数.