date - 在 Julia 中,如何将 DateFormat 年份设置为 19 表示 2019?
问题描述
我的日期看起来像“17-JAN-19”、“18-FEB-20”。当我尝试使用该Dates
软件包时Date("17-JAN-19", "d-u-yy")
,我得到了合理的结果0019-01-17
。我可以这样做Date("17-JAN-19", "d-u-yy") + Year(2000)
,但这会引入新错误的可能性(我打算举闰年的例子,但尽管存在非常罕见的错误,但它通常有效Date("29-FEB-00", "d-u-yy")+Year(1900)
)。
是否有嵌入关于世纪的已知信息的日期格式?
解决方案
如https://github.com/JuliaLang/julia/issues/30002中所述,将世纪分配给日期有多种启发式方法。我建议明确并通过辅助函数处理它。
const NOCENTURYDF = DateFormat("d-u-y")
"""
parse_date(obj::AbstractString,
breakpoint::Integer = year(now()) - 2000,
century::Integer = 20)
Parses date in according to DateFormat("d-u-y") after attaching century information.
If the year portion is greater that the current year,
it assumes it corresponds to the previous century.
"""
function parse_date(obj::AbstractString,
breakpoint::Integer = year(now()) - 2000,
century::Integer = 20)
# breakpoint = year(now()) - 2000
# century = year(now()) ÷ 100
@assert 0 ≤ breakpoint ≤ 99
yy = rpad(parse(Int, match(r"\d{2}$", obj).match), 2, '0')
Date(string(obj[1:7],
century - (parse(Int, yy) > breakpoint),
yy),
NOCENTURYDF)
end
parse_date("17-JAN-19")
parse_date("29-FEB-00")
推荐阅读
- docker - 在 Ubuntu Docker 容器内创建 VPN 适配器失败,LCOW 除外?
- typescript - 对象中键的索引签名
- racket - 将两个递归合并为一个尾递归代码
- ruby-on-rails - 有人可以解释一下 Hash#dig 与 Hash#fetch 有什么区别吗
- node.js - webpack 不再缩小捆绑包
- php - 如果 4 个 mysql 查询中的任何一个失败,如何设置 mysqli 错误
- java - 线程“主”java.lang.NoSuchFieldError 中的异常:JAVA_VENDOR
- google-apps-script - 获取列谷歌表格的最后一个非空白行
- visual-studio-code - 在 Visual Studio 代码的实时服务器扩展中设置 liveServer.settings.port 的值的位置
- ios - 使用 xcode10 时在 iOS9 上崩溃