Converting Utc Timestamp to Iso 8601 in Ruby

Converting UTC timestamp to ISO 8601 in Ruby

I think you're trying to trick us.

The input date to your question is the 25th of October, 2010, whilst the output is the 29th of October, 2010. Well played!

Continuing on this nit-picking thread: your times are also completely different and you're missing the seconds from the output time.

Now for the true answer.

A little factoid first though: the ISO 8601 output in Ruby is similar to the "Combined date and time" output from ISO 8601's Wikipedia page.

You've got a string and so you'll need to convert it into a Time object which you can do with to_time. Then it's simply a matter of calling iso8601 on that object to get the ISO 8601 version:

"2010-10-25 23:48:46 UTC".to_time.iso8601

The to_time method is courtesy of Rails, whilst the iso8601 is courtesy of Ruby's standard library.

Millisecond timestamp = ISO date is off by 1 millisecond

While the Float inaccuracies of which you are already aware are playing a role here, it's ultimately the “fault” of the %N conversion specifier which wants to prevent you from traveling into the future. From the documentation:

  The digits under the specified length are truncated to avoid
carry up.

Other conversions (like sprintf with '%.3f') would've rounded up in this case. So your Time object's milliseconds aren't actually 123 but 122.9??? which due to %3N becomes 122. To handle (sub-)seconds precisely Time supports integers and rational numbers:

Time.at(*timestamp.divmod(1000), :millisecond, in: 'utc')
# or
Time.at(Rational(timestamp, 1000), in: 'utc')
Time.at(timestamp / 1000r, in: 'utc')

convert iso-8601 datetime to utc time rails

I think what you're asking for is a locale time in GMT+5.

Given an ISO timestamp, 1325233011

When I convert this to a locale-based date/time

Time.at(1325233011) => '2011-12-30 03:16:51 -0500'

Take a look at the ruby-docs, http://www.ruby-doc.org/core-1.9.3/Time.html for more information. Ruby has robust Time and Date classes with many helper utilities. My machine is configured for GMT-5 so it returns the local time. It's easy to change the way timezone settings are interpreted in your program, but that's for another day. Hope this helps!

Is there a full implementation for ISO-8601 date parsing for Ruby?

Yes, but unfortunately it's in Ruby 1.9.

require "date"

Date.iso8601("2010-W32-5").strftime
#=> "2010-08-13"

I don't believe there are any implementations for Ruby 1.8.7 (or at least I couldn't find any). You could either try to upgrade to Ruby 1.9, which is pretty stable as of 1.9.2. Alternatively, you could try to parse the dates yourself.

RABL: change default date and time format to iso8601

FWIW I fixed this problem in rails 4.2.8, Rabl 0.13.1 and OJ 2.18.5

Add a initializer with the following..

Oj.default_options = {:use_as_json => true}

Convert UTC timestamp to other timezone in ISO format

If you don't need to guarantee the format of the resulting timestamp, you can use toLocaleDateString plus toLocaleTimeString with a suitable language tag, e.g.

function getISOLocal(loc, date = new Date()) {
return `${date.toLocaleDateString('en-CA', {timeZone: loc})}` +
`T${date.toLocaleTimeString('en', {timeZone: loc, hour12:false})}`;
}

console.log('Current local time in:');
['Pacific/Midway',
'America/New_York',
'Europe/Berlin',
'Pacific/Kiritimati'].forEach(
loc => console.log(`${loc} : ${getISOLocal(loc)}`)
);

Subtract ISO 8601 timestamps in Oracle

Like the comment says, a lot depends on how you want the result formatted.

If you are ok with an INTERVAL, then the easiest thing to do is use TO_UTC_TIMESTAMP_TZ which actually takes an ISO 8601 formatted string as a parameter:

SELECT TO_UTC_TIMESTAMP_TZ('2021-08-24T12:59:35Z') - TO_UTC_TIMESTAMP_TZ('2021-08-24T12:59:05Z') AS RESULT
FROM DUAL;

Which returns this result:



Leave a reply



Submit