Search code examples
rubystrftimexml-sitemap

Why is the Ruby Time Zone Designator formatting in strftime not working?


I am building a sitemap for Google, and want to use Ruby to format the datetime in their recommended format using the W3C standard with the time zone, like this,

YYYY-MM-DDThh:mmTZD

where TZD is the Time Zone Designator. However the default format being exported from the last_updated field from an SQL database by Ruby on Rails has spaces in it, eg

2014-09-19 10:33:05 UTC

How do I format the datetime in ruby to remove spaces and include the time zone in the correct format?

TZD can be accessed in ruby using the strftime method, like this:

myobject.updated_at.strftime("%Y-%m-%dT%H:%M:%S%z")

This will format time with TZD in the following format

2014-09-26T07:53:14+0000

However I need to get the TZD in format +00:00

According to the Ruby docs I should be able to use

%:z

However this shows up as

2014-09-26T08:10:21%:z

ruby 1.8.7


Solution

  • Yes, but the docs you are linking are for latest stable version (1.9.3), not 1.8.7 - which BTW - is no longer supported. And it seems that %:z option was not available in Ruby 1.8.7.

    There are also stable versions: 2.0.0, and 2.1.3 - in any of them you will get required behaviour.