DATE::DATETIME 1g 2023 laplante@plcb.ca GOWEB/DATE


title: DATE::DATETIME section: 1g version: 1.0.0 date: 2023 author: laplante@plcb.ca

category: GOWEB/DATE

name

date::datetime Parse a date, perform an operation, and output the result.

SYNOPSIS

date::datetime or datetime([date: "date_string", op: "operation_string", format: "format_string",
    location: "location_string", layout: "layout_string", locale: "locale_string", lg: "lg_string"])

DESCRIPTION

The datetime function parses a given date, optionally applies an operation (such as adding days or months), and outputs the date in a specified format.

PARAMETERS

date

The input date can be in multiple formats. The function attempts to detect the layout automatically unless explicitly set with the layout parameter.

Order of parsing attempts:

  1. If the date contains only digits, it is parsed as seconds since the epoch.
  2. Layout 2006-01-02 15:04:05
  3. Layout RFC1123Z — e.g., Thu, 19 Apr 2018 11:00:49 +0000
  4. Layout 2006-01-02

layout

Specifies the format of the input date. Follows Go’s time package layouts (see time package constants).

Additional Layout Behavior

When lg is set and format contains %!e(MISSING)lg:

In format, %!b(MISSING) outputs the abbreviated month name according to the current locale. The locale parameter affects this result.

locale

Sets the locale for output formatting (does not affect input parsing — use location for that).

List available locales:

locale -a

(The full list can be very long; run the command above on your system to see all installed locales.)

location

Specifies the timezone used to interpret the input date.

List available locations (Linux):

awk '/^Z/ { print $2 }; /^L/ { print $3 }' /usr/share/zoneinfo/tzdata.zi

lg

Shorthand to select a locale via a language key defined in the configuration file, for example:

[lg]
    fr = "fr_CA.utf8"
    en = "en_CA.utf8"

operation

Defines a modification to apply to the input date.

[+|-]<number><unit>

Where unit is one of:

Multiple operations can be chained (e.g., 1M5m7s).

format

Output formatting uses Linux strftime. Default: %!F(MISSING) %!T(MISSING).

Common directives include:

EXAMPLES

datetime(date: "2000-01-31", op: "1M")
→ 2000-02-29 00:00:00

datetime(location: "Europe/Paris", date: "2000-01-31 03:00:00", op: "1M", format: "%!F(MISSING) %!T(MISSING)")
→ 2000-02-29 03:00:00

datetime(date: "2000-01-31 03:04:05", op: "1M5m7s", format: "%!F(MISSING) %!T(MISSING)")
→ 2000-02-29 03:09:12

datetime(date: "2000-01-31 03:04:05", op: "-1M-5m-7s", format: "%!F(MISSING) %!T(MISSING)")
→ 1999-12-31 02:58:58

a := "2000-01-31 03:04:05";
a; "  ";
a = datetime(date:a, op:"+15M", format: "%!F(MISSING) %!T(MISSING)");
a; "  ";
datetime(date:a, op:"-15M", format: "%!F(MISSING) %!T(MISSING)")
→ 2000-01-31 03:04:05  2001-04-30 03:04:05  2000-01-30 03:04:05

datetime(date: "2021-10-28 00:00:00", op: "-10m")
→ 2021-10-27 23:50:00

datetime(date: "2000-02-29", op: "-1y")
→ 2001-02-28 00:00:00

datetime(date: "2000-02-29", op: "+1y")
→ 2001-02-28 00:00:00

datetime(date: "2023-05-01", lg: "fr", format: "%!B(MISSING)")
→ mai

datetime(date: "2023-05-01", lg: "xr", format: "%!B(MISSING)")
→ *invalid lg 'xr'*

AUTHOR

laplante@plcb.ca

SEE ALSO

VERSION