简体   繁体   English

识别红宝石中的时间串

[英]Recognizing strings of Time in ruby

This isn't much of a problem, really, it's entirely out of curiosity. 其实这不是什么大问题,完全是出于好奇。

For a program I'm working on I write a information to files at certain times (currently once/day, may change), but it's done in such a way that the time for each recorded piece of information is important. 对于我正在开发的程序,我会在某些时间(当前每天一次,可能会更改)将信息写入文件,但是这样做的方式使得记录每个信息的时间都很重要。 As such, every time I want to take samples and append information to the file, first I append the following: 因此,每次我想取样并将信息附加到文件中时,首先我附加以下内容:

file_out.write Time.now

As an easy way of remembering exactly when each write was made. 这是一种精确记住每次写入时间的简便方法。

Now here's what I'm curious about: is there a way to check if a string received from the file ("cur_line = file_in.gets", for example) is a Time stamp? 现在,我很想知道:是否有办法检查从文件接收的字符串(例如“ cur_line = file_in.gets”)是否为时间戳?

The reason this isn't a problem is that my current method is simple regex: 这不是问题的原因是我当前的方法是简单的正则表达式:

if cur_line =~(/^(Mon|Tue|Wed|Thu|Fri|Sat|Sun) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/)

And seems to work perfectly for my needs. 似乎完全可以满足我的需求。 (Due to the nature of the project, and the formatting of what I write, I know that there will be no other lines in the file that include any text of format '{Day} {Month}' so I stop parsing there. Just day or just month would be insufficient and lead to false positives). (由于项目的性质以及所写内容的格式,我知道文件中将没有其他行包含任何格式为“ {Day} {Month}”的文本,因此我在此处停止解析。只是一天或仅仅一个月就不够用,并导致误报)。

Are there other, perhaps prettier, ways of checking for the timestamps? 还有其他(也许更漂亮)的时间戳检查方法吗? I won't necessarily use something else (especially if you say "well you can do X but it will cause performance to suffer greatly" (not that comparisons are a great concern for performance, in this project. I'd guess that the difference would probably be negligible)). 我不一定会使用其他东西(特别是如果您说“好吧,您可以做X,但会导致性能受到很大影响”)(在这个项目中,比较并不是对性能的重要考虑。我猜想差异是可能可以忽略不计))。

It's just something that struck me as interesting and something I guessed there might be cool ways of doing in Ruby. 只是让我感到很有趣,而我猜想在Ruby中可能有很酷的方法。 :) :)

You can just do Time.parse cur_line , which will return the current date if the time cannot be parsed properly. 您可以只执行Time.parse cur_line ,如果无法正确解析时间,它将返回当前日期。 Not necessarily ideal, but Time.parse is pretty powerful :) 不一定理想,但是Time.parse非常强大:)

EDIT: Interesting way to do your regexp: Before you loop over the file, declare the regular expression as so: 编辑:做您的正则表达式的有趣方式:在循环文件之前,按如下方式声明正则表达式:

expression = Regexp.new("^(#{Time::RFC2822_DAY_NAME.join("|")}) (#{Time::RFC2822_MONTH_NAME.join("|")})")

Then, when you loop over each line, do: cur_line.match(expression) . 然后,当您遍历每行时,请执行: cur_line.match(expression) This will return nil if no match. 如果不匹配,则返回nil An interesting (albeit longer) way to do this. 一种有趣的方法(尽管更长)。

You could try to run the lines through chronic or Date.parse , but I would bet that both of these methods are less efficient than using a regular expression. 您可以尝试通过chronicDate.parse运行这些行,但是我敢打赌,这两种方法都比使用正则表达式效率低。 The impact of that efficiency is completely relative to how much text you have to parse. 这种效率的影响完全取决于您必须解析多少文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM