Ada中微秒数的计算

发布于 2025-01-09 15:16:56 字数 161 浏览 4 评论 0原文

在 C 语言中,我们有 get_usec(),它为我们提供自当前秒开始以来的微秒数。 -说到“当前秒”必然指的是时间参考,通常是 EpochTime。 -在 Ada.Calendar 包中,我通过示例看到了秒或时钟功能,并且能够分割和显示时间。获取秒数。 但是如何获取自当前秒开始以来的微秒数呢? 谢谢 标记

in C langage we have get_usec() which gives us the number of microseconds since the start of the current second.
-Speaking of the "current second" necessarily refers to time reference which is often EpochTime.
-In Ada.Calendar package, I see Seconds or Clocks functions by example with ability to split & get the seconds.
But how to get the number of microseconds since the start of the current second, please?
Thanks
Mark

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

初相遇 2025-01-16 15:16:56

Ada.Calendar 和 Ada.Calendar.Formatting 包提供了您需要的信息。

with Ada.Text_IO;             use Ada.Text_IO;
with Ada.Calendar;            use Ada.Calendar;
with Ada.Calendar.Formatting; use Ada.Calendar.Formatting;

procedure Main is
   Now     : Time            := Clock;
   Seconds : Second_Duration := Sub_Second (Now);
begin
   Put_Line
     ("Sub seconds since current second: " &
        Second_Duration'Image (Seconds));
end Main;

该程序一次执行的输出为:

自当前秒起的亚秒数:0.655316600

在此执行中,值指示 655316.6 微秒。

The packages Ada.Calendar and Ada.Calendar.Formatting provide the information you will need.

with Ada.Text_IO;             use Ada.Text_IO;
with Ada.Calendar;            use Ada.Calendar;
with Ada.Calendar.Formatting; use Ada.Calendar.Formatting;

procedure Main is
   Now     : Time            := Clock;
   Seconds : Second_Duration := Sub_Second (Now);
begin
   Put_Line
     ("Sub seconds since current second: " &
        Second_Duration'Image (Seconds));
end Main;

The output of one execution of this program is:

Sub seconds since current second: 0.655316600

In this execution the value indicated 655316.6 microseconds.

櫻之舞 2025-01-16 15:16:56

请注意,Ada.Calendar 适用于当地时间,并且可能会向后跳转。如果它可用(是否有任何 83 后编译器不提供它?),您最好使用 Ada.Real_Time ARM D.8

Now : constant Ada.Real_Time.Time := Ada.Real_Time.Clock;

Count : Ada.Real_Time.Seconds_Count;
Sub   : Ada.Real_Time.Time_Span;
...
Ada.Real_Time.Split (T => Now, SC => Count, TS => Sub);

现在 Count 包含自纪元以来的整秒数,而 Sub 包含除了计数之外还有一秒。 Ada.Real_Time.To_Duration 将 Time_Span 转换为 Duration,允许您将其乘以 1E6 以获得微秒。

Note that Ada.Calendar is for local time, and may jump backwards. If it's available (are there any post-83 compilers that don't provide it?), you'll be better off using Ada.Real_Time ARM D.8:

Now : constant Ada.Real_Time.Time := Ada.Real_Time.Clock;

Count : Ada.Real_Time.Seconds_Count;
Sub   : Ada.Real_Time.Time_Span;
...
Ada.Real_Time.Split (T => Now, SC => Count, TS => Sub);

Now Count contains the number of whole seconds since the epoch and Sub contains the fraction of a second in addition to Count. Ada.Real_Time.To_Duration converts a Time_Span to Duration, allowing you to multiply it by 1E6 to get microseconds.

独留℉清风醉 2025-01-16 15:16:56

当然,也可以在没有 Ada.Calendar.Formatting 的情况下完成,例如:

with Ada.Calendar; use Ada.Calendar;
...
type Seconds_In_Day is range 0 .. 86_400;
-- Or use Integer if it is 32 bits.

Now : constant Day_Duration := Seconds (Clock);
Subsec : Duration := Now - Day_Duration (Seconds_In_Day (Now));
...
if Subsec < 0.0 then
   -- Conversion of Now rounded up instead of down.
   Subsec := Subsec + 1.0;
end if;

结果以亚秒为单位。

但据我所知,使用 Ada.Calendar.Formatting.Sub_Second 更短,并且可能更好(更快或更准确);我没有比较这两种方法。

It can also be done (of course) without Ada.Calendar.Formatting, like this for example:

with Ada.Calendar; use Ada.Calendar;
...
type Seconds_In_Day is range 0 .. 86_400;
-- Or use Integer if it is 32 bits.

Now : constant Day_Duration := Seconds (Clock);
Subsec : Duration := Now - Day_Duration (Seconds_In_Day (Now));
...
if Subsec < 0.0 then
   -- Conversion of Now rounded up instead of down.
   Subsec := Subsec + 1.0;
end if;

with the result in Subsec.

But using Ada.Calendar.Formatting.Sub_Second is shorter, and may be better (faster or more accurate) for all I know; I did not compare the two methods.

染火枫林 2025-01-16 15:16:56

非常感谢您的回答。

使用你所有的例子,我做了一些尝试,如下:

with Ada.Text_IO;   use Ada.Text_IO;

with Ada.Calendar;  use Ada.Calendar;
with Ada.Real_Time; use Ada.Real_Time;

procedure Display_Current_Year is

   --need to precise the origin package Ada.Real-Time else ambiguous
   Now         : Ada.Calendar.Time := Clock;
   
   Now_Year    : Year_Number;
   Now_Month   : Month_Number;
   Now_Day     : Day_Number;
   Now_Seconds : Day_Duration;
   
   Current_Real_Time : Ada.Real_Time.Time;
   Time_Span         : Ada.Real_Time.Time_Span;
   Seconds_Count     : Ada.Real_Time.Seconds_Count;
   
   Hour : float;
      
begin

   --- Ada.Calendar
   Split (Now,
          Now_Year,
          Now_Month,
          Now_Day,
          Now_Seconds);
          
   Put_Line("Calendar : Date du jour = ");
   Put_Line ("Current year  is: "
             & Year_Number'Image (Now_Year));
   Put_Line ("Current month is: "
             & Month_Number'Image (Now_Month));
   Put_Line ("Current day   is: "
             & Day_Number'Image (Now_Day));
   Put_Line ("'Current' seconde   is: "
             & Day_Duration'Image (Now_Seconds));
   New_Line;

   --Ada.Real_Time; 
   Current_Real_Time := Ada.Real_Time.Clock;

   Ada.Real_Time.Split (T  => Current_Real_Time,
                        Sc => Seconds_Count,
                        Ts => Time_Span);

   Put_Line ("Real_Time : Seconds_Count = " & Seconds_Count'Img);
   Hour := (float(Seconds_count) / 3600.00);
   Put_Line ("Hour since seconds origin : " 
                                     & (Hour'Img));

end Display_Current_Year;
结果:
$ ./display_current_year
Calendar : Date du jour = 
Current year  is:  2022
Current month is:  2
Current day   is:  27
'Current' seconde   is:  68625.325897000

Real_Time : Seconds_Count =  30953
Hour since seconds origin :  8.59806E+00
$
-Results for calendar are OK, but why 30953 seconds !!
Where does GNAT take the Epoch, if this is, in this case, please?
Thanks
Mark

Many thaks for yours answers.

Using all yours examples, i made some trials, one is below :

with Ada.Text_IO;   use Ada.Text_IO;

with Ada.Calendar;  use Ada.Calendar;
with Ada.Real_Time; use Ada.Real_Time;

procedure Display_Current_Year is

   --need to precise the origin package Ada.Real-Time else ambiguous
   Now         : Ada.Calendar.Time := Clock;
   
   Now_Year    : Year_Number;
   Now_Month   : Month_Number;
   Now_Day     : Day_Number;
   Now_Seconds : Day_Duration;
   
   Current_Real_Time : Ada.Real_Time.Time;
   Time_Span         : Ada.Real_Time.Time_Span;
   Seconds_Count     : Ada.Real_Time.Seconds_Count;
   
   Hour : float;
      
begin

   --- Ada.Calendar
   Split (Now,
          Now_Year,
          Now_Month,
          Now_Day,
          Now_Seconds);
          
   Put_Line("Calendar : Date du jour = ");
   Put_Line ("Current year  is: "
             & Year_Number'Image (Now_Year));
   Put_Line ("Current month is: "
             & Month_Number'Image (Now_Month));
   Put_Line ("Current day   is: "
             & Day_Number'Image (Now_Day));
   Put_Line ("'Current' seconde   is: "
             & Day_Duration'Image (Now_Seconds));
   New_Line;

   --Ada.Real_Time; 
   Current_Real_Time := Ada.Real_Time.Clock;

   Ada.Real_Time.Split (T  => Current_Real_Time,
                        Sc => Seconds_Count,
                        Ts => Time_Span);

   Put_Line ("Real_Time : Seconds_Count = " & Seconds_Count'Img);
   Hour := (float(Seconds_count) / 3600.00);
   Put_Line ("Hour since seconds origin : " 
                                     & (Hour'Img));

end Display_Current_Year;
with result :
$ ./display_current_year
Calendar : Date du jour = 
Current year  is:  2022
Current month is:  2
Current day   is:  27
'Current' seconde   is:  68625.325897000

Real_Time : Seconds_Count =  30953
Hour since seconds origin :  8.59806E+00
$
-Results for calendar are OK, but why 30953 seconds !!
Where does GNAT take the Epoch, if this is, in this case, please?
Thanks
Mark
网名女生简单气质 2025-01-16 15:16:56

您可以使用一个肮脏的技巧,定义一条记录 My_raw_duration_Type :whole_part、fraction_part,均为 U32。定义 Unchecked_Conversion To_Raw (Ada.Real_Time.Duration, My_Raw_Duration_Type)。然后获取结果并将其命名为 My_Raw_Duration。您想要的毫秒结果是integer(float(My_Raw_Duration.Fraction_Part)/float(4*1032**2) * 1000.0);

You can do a dirty trick where you define a record My_raw_duration_Type : whole_part, fraction_part, both U32. Define Unchecked_Conversion To_Raw (Ada.Real_Time.Duration, My_Raw_Duration_Type). Then take the result of that and call it My_Raw_Duration. The milliseconds result you want is integer(float(My_Raw_Duration.Fraction_Part)/float(4*1032**2) * 1000.0);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文