i have hex timestamp like this '6132103D' (got it from facebook API) according to this service i get expected date Friday, September 3, 2021 12:08:29 PM.
I have seen such discussion but there is no any success
Cannot use Time.at because i operate with String.
Be thankful if you suggest some solution or gem which i could use.
ruby 2.7.1
rails 6.0.3.2
First you need to convert hex value to decimal.
Then, use Time.at() or DateTime.strptime() function to get the timestamp
2.5.3 :019 > DateTime.strptime(str.to_i(16).to_s, '%s')
=> Fri, 03 Sep 2021 12:08:29 +0000
2.5.3 :020 > Time.at(str.to_i(16))
=> 2021-09-03 17:38:29 +0530
Crossverified using https://www.epochconverter.com/hex
hex timestamp : 6132103D
Equivalent timestamp:
GMT: Friday, September 3, 2021 12:08:29 PM
Your time zone: Friday, September 3, 2021 5:38:29 PM GMT+05:30
Decimal timestamp/epoch: 1630670909
Related
Given a (UTC) DateTime object, how can I get the corresponding time in Berlin, in CET or CEST, depending on the date?
Examples for the desired "convert" function:
convert(DateTime.new(2018, 07))
=> Sun, 01 Jul 2018 02:00:00 +0200
convert(DateTime.new(2018, 12))
=> Sat, 01 Dec 2018 01:00:00 +0100
Try this
require 'tzinfo'
timezone = TZInfo::Timezone.get('Europe/Berlin')
local_time = timezone.utc_to_local(utc_time)
That even works without Rails.
With ActiveSupport::TimeWithZone I'm able to do the following and get back a TimeWithZone object in the correct timezone.
Time.current.in_time_zone('Alaska')
:> Thu, 19 Oct 2017 08:45:08 AKDT -08:00
Is there an equivalent method to in_time_zone where I can pass it the UTC Offset in seconds and get back a TimeWithZone object with the specified offset?
offset = -25200 # -25200 seconds == -08:00
Time.current.in_utc_offset(offset)
:> Thu, 19 Oct 2017 08:45:08 -08:00
Thanks in advance!
Using Rails 5.1.2 & Ruby 2.4.1
Yes it is in vanilla Ruby: #getlocal(sec). It is not going to give you an ActiveSupport::TimeWithZone but it will give you the Time which you can format or do whatever you want with it, including using ActiveSupport extensions.
2.4.1 :016 > Time.now.getlocal(3600)
=> 2017-10-19 22:56:45 +0100
2.4.1 :017 > Time.now.getlocal(-3600)
=> 2017-10-19 20:56:48 -0100
PS: -25200 is 7 hours in seconds :) -8:00 would be 28800
I am trying to convert a date/time string "06/14/2016 07:00 PM" to a Time object under the PST time zone. I want the result to be Tue, 14 Jun 2016 19:00:00 PDT -07:00.
I tried the following:
t = "06/14/2016 07:00 PM"
r = Time.strptime(t, "%m/%d/%Y %I:%M %p").in_time_zone("Pacific Time (US & Canada)")
The time comes back as Tue, 14 Jun 2016 17:00:00 PDT -07:00. Any ideas?
This code to me works properly, but there are two possible issues that are giving you the wrong result:
You are not in the pacific time zone (or at least not according to your computer): You can test this by running r = Time.strptime(t, "%m/%d/%Y %I:%M %p"), and then printing the result of r. I'm willing to wager that it outputs
Tue, 14 Jun 2016 19:00:00 CDT -05:00
You are using ruby and not rails, in_time_zone is a method only in rails. If you try to use it in ruby it will not work.
I'm trying to parse a specific hour of a specific date. When I put the date directly as an argument, it works fine, but when I create a variable and put it in the argument it returns the current date.
Why is that?
NOTE: the variable time is 9pm and I need to parse 9pm of 12 March 2016.
datetime = DateTime.new(2016,3,12,9)
=> Sat, 12 Mar 2016 09:00:00 +0000
DateTime.parse("sat 12 march 2016 9pm")
=> Sat, 12 Mar 2016 21:00:00 +0000
DateTime.parse("datetime 9pm")
=> Mon, 14 Mar 2016 21:00:00 +0000
In your third call, you use the literal string "datetime" rather than the value of your datetime variable. You can use string interpolation to use the variable's value:
DateTime.parse("#{datetime} 9pm")
In this case, the "9pm" is ignored since it doesn't make sense added to the end of an existing date but this is why the initial attempt wasn't working. Interpolation is generally a solution for using a variable's value rather than its name.
If your goal is to change the time of an existing date, use the change method:
datetime.change(hour:21)
You can also try this
date = Date.new(2016,3,12)
DateTime.parse("#{date} 9pm")
## Output
Sat, 12 Mar 2016 21:00:00 +0000
OR
datetime = DateTime.new(2016,3,12,9)
DateTime.parse((datetime + 12.hours).to_s)
## Output
Sat, 12 Mar 2016 21:00:00 +0000
OR
DateTime.parse((datetime + 12.hours).to_s).strftime("%a, %d %b %Y %I:%M %p")
## Output
Sat, 12 Mar 2016 09:00 PM
I have a problem related to timezone behaviour of a Rails 3.1.1 application. Here is, what I did on my console:
(rdb:1) Time.zone = "Amsterdam"
"Amsterdam"
(rdb:1) Time.zone.parse("Sun, 06 Nov 2011 13:05:18 +0000")
Sun, 06 Nov 2011 14:05:18 CET +01:00
(rdb:1) Time.zone = "Atlantic Time (Canada)"
"Atlantic Time (Canada)"
(rdb:1) Time.zone.parse("Sun, 06 Nov 2011 13:05:18 +0000")
Sun, 06 Nov 2011 09:05:18 AST -04:00
My object's timestamp is UTC. In my timezone Amsterdam it was 14:05 when I created it. In New York City the timezone is "Atlantic Time (Canada)". Parsing the timestamp in that zone results in 09:05. But thats wrong, it should be 08:05.
Besides that the time difference between both zones seems to be -4 -1 = -5 but is in fact -6 hours.
That behaviour completely destroy's my apps behaviour. What am I doing wrong here?
Regards
Felix.
You are not doing anything wrong. The DST changed today, Nov 6, at 2 AM. So the time is 9:05, and not 8:05. Also, New York is in Eastern time, not Atlantic time.