Zulip Chat Archive
Stream: general
Topic: automatically get version info
Asei Inoue (Oct 04 2025 at 03:42):
How to get current date by Lean?
Background
I'm working CLI tool in Lean. I don't want to specify version info manually.
So, I came up with the idea of making the result of running --version be (the Lean version) + (the release date). That way, there should be no need to specify the output of --version manually.
see https://github.com/Seasawher/mdgen/issues/106 for more detail
Chris Henson (Oct 04 2025 at 03:55):
Are you looking for something like what docs#Std.Time.Timestamp.now or docs#Std.Time.DateTime.now gives?
Asei Inoue (Oct 04 2025 at 04:02):
Thank you!
Asei Inoue (Oct 04 2025 at 04:09):
But how can I convert the current time, which is wrapped in the IO monad, into a String…?
Chris Henson (Oct 04 2025 at 04:17):
@loogle Std.Time.DateTime, String
loogle (Oct 04 2025 at 04:17):
:search: Std.Time.GenericFormat.format, Std.Time.DateTime.toAscTimeString, and 10 more
Asei Inoue (Oct 04 2025 at 04:18):
We need to write elaborator to get a String representing current date…?
Chris Henson (Oct 04 2025 at 04:25):
Oh, I see. From the link above, you want this to go inside the Cli DSL? I've not used that library before, sorry.
Asei Inoue (Oct 04 2025 at 04:27):
Yes!
Jon Eugster (Oct 05 2025 at 17:39):
is
let now := (← Std.Time.PlainDate.now) |>.format "uuuu-MM-dd"
what you want? That's run in the IO monad
Asei Inoue (Oct 05 2025 at 20:37):
@Jon Eugster
Thank you. I will try this:
/-- Get the release date of the project -/
def getReleaseDate : IO String := do
let date ← Std.Time.PlainDate.now
return date.format "uuuu-MM-dd"
/-- version message of `mdgen --version` -/
unsafe def versionMsg : String :=
let dateExcept := unsafeIO getReleaseDate
let date := match dateExcept with
| Except.ok date => date
| Except.error _ => "unknown date"
s!"{Lean.versionString} (released on {date})"
/-- API definition of `mdgen` command -/
unsafe def mkMdgenCmd : Cmd := `[Cli|
mdgen VIA runMdgenCmd; [versionMsg]
"mdgen is a tool to generate .md files from .lean files."
FLAGS:
c, count; "Counts the total number of characters in the input Lean files. However, please be aware that the output may not be entirely accurate."
e, exercise; "Erases parts of Lean code and replaces them with sorry."
ARGS:
input_dir : String; "The directory containing the input Lean files."
output_dir : String; "The directory to write the markdown files."
]
Eric Wieser (Oct 05 2025 at 21:19):
Usually version dates indicate when the code was changed, not when it was compiled
Eric Wieser (Oct 05 2025 at 21:20):
Especially since Lean projects are almost always shipped as source
Asei Inoue (Oct 05 2025 at 21:21):
@Eric Wieser Thanks.
How about this? This code get release date from git.
/-- Get the release date of the project -/
def getReleaseDate : IO String := do
let out ← IO.Process.output {
cmd := "git"
args := #["log", "-1", "--format=%cs"]
}
return out.stdout.trimRight
/-- version message of `mdgen --version` -/
unsafe def versionMsg : String :=
let dateExcept := unsafeIO getReleaseDate
let date := match dateExcept with
| Except.ok date => date
| Except.error _ => "unknown date"
s!"{Lean.versionString} (released on {date})"
/-- API definition of `mdgen` command -/
unsafe def mkMdgenCmd : Cmd := `[Cli|
mdgen VIA runMdgenCmd; [versionMsg]
"mdgen is a tool to generate .md files from .lean files."
FLAGS:
c, count; "Counts the total number of characters in the input Lean files. However, please be aware that the output may not be entirely accurate."
e, exercise; "Erases parts of Lean code and replaces them with sorry."
ARGS:
input_dir : String; "The directory containing the input Lean files."
output_dir : String; "The directory to write the markdown files."
]
Eric Wieser (Oct 05 2025 at 21:24):
That's a little better, but calling git every time your binary is invoked sounds like a bad idea
Asei Inoue (Oct 05 2025 at 21:25):
@Eric Wieser
so you mean "getting version info automatically" is bad idea?
Eric Wieser (Oct 05 2025 at 21:32):
Well, ideally you would only call git when you pass the--version flag, so as not to slow things down
Eric Wieser (Oct 05 2025 at 21:33):
But yes, doing this automatically seems fishy to me
Asei Inoue (Oct 05 2025 at 21:34):
ok, I should check version info automatically when developing it?
Last updated: Dec 20 2025 at 21:32 UTC