Zulip Chat Archive

Stream: general

Topic: Accessing system date/time


Jon Eugster (Feb 20 2024 at 20:36):

Is there any way within lean to print the current date/time into a string?

Henrik Böving (Feb 20 2024 at 21:19):

There does not currently exist a library that does it for you but you should be able to bind the C functions medium easily if you want this to exist.

Jon Eugster (Feb 20 2024 at 21:25):

Thx! Looks like there is a reasonable example how to bind a C-function at Foreign Function Interface, maybe I give that a try tomorrow.

Jason Rute (Feb 20 2024 at 21:36):

Also, if you don't need it to be too general, you can have Lean run a command on the command line. Lean 3 for Hackers had an example of calling the command line to generate random numbers. I think the interface is similar in Lean 4.

Damiano Testa (Feb 20 2024 at 21:57):

Is docs#IO.FS.SystemTime any use? I never tried it myself.

Eric Rodriguez (Feb 20 2024 at 22:01):

it seems not from a loogle, unless you want to make a new file and then check its created time for the current time, lol

Damiano Testa (Feb 20 2024 at 22:04):

Oh, is it only used as metadata for a file? :lol:

Eric Rodriguez (Feb 20 2024 at 22:04):

FS usually stands for file system :b

Jason Rute (Feb 20 2024 at 22:09):

Similarly, I assume docs#IO.monoMsNow and docs#IO.monoNanosNow are not useful. They are good for timing stuff, but I don't even know if the number itself has an objective interpretation (maybe just the difference between two numbers.)

Henrik Böving (Feb 20 2024 at 22:25):

You get the result of

    auto now = std::chrono::steady_clock::now();
    auto tm = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());

so whatever C++ stdlib thinks steady_clock semantics is.

Henrik Böving (Feb 20 2024 at 22:26):

cppreference remarks:

Class std::chrono::steady_clock represents a monotonic clock. The time points of this clock cannot decrease as physical time moves forward and the time between ticks of this clock is constant. This clock is not related to wall clock time (for example, it can be time since last reboot), and is most suitable for measuring intervals.

so you cannot rely on any semantics from the standard's POV

Jon Eugster (Feb 20 2024 at 23:51):

Thanks for the tips! Actually, FFI wasn't that tricky to use at all, the C++ code looks something like this:

#include <stdio.h>
#include <time.h>
#include <string>

#include <lean/lean.h>

extern "C" lean_obj_res formatLocalTime() {
    time_t rawtime;
    struct tm *timeinfo;
    char buffer[80];
    time(&rawtime);
    timeinfo = localtime(&rawtime);
    strftime(buffer, sizeof(buffer), "%c", timeinfo);
    return lean_io_result_mk_ok(lean_mk_string(buffer));
}

and the Lean code is

/-- Returns the local date/time as a string. -/
@[extern "formatLocalTime"]
opaque Time.getLocalTime : IO String

Jon Eugster (Feb 20 2024 at 23:53):

(I don't know if it needs to be in IO, but that's the only way I managed to create a function with no arguments without searching too long)

Patrick Massot (Feb 20 2024 at 23:55):

Getting the local time is certainly interacting with real world so IO is definitely required.

Thea Brick (Feb 21 2024 at 00:47):

I've been working on a library: https://github.com/T-Brick/DateTime

Thea Brick (Feb 21 2024 at 00:49):

still a work in progress but it current does some of the stuff described here

Jürgen Bergmann (Feb 21 2024 at 12:15):

Maybe my port of the haskell time library to lean could help https://github.com/bergmannjg/time


Last updated: May 02 2025 at 03:31 UTC