KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ Five easy pieces

  • Five easy pieces

    Posted by SJT on March 2, 2022 at 12:00 am

    Some days it takes me a while to get started. I need to warm up my hands and brain. Hot coffee and a small problem help.

    .z.p gives me Co-ordinated Universal Time. A neat way to convert that into a string in the unambiguous ISO 8601 format? That is

    q).z.p 2022.03.02D11:50:33.883331000

    to give "2022-03-02T11:50:33.883"

     

    SJT replied 9 months ago 5 Members · 16 Replies
  • 16 Replies
  • SJT

    Member
    February 3, 2022 at 12:00 am

    Doesnt get easier than that!

    But the challenge is to code it in q.

  • murrmack

    Member
    February 3, 2022 at 12:00 am

    You can use the built in .h.iso8601

    q).h.iso8601 .z.p 
    "2022-03-02T12:27:37.506760000" 
    q)-6_.h.iso8601 .z.p 
    "2022-03-02T12:27:37.506"

     

  • SJT

    Member
    March 3, 2022 at 12:00 am

    Too easy? Too unnecessary?  Lets kick it up.

    q)ts: .z.d+.z.t 
    q).h.iso8601 ts 
    "2022-03-02T09:12:34.684000000" 
    q).dt.fmtd[;ts] each `iso`dmy`mdy 
    "2022-03-02" "2/3/2022" "3/2/2022"

    Write .dt.fmtd without control words: no if, do, while or Cond.

  • SJT

    Member
    March 3, 2022 at 12:00 am

    Elegant! But is the datetime type not deprecated?

  • SJT

    Member
    March 3, 2022 at 12:00 am

    Nice! Though I always wonder about using powerful ssr to replace just one character with another.

     

    q)?[;”-“;] . reverse 1(“.”=) string `date$.z.p “2022-03-03”

     

    (The Zen monks wanted to play.)

  • fbodon

    Member
    March 3, 2022 at 12:00 am

    I would do

    @[-6_string .z.p; 4 7 10; :; “–T”]

    or if you prefer narrow square brackets then

    @[; 4 7 10; :; “–T”] -6_string .z.p

  • davidcrossey

    Member
    March 3, 2022 at 12:00 am

    I took a similar approach

    @[;4 7;:;”-“] string `datetime$.z.p

     

  • fbodon

    Member
    March 3, 2022 at 12:00 am

    Haha. Nice! The good old datetime! I already erased it from my memory

  • davidcrossey

    Member
    March 3, 2022 at 12:00 am

    Here is my quick attempt for which I’m sure there is a much more elegant solution

    .dt.fmtd:{(`iso`dmy`mdy!({ssr[string x;”.”;”-“]};{“/” sv string `dd`mm`year$x};{“/” sv string `mm`dd`year$x}))[x] `date$y}

     

  • davidcrossey

    Member
    March 3, 2022 at 12:00 am

    Very well gentlemen, I raze you a substitute for our departed  `datetime type with `date`time

    raze (ssr[;”.”;”-“];”T”,)@’string `date`time$.z.p

    / or alternatively

    “T” sv (ssr[;”.”;”-“];::)@’string `date`time$.z.p

     

     

  • rolf

    Member
    April 3, 2022 at 12:00 am
    q)f:"T"0:2 1#"dt"$ 
    q)f .z.p "2022-03-04T11:07:52.077" 
    
    / if you want to be picky about the type 
    q)f:first"T"0:2 1#"dt"$ 
    q)f .z.p "2022-03-04T11:08:49.988"

     

  • SJT

    Member
    July 3, 2022 at 12:00 am

    Ingenious use of Prepare Text!

    Maybe a simpler version of it would be "T"sv string"dt"$.z.p.

  • rolf

    Member
    July 3, 2022 at 12:00 am

    0: gets you the dashes in the date

  • SJT

    Member
    August 3, 2022 at 12:00 am

    I speculate its because its the international standard for dates so Prepare Text uses it.

    At any rate I should note in the Reference that it does so.

  • davidcrossey

    Member
    August 3, 2022 at 12:00 am

    I would love to know how / why this period to dash conversion is happening if anyone can shed some light?

Page 1 of 2

Log in to reply.