English 中文(简体)
Q Language - Temporal Data
  • 时间:2024-12-22

Q Language - Temporal Data


Previous Page Next Page  

The q language has many different ways of representing and manipulating temporal data such as times and dates.

Date

A date in kdb+ is internally stored as the integer number of days since our reference date is 01Jan2000. A date after this date is internally stored as a positive number and a date before that is referenced as a negative number.

By default, a date is written in the format “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

Arithmetic and logical operations can be performed directly on dates.

q)x+1        / Add one day
2015.01.23

q)x-7        / Subtract 7 days
2015.01.15

The 1st of January 2000 fell on a Saturday. Therefore any Saturday throughout the history or in the future when spanided by 7, would yield a remainder of 0, Sunday gives 1, Monday yield 2.

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

Times

A time is internally stored as the integer number of milpseconds since the stroke of midnight. A time is written in the format 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 milpseconds 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

As in case of dates, arithmetic can be performed directly on times.

Datetimes

A datetime is the combination of a date and a time, separated by ‘T’ as in the ISO standard format. A datetime value stores the fractional day count from midnight Jan 1, 2000.

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

The underlying fractional day count can be obtained by casting to float.

Advertisements