Skip to content

Compact dates

DRAFT

Functional style avoids control structures and branches in the program logic.

Sentences that refer to more than two or three dates quickly get confusing. Choices get misunderstood, calls and meetings get missed.

Representing dates tersely helps, provided you can avoid ambiguity. Here is a compact format for a date:

Tu14May2024

Single full date string

Write a lambda cds that returns a string with its date argument in this format; i.e.

q)cds 2024.05.14  / compact date string
"Tu14May2024"

Answer
cds:{[D;M;z]                             / compact date string
  nd:`y`m`d!/:`year`mm`dd$/:(.z.d;z);      / now, date
  D[z mod 7], #[-2;"0",string nd[1]`d],    / day,day#
    M[nd[1]`m],                            / month
    string nd[1]`y                         / year
  }[string`Sa`Su`Mo`Tu`We`Th`Fr;
    string``Jan`Feb`Mar`Apr`May`Jun`Jul`Aug`Sep`Oct`Nov`Dec; ]

Omit current month or year

Current year and month could be omitted.

Tu14May   / omit current year
Tu14      / omit current year and month
Write a lambda cd that returns truncated strings as above.

Answer
cd:{[D;M;z]                              / compact date string
  nd:`y`m`d!/:`year`mm`dd$/:(.z.d;z);      / now, date
  ym:maxs .[<>]flip nd`y`m;                / display year, month?
  D[z mod 7], #[-2;"0",string nd[1]`d],    / day,day#
    M[ym[1]*nd[1]`m],                      / month
    (4*ym 0)#string nd[1]`y                / year
  }[string`Sa`Su`Mo`Tu`We`Th`Fr;
    string``Jan`Feb`Mar`Apr`May`Jun`Jul`Aug`Sep`Oct`Nov`Dec; ]

Refactor

Rewrite cd and cds as projections of a function that takes an extra argument with values:

value suppress example
0 nothing Tu14May2024
1 year Tu14May
2 month and year Tu14
3 [year[+month]]
according to current date
any of above
Answer

FIXME

Date range

A range of two dates could extend the convention.

Tu14May2024-Fr03Jan2025
Tu14May-Fr02Aug
Tu14-Th16May

Write a function cdr that returns a string representing a date range.

q)cdr 2025.05.14 2026.01.03
"We14May2025-Sa03Jan2026"
q)/ assuming in 2024
q)cdr 2024.05.14 2026.01.03
"We14May-Sa03Jan2026"
q)cdr 2024.05.14 2024.08.02
"Tu14May-Fr02Aug"
q)cdr 2024.05.14 2024.05.22
"Tu14-We22May"

Answer

FIXME

Date vector

Write a function cdv that represents a vector of dates. Assume the dates are in ascending order. E.g.

q)cdv 2024.05.14+3 16 17 18 59 60 61
"Fr17May Th30 Fr31 Sa01Jun We03 Sa13Jul Su09Mar2025 Mo10 Tu11"

Answer

FIXME

Grouped date vector

Adapt cdv to identify ranges of contiguous dates.

Contiguous dates can be in different months.

q)cdv 2024.05.14+3 16 17 18 59 60 61
"Fr17 Th30May-Sa01Jun We03Jul Sa13Jul Su09-Tu11Mar2025"
Answer

FIXME