You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Ada语言中使用Ada.Calendar包获取当前年份?

在Ada中使用Ada.Calendar获取当前年份

要通过Ada.Calendar获取当前年份,你需要结合Ada.Calendar.Year_Month_Day包来拆分时间值,具体步骤和代码示例如下:

  • 引入必要的包:Ada.Calendar提供时间获取函数,Ada.Calendar.Year_Month_Day提供时间拆分的过程
  • 调用Clock函数获取当前系统时间
  • 使用Split过程将时间值分解为年、月、日三个部分,提取年份即可

完整代码示例:

with Ada.Calendar; use Ada.Calendar;
with Ada.Calendar.Year_Month_Day; use Ada.Calendar.Year_Month_Day;
with Ada.Text_IO; use Ada.Text_IO;

procedure Get_Current_Year is
   Current_Time : Time := Clock;
   Year : Year_Number;
   Month : Month_Number;
   Day : Day_Number;
begin
   Split(Current_Time, Year, Month, Day);
   Put_Line("当前年份: " & Year_Number'Image(Year));
end Get_Current_Year;

关键说明

  • Clock:返回当前系统时间的Time类型实例
  • Split:将Time类型参数拆分为对应的年份(Year_Number类型)、月份(Month_Number类型)和日期(Day_Number类型),年份的有效范围通常是1901到2399
  • Year_Number'Image:将年份数值转换为字符串,方便输出或后续处理

内容的提问来源于stack exchange,提问作者sufocator

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 14:55:23