Up to Main Index                              Up to Journal for July, 2023

                     JOURNAL FOR THURSDAY 6TH JULY, 2023
______________________________________________________________________________

SUBJECT: Extending time built-in information after feedback
   DATE: Thu  6 Jul 21:35:10 BST 2023

Yesterday’s post caused a bit of a buzz. After talking to some users, feedback
was positive with a lot of “can time provide more information like elapse?”.

To recap: time is a built-in that returned the number of micro-seconds since
the Unix epoch[1]. Due to only using 32-bit int on 32-bit platforms the date
range available was tiny and mostly unusable:


    1969-12-31 23:24:12.516352  to  1970-01-01 00:35:47.483647 (+0000 UTC)


To work around the problem I proposed adding a lapse built-in that returns
time information using the start of execution as the epoch:


    >cat elapse.mr
    println literal elapse
    >mere elapse.mr
    [string](
      "hour"       0,
      "minute"     0,
      "second"     0,
      "fraction"   4880,
      "stampNano"  "0:00:00.000004880",
      "stampMicro" "0:00:00.000004",
      "stampMilli" "0:00:00.000",
      "stamp"      "0:00:00",
    )


The time built-in would then be updated to return only the seconds since the
Unix epoch.

People seemed to like the elapse proposal a lot. Maybe not so much the name,
some felt ‘elapsed’ would be a better name. Now I’ve been asked if the time
built-in can return more information similar to elapse.

After tinkering for a while, this is what I came up with:


    >cat time.mr
    println literal time
    >mere time.mr
    [string](
      "year"       2023,
      "month"      7,
      "day"        6,
      "weekday"    4,
      "hour"       18,
      "minute"     40,
      "second"     6,
      "fraction"   497000000,
      "offset"     3600,
      "zone"       "UTC+1",
      "unix"       1688665206,
      "date"       "2023-07-06",
      "time"       "18:40:06",
      "clock"      "6:40PM",
      "dateTime"   "2023-07-06 18:40:06",
      "stampNano"  "2023-07-06T18:40:06.497+01:00",
      "stampMicro" "2023-07-06T18:40:06.497+01:00",
      "stampMilli" "2023-07-06T18:40:06.497+01:00",
      "stamp"      "2023-07-06T18:40:06+01:00",
    )
    >


Like elapse, time has some useful predefined formats. An example of some of
the formats:


    >cat time-format.mr
    s = time
    println s["date"]
    println s["time"]
    println s["clock"]
    println s["dateTime"]
    println s["stamp"]
    >mere time-format.mr
    2023-07-06
    18:40:06
    6:40PM
    2023-07-06 18:40:06
    2023-07-06T18:40:06+01:00
    >


The information returned by time can also be used for custom formatting. The
information does not include any textual information, such as the names of the
days or months. If these are needed they can be added using custom formatting
for any language required. A small example using English day and month names:


    >cat time-custom.mr
    months = []string("" "January" "February" "March" "April" "May" "June",
               "July" "August" "September" "October" "November" "December")
    days   = []string("Sunday"   "Monday" "Tuesday" "Wednesday",
                      "Thursday" "Friday" "Saturday")
    ampm   = []string("am" "pm")

    s = time
    printf "%s, %d %s %d %02d:%02d%s\n",
           days[s["weekday"]] s["day"] months[s["month"]] s["year"],
           s["hour"] s["minute"] ampm[s["hour"]/12]
    >mere time-custom.mr
    Thursday, 6 July 2023 18:46pm
    >


However, the time built-in is not supposed to be a complete date and time
implementation with lots of bells and whistles. Having said that, I quite like
this change to time so it’s going in :) I’ll also rename elapse to elapsed :/

--
Diddymus

  [1] Unix epoch being: 1970-01-01 00:00:00 +0000 UTC


  Up to Main Index                              Up to Journal for July, 2023