How to convert a Ada.Real_TIme.Time to a string?

2019-07-01 14:25发布

I would like to write a Ada.Real_Time.Time in a file,

How can I do that?

Thanks

4条回答
放荡不羁爱自由
2楼-- · 2019-07-01 14:41

Invoke the Ada.Real_Time.Split() function, which converts a Time to a Seconds_Count and a Time_Span. The Seconds_Count value is the number of seconds elapsed since the epoch, and the Time_Span value is the number of (very small) Time_Units after that last second. See D.8 Monotonic Time (29) for details.

Seconds_Count is publicly visible in the package, and the Time_Span can be converted to a Duration via To_Duration().

Note that you can invert the process and use Time_Of() to reconstruct a Time value.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-07-01 14:49

You can use Ada.Real_Time.Split to convert an Ada.Real_Time.Time into (a) the number of seconds since the epoch, type Ada.Real_Time.Seconds_Count and (b) the fractional part, type (private) Ada.Real_Time.Time_Span; and you can use Ada.Real_Time.To_Duration to convert the fractional part into a Duration.

You can then use Ada.Real_Time.Seconds_Count'Image and Duration'Image to convert to String.

But what do you want the string for? If it's to compare when things happened in a single run, fine, but there's nothing in the language definition to say when the epoch was; it could be the time when the computer was last booted, for example.

查看更多
趁早两清
4楼-- · 2019-07-01 14:54

Package Ada.Real_time doesn't provide a method for the direct format.

I'd advise you to look at Ada.Calendar.Formatting. You have a method Clock like in Ada.Real_time. Indeed, there is a method Image(parameters : Time), which returns a String.

For more details : Package: Ada.Calendar.Formatting

查看更多
时光不老,我们不散
5楼-- · 2019-07-01 14:58

If you don't need it as readable text, but just want it saved to a file, you could try using the stream output attribute ('Write)

查看更多
登录 后发表回答