Julia 日期和时间

Julia 通过 Dates 模块提供了以下三个函数来处理日期和时间:

  • Date:表示日期,精确到日,只显示日期。
  • DateTime:表示日期和时间,精确到毫秒。
  • DateTime:表示日时间,精确到纳秒,代表一天 24 小时中的特定时刻。

使用前,我们需要先导入 Dates 模块:

import Dates

Date 和 DateTime 类型可以通过整数或 Period 类型解析。

Period 基于日期值,表示年、月、日等:

Period
Year
Quarter
Month
Week
Day
Hour
Minute
Second
Millisecond
Microsecond
Nanosecond

Date 和 DateTime 都是抽象类型 TimeType 的子类型。

下图展示了日期类型间的关系,点击图片可以放大查看:

输出日期的时间:

实例

julia> import Dates

julia> rightnow = Dates.Time(Dates.now()) # 时间
08:41:15.917

julia> theday = Dates.Date(2022,5,6) # 日期
2022-05-06

julia> today_date = Dates.today() 
2022-05-11

julia> Dates.now(Dates.UTC)
2022-05-11T00:44:20.136

# 格式化时间
julia> Dates.DateTime("20220429 120000", "yyyymmdd HHMMSS")
2022-04-29T12:00:00


julia> Dates.DateTime("19/04/2022 17:42", "dd/mm/yyyy HH:MM")
2022-04-19T17:42:00

下表给出了日期格式代码,通过它们可以格式化我们的日期:

字符日期/时间 元素
Y表示年,例如: yyyy => 1984, yy => 84
m表示年,例如: m => 7 or 07
u表示月份简写名称,例如: Jun
U表示月份完整名称,例如: January
e表示简写星期几,例如: Mon
E表示完整星期几,例如: Monday
d表示日,例如: 1 or 01
H表示小时,例如: HH => 00
M表示分钟,例如: MM => 00
S表示秒,例如: S => 00
s表示毫秒,例如: .000

实例

julia> Dates.Date("Sun, 27 Sep 2022", "e, d u y")
2022-09-27

julia> Dates.DateTime("Sun, 27 Sep 2022 10:25:10", "e, d u y H:M:S")
2022-09-27T10:25:10

通过上面的实例我们常见了一些日期时间对象,接下来我们就可以使用这些对象来获取数据(包含年、月、日、分、秒、时等):

实例

julia> theday = Dates.Date(2022,5,6) # 创建日期对象
2022-05-06

# 接下来获取 theday 中的数据
julia> Dates.year(theday)
2022

julia> Dates.month(theday)
5

# 获取当前时间的数据
julia> rightnow = Dates.now()
2022-05-11T08:51:45.342

julia> Dates.minute(rightnow)
51

julia> Dates.hour(rightnow)
8

julia> Dates.second(rightnow)
45

# 获取月份及星期几
julia> Dates.dayofweek(theday)
5

julia> Dates.dayname(theday)
"Friday"

julia> Dates.yearmonthday(theday)
(2022, 5, 6)

julia> Dates.dayofweekofmonth(theday)
1

日期运算

我们可以对日期对象进行算术运算。

比如我们计算两个日期相差几天:

实例

julia> day1 = Dates.Date(2022,1,17)
2022-01-17

julia> day2 = Dates.Date(2022,3,23)
2022-03-23

julia> day2 - day1
65 days

# 使用不同时间单位
julia> Dates.canonicalize(Dates.CompoundPeriod(day2 - day1))
9 weeks, 2 days

我们也可以对日期相加,比如计算 2 年 6个月后的日期:

实例

julia> rightnow = Dates.now()
2022-05-11T09:01:07.946

julia> rightnow + Dates.Year(20) + Dates.Month(6)
2042-11-11T09:01:07.946

# 6 天后的日期
julia> rightnow + Dates.Day(6)
2022-05-17T09:01:07.946

日期范围

Julia 可以通过可迭代的 range(区间范围)对象来创建指定区间的日期。 在下面给出的示例中,我们将创建一个生成每个月的第一天的迭代器。

实例

date_range = Dates.Date(2011,1,1):Dates.Month(1):Dates.Date(2022,1,1)
Dates.Date("2011-01-01"):Dates.Month(1):Dates.Date("2022-01-01")

在上的区间范围对象中,我们可以找出其中哪些属于工作日,这里需要为 filter() 创建一个匿名函数,它将根据给定的日期判断是否为工作日:

实例

julia> weekdaysfromrange = filter(dy -> Dates.dayname(dy) != "Saturday" && Dates.dayname(dy) != "Sunday" , date_range)
94-element Vector{Dates.Date}:
 2011-02-01
 2011-03-01
 2011-04-01
 2011-06-01
 2011-07-01
 2011-08-01
 2011-09-01
 2011-11-01
 2011-12-01
 2012-02-01
 ⋮
 2021-02-01
 2021-03-01
 2021-04-01
 2021-06-01
 2021-07-01
 2021-09-01
 2021-10-01
 2021-11-01
 2021-12-01

四舍五入日期和时间

我们常用 round()、floor() 和 ceil() 函数来对参数进行向上或向下舍入,同样这些函数也可用于对日期进行四舍五入,以便及时向前或向后调整日期。

实例

julia> Dates.now()
2022-05-11T09:17:49.824

julia> Dates.format(round(Dates.DateTime(Dates.now()), Dates.Minute(15)), Dates.RFC1123Format)
"Wed, 11 May 2022 09:15:00"

ceil() 函数将向前调整日期/时间,如下所示:

实例

julia> theday = Dates.Date(2022,5,6)
2022-05-06

# 下个月
julia> ceil(theday, Dates.Month)
2022-06-01

#明年
julia> ceil(theday, Dates.Year)
2023-01-01

#下周
julia> ceil(theday, Dates.Week)
2022-05-09

重复日期

我们可以查找一个区间范围内的重复日期,比如每个周日:

实例

# 创建区间日期
julia> date_range = Dates.Date(2011,1,1):Dates.Month(1):Dates.Date(2022,1,1)
Dates.Date("2011-01-01"):Dates.Month(1):Dates.Date("2022-01-01")
# 使用 filter() 函数过滤出每个周日
julia> filter(d -> Dates.dayname(d) == "Sunday", date_range)
20-element Vector{Dates.Date}:
 2011-05-01
 2012-01-01
 2012-04-01
 2012-07-01
 2013-09-01
 2013-12-01
 2014-06-01
 2015-02-01
 2015-03-01
 2015-11-01
 2016-05-01
 2017-01-01
 2017-10-01
 2018-04-01
 2018-07-01
 2019-09-01
 2019-12-01
 2020-03-01
 2020-11-01
 2021-08-01

Unix 时间

UNIX 时间,或称 POSIX 时间是 UNIX 或类 UNIX 系统使用的时间表示方式:从UTC 1970 年 1 月 1 日 0 时 0 分 0 秒起至现在的总秒数,不考虑闰秒。

time() 函数返回 Unix 值:

实例

julia> time()
1.652232489777e9

unix2datetime() 函数将 Unix 时间转化为日期/时间对象:

实例

julia> Dates.unix2datetime(time())
2022-05-11T01:28:23.493

当下时刻

DateTimes 以毫秒为单位,我们可以使用 Dates.value 函数获取以毫秒计的时间:

实例

julia> moment=Dates.now()
2022-05-11T09:31:31.037

julia> Dates.value(moment)
63787944691037

julia> moment.instant
Dates.UTInstant{Millisecond}(Millisecond(63787944691037))

时间和监控

Julia 为我们提供了 @elapsed 宏,它将返回表达式执行所需的时间(秒数)。

计算以下代码需要执行的时间:

实例

julia> function foo(n)
         for i in 1:n
           x = sin(rand())
         end
       end
foo (generic function with 1 method)


julia> @elapsed foo(100000000)
1.360567967

julia> @time foo(100000000)
  1.363258 seconds

更多实例

实例

julia> DateTime(2013)
2013-01-01T00:00:00

julia> DateTime(2013,7)
2013-07-01T00:00:00

julia> DateTime(2013,7,1)
2013-07-01T00:00:00

julia> DateTime(2013,7,1,12)
2013-07-01T12:00:00

julia> DateTime(2013,7,1,12,30)
2013-07-01T12:30:00

julia> DateTime(2013,7,1,12,30,59)
2013-07-01T12:30:59

julia> DateTime(2013,7,1,12,30,59,1)
2013-07-01T12:30:59.001

julia> Date(2013)
2013-01-01

julia> Date(2013,7)
2013-07-01

julia> Date(2013,7,1)
2013-07-01

julia> Date(Dates.Year(2013),Dates.Month(7),Dates.Day(1))
2013-07-01

julia> Date(Dates.Month(7),Dates.Year(2013))
2013-07-01

Captcha Code

0 笔记

分享笔记

Inline Feedbacks
View all notes