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

痴心易碎 提交于 2019-12-23 18:14:58

问题


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

How can I do that?

Thanks


回答1:


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




回答2:


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.




回答3:


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.




回答4:


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)



来源:https://stackoverflow.com/questions/2944565/how-to-convert-a-ada-real-time-time-to-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!