如何在Standard ML中获取当前时间?已尝试Time.now返回函数类型
Hey there! I see you've already checked out the TIME signature and noticed that Time.now returns a function of type unit -> Time.time — that's totally expected!
In Standard ML, functions that perform side effects (like fetching the current system time) typically take a unit argument (written as ()) to signal that they're not pure, stateless functions. To actually get the current time value, you just need to call the function with this unit argument:
Time.now ();
When you run this, you'll get a value of type Time.time, which represents the current time as a timestamp (usually the number of seconds/nanoseconds since a specific epoch, like the Unix epoch). For example, you might see output like:
val it = 1718923456.789012345 : Time.time
If you want a human-readable string representation of this time, you can pair it with the Time.toString function:
Time.toString (Time.now ());
This will give you something like:
val it = "2024-06-20 14:44:16.789" : string
Different SML implementations (like SML/NJ, MLton) might have slight variations in how they format the string or handle the timestamp, but the core way to get the current time with Time.now () is consistent across all implementations that follow the TIME signature.
内容的提问来源于stack exchange,提问作者Winny




