如何自动检测和解析朱莉娅的日期格式?
我正在尝试构建一种算法来执行日期时间格式的自动检测并相应地解析。我想寻求一些有关改进和增强算法的建议。
在以下代码中,我尝试了一种简单的方法来构建所有可能的日期格式,然后在它们上迭代将字符串与dateFormat匹配,一旦匹配,它将分析日期。
代码:
using Dates
function createDateFormats()
sep = [",",".","-","/",":"]
dateFormatComb = []
for i in sep
vals = [string("dd",i,"mm",i,"yyy"),string("mm",i,"dd",i,"yyy"),
string("yyy",i,"mm",i,"dd"),string("yyy",i,"dd",i,"mm"),
string("mm",i,"yyy",i,"dd"),string("dd",i,"yyy",i,"mm")
]
push!(dateFormatComb, vals)
end
return vcat(dateFormatComb...)
end
function parse(x)
dateFormat = createDateFormats()
try
for i in 1:size(dateFormat,1)
try
val = Date.(x, dateFormat[i])
yearCol = Dates.year.(val)
monthCol = Dates.month(val)
dayCol = Dates.day.(val)
dayofweekCol = Dates.dayofweek.(dayCol)
return yearCol, monthCol, dayCol, dayofweekCol
catch
continue
end
end
catch
throw(ArgumentError("Invalid date object"))
end
end
但是,这是非常有限的,也不有效。同样,一旦涉及时间,复杂性就会增加。我可以问一下,如果有人有更好的方法来执行此类操作? 谢谢,感谢所有建议和建议。
I am trying to build an algorithm to perform auto detection of date time formats and parse them accordingly. And I would like to seek some advice on improving and enhancing my algorithm.
In the following code, I tried a simple approach to build all the possible date formats and then iterate over them to match the string to dateformat, once matched it will parse the date.
Code:
using Dates
function createDateFormats()
sep = [",",".","-","/",":"]
dateFormatComb = []
for i in sep
vals = [string("dd",i,"mm",i,"yyy"),string("mm",i,"dd",i,"yyy"),
string("yyy",i,"mm",i,"dd"),string("yyy",i,"dd",i,"mm"),
string("mm",i,"yyy",i,"dd"),string("dd",i,"yyy",i,"mm")
]
push!(dateFormatComb, vals)
end
return vcat(dateFormatComb...)
end
function parse(x)
dateFormat = createDateFormats()
try
for i in 1:size(dateFormat,1)
try
val = Date.(x, dateFormat[i])
yearCol = Dates.year.(val)
monthCol = Dates.month(val)
dayCol = Dates.day.(val)
dayofweekCol = Dates.dayofweek.(dayCol)
return yearCol, monthCol, dayCol, dayofweekCol
catch
continue
end
end
catch
throw(ArgumentError("Invalid date object"))
end
end
However, this is quite limited and not efficient. Also, once the time is involved the complexity increases furthermore. May I ask, if someone has a better approach to perform such operations?
Thanks, would appreciate all the suggestions and advice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一种做到这一点的方法,还概述了歧义:
现在,如果这一年是2位数字,会发生什么?如果订单是MM,DD,YY对DD,MM,YY,会发生什么?您如何判断10-11-2021是10月11日还是11月10日?在这些情况下,您必须知道使用了哪些惯例或会发生错误。
Here is a way to do it that also outlines the ambiguity:
Now, what happens if the year is 2 digits? What happens if the order is mm, dd, yy versus dd, mm, yy? How do you tell whether 10-11-2021 is October 11 or November 10? In those cases you have to know what convention was used or errors will occur.