简体   繁体   English

Ruby:now.strftime(“%a,%d%b%Y%X%z”)给出了错误的时间

[英]Ruby: now.strftime(“%a, %d %b %Y %X %z”) gives wrong time

Hello guys I'm using a Ruby script to send error mails ( https://github.com/u-ichi/fluent-plugin-mail/blob/master/lib/fluent/plugin/out_mail.rb ) which relies on Net::SMTP. 大家好,我使用Ruby脚本来发送依赖于Net的错误邮件( https://github.com/u-ichi/fluent-plugin-mail/blob/master/lib/fluent/plugin/out_mail.rb ) :: SMTP。 The time is obtained using "Time::now.strftime("%a, %d %b %Y %X %z")", 使用“ Time :: now.strftime(“%a,%d%b%Y%X%z”)“获得时间,

...
smtp.send_mail(<<EOS, @from, @to.split(/,/), @cc.split(/,/), @bcc.split(/,/))
   Date: #{Time::now.strftime("%a, %d %b %Y %X %z")}
   From: #{@from}
   To: #{@to}
   Cc: #{@cc}
   Bcc: #{@bcc}
   Subject: #{subject}
   Mime-Version: 1.0
   Content-Type: text/plain; charset=utf-8
   #{body}
EOS
smtp.finish

Unfortunately, I'm getting the mails with a wrong time. 不幸的是,我收到的邮件时间不正确。 I don't know how Time::now.strftime works but I guess they pick the time from the server? 我不知道Time :: now.strftime的工作原理,但是我猜他们是从服务器选择时间的? I'm on CentOS and checked the "date" of the server and found no error... 我在CentOS上,检查服务器的“日期”,没有发现错误...

Is there other way to get the time? 还有其他方法可以打发时间吗?

You should convert this time in a specific timezone. 您应该将此时间转换为特定的时区。 like: 喜欢:

time_zone = ActiveSupport::TimeZone.new(your_desire_time_zone)
converted_time = time.in_time_zone(time_zone)

or convert in UTC 或转换为UTC

converted_time = Time.now.utc

Then try to use strftime 然后尝试使用strftime

converted_time.strftime("%a, %d %b %Y %X %z")

For more details, find here . 有关更多详细信息,请单击此处 Thanks. 谢谢。

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

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