Time conversion in ruby - ruby-on-rails

I have data as following
pubdate=10 hours ago +0000
pudate=Yesterday +0000
pubdate=1 min ago +0000
i want to convert in this format like this
Wed Jan 30 10:00:00 +0000 2013
please someone help me

You can use strftime on date, datetime object to convert into whatever format you want
1.9.3p327 :006 > date = Date.today
 => Wed, 30 Jan 2013 
1.9.3p327 :007 > time = Time.now
 => 2013-01-30 17:00:30 +0530 
1.9.3p327 :008 > date.strftime("%a %b %d %H:%M:%S %z %Y")
 => "Wed Jan 30 00:00:00 +0000 2013" 
1.9.3p327 :009 > time.strftime("%a %b %d %H:%M:%S %z %Y")
 => "Wed Jan 30 17:00:30 +0530 2013" 

Related

Add 30 seonds to minutes in time class in Ruby

I used the following format in Ruby which is working fine.
Time.new.strftime("%a %d %b, %Y %H:%M") # output "Thu 05 Aug, 2021 12:29"
But I need to add additional 30 seconds to the current time. I tried with active support, but unable to figure out how to do it. Any help would be appreciated.
Given an instance of time: (Rails flavored)
t = Time.current
#=> Thu, 05 Aug 2021 10:55:12 CEST +02:00
There are several ways to add 30 seconds to it, e.g.
Ruby's Time#+ with an integer:
t + 30
#=> Thu, 05 Aug 2021 10:55:42 CEST +02:00
Ruby's Time#+ with a Rails' ActiveSupport::Duration instance from Numeric#seconds:
t + 30.seconds
#=> Thu, 05 Aug 2021 10:55:42 CEST +02:00
Rails' Time#advance which takes a hash:
t.advance(seconds: 30)
#=> Thu, 05 Aug 2021 10:55:42 CEST +02:00
%S - Second of the minute (00..59)
Time.new.strftime("%a %d %b, %Y %H:%M:%S")
# "Thu 05 Aug, 2021 10:18:50"
+ 30 seconds
(Time.new + 30.seconds).strftime("%a %d %b, %Y %H:%M:%S")
Or 0.5 min (30 sec)
(Time.new + 0.5.minutes).strftime("%a %d %b, %Y %H:%M:%S")
There is a method since for time object which can be used for this purpose
Time.new.since(30.seconds).strftime("%a %d %b, %Y %H:%M:%S")

Rails/ruby transform string to UTC

I'm currently doing:
DateTime::strptime('12/11/17', "%d/%m/%Y") => Fri, 12 Nov 0017 00:00:00 +0000
But I want Mon, 11 Dec 2017 00:00:00 UTC +00:00
Thanks in advance
The following snipped should do what you are looking for (using ruby 2.3):
DateTime::strptime('12/11/17', "%m/%d/%y").strftime("%a, %d %b %Y %H:%M:%S UTC %Z")
#=> "Mon, 11 Dec 2017 00:00:00 UTC +00:00"

Rails 5: Not converting DateTime object timezone correctly

This issue has been eating away at me for a while.
I take a date time string:
[14] pry(#<EventsController>)> params[:event][:ended_at]
=> "05/31/2017 2:00 PM"
I convert it to a DateTime object:
pry(#<EventsController>)> to_datetime = DateTime.strptime(params[:event][:ended_at], "%m/%d/%Y %H:%M %p")
=> Wed, 31 May 2017 14:00:00 +0000
If I run the in_time_zone method on the to_datetime object it outputs the wrong time for pacific timezone:
[16] pry(#<EventsController>)> to_datetime.in_time_zone("Pacific Time (US & Canada)")
=> Wed, 31 May 2017 07:00:00 PDT -07:00
It should read "2:00PM" the same as how it is entered.
If I go to google and check to see if "14:00:00 +0000" is the correct entry for PDT time it verifies as correct:
http://imgur.com/a/ZJ80F
Any clues about why it's not converting correctly?
the error comes that the ended_at is been assumed by the system configuration that in this case is '+0000' you need to include the original timezone the ended_at is.
irb(main):001:0> date = "05/31/2017 2:00 PM"
=> "05/31/2017 2:00 PM"
irb(main):002:0> to_datetime = DateTime.strptime(date, "%m/%d/%Y %H:%M %p")
=> Wed, 31 May 2017 14:00:00 +0000
Note that this one has already set as UTC since was assumed by the system the timezone
irb(main):001:0> date = "05/31/2017 2:00 PM -0700"
=> "05/31/2017 2:00 PM -0700"
irb(main):002:0> to_datetime = DateTime.strptime(date, "%m/%d/%Y %H:%M %p %z")
=> Wed, 31 May 2017 14:00:00 -0700
irb(main):003:0> new_to_datetime = to_datetime.utc
=> Wed, 31 May 2017 21:00:00 +0000
irb(main):004:0> new_to_datetime.in_time_zone("Pacific Time (US & Canada)")
=> Wed, 31 May 2017 14:00:00 PDT -07:00
Update
#antonio's comment mention he was 1 hour off
irb(main):046:0> time = DateTime.strptime(date + " Pacific Time (US & Canada)", "%m/%d/%Y %H:%M %p %Z").class
=> DateTime
irb(main):047:0> time = DateTime.strptime(date + " Pacific Time (US & Canada)", "%m/%d/%Y %H:%M %p %Z")
=> Wed, 31 May 2017 14:00:00 -0800
irb(main):048:0> time.utc.class
=> Time
As you can see these are different classes and that's sign of problems what you can do is use Time instead of DateTime
irb(main):049:0> time = Time.strptime(date + " Pacific Time (US & Canada)", "%m/%d/%Y %H:%M %p %Z")
=> 2017-05-31 14:00:00 -0700
irb(main):050:0> time.class
=> Time
If we look at your first attempt:
pry(#<EventsController>)> to_datetime = DateTime.strptime(params[:event][:ended_at], "%m/%d/%Y %H:%M %p")
=> Wed, 31 May 2017 14:00:00 +0000
we see that "05/31/2017 2:00 PM" is being parsed correctly in UTC, hence the +0000 offset.
Then, if you convert that UTC time to PDT, you apply a -7 hour adjustment:
=> Wed, 31 May 2017 07:00:00 PDT -07:00
# -------------------------------^^^^^^
and the 14:00:00 becomes 07:00:00 because 14 - 7 == 7.
If you want to_datetime to be in PDT then it would be easiest to start that way by telling DateTime.strptime that the timestamp string is PDT:
no_tz = '05/31/2017 2:00 PM'
to_datetime = DateTime.strptime("#{no_tz} PDT", '%m/%d/%Y %H:%M %p %z')
# ------------------------------^^^^^^^^^^^^^^^---------------------^^
Then you'll get the 14:00:00 PDT you're looking for:
> to_datetime.iso8601
=> "2017-05-31T14:00:00-07:00"
# -------------^^------^^^^^^

Rails datetime format "dd/mm/yyyy"

I receive dates like "14/04/17 07:00"
I want to get only the date like this: 14/04/2017
And only the hour 07:00
I tried this way, BTW I know it's for US time, it render me 17/04/10
DateTime.parse(datetime).strftime("%d/%m/%Y")
Humm I can't find anywhere how to do this?
Thanks for your help
EDIT
with the suggestion:
my_date = DateTime.parse(datetime).strftime("%d/%m/%Y")
If I do in my terminal:
[1] pry(main)> O = Onduleur.last
Onduleur Load (0.2ms) SELECT "onduleurs".* FROM "onduleurs" ORDER BY "onduleurs"."id" DESC LIMIT ? [["LIMIT", 1]]
=> #<Onduleur:0x007fa6b70186e0
id: 144,
identifier: 2,
datetime: "11/04/17 23:00",
energy: 0,
created_at: Wed, 12 Apr 2017 15:17:04 UTC +00:00,
updated_at: Wed, 12 Apr 2017 15:17:04 UTC +00:00>
[2] pry(main)> my_date = DateTime.parse(O.datetime).strftime("%d/%m/%Y")
=> "17/04/2011"
[3] pry(main)>
For reading you can use strptime and specify the format:
datetime = "14/04/17 07:00"
DateTime.strptime(datetime, "%d/%m/%y %R")
=> Wed, 14 Apr 0017 07:00:00 +0000
Explanation
%d - Day of the month, zero-padded (01..31)
%m - Month of the year, zero-padded (01..12)
%y - year % 100 (00..99)
%R - 24-hour time (%H:%M)
For getting the date you can transform DateTime objects to Date objects using to_date or use .strftime("%d/%m/%Y") directly on DateTime to get String.
[47] pry(main)> a
=> Fri, 14 Apr 2017 07:00:00 +0000
[48] pry(main)> a.to_date
=> Fri, 14 Apr 2017
[49] pry(main)> a.strftime("%d/%m/%Y")
=> "14/04/2017"
[50] pry(main)> a.strftime("%R")
=> "07:00"
Full docs here. Also a full list of format directives is available on strftime docs
You can set to variables with the desire data
datetime = "2017-04-12 10:30:14"
my_date = DateTime.parse(datetime).strftime("%d/%m/%Y")
my_hour = DateTime.parse(datetime).strftime("%H:%M")
puts my_date
puts my_hour
Output:
12/04/2017
10:30
EDIT:
If you have this format date (11/04/2017 23:00), you can try with .to_time method
require 'active_support/core_ext/string'
datetime2 = "11/04/2017 23:00".to_time
my_date2 = datetime2.strftime("%d/%m/%Y")
my_hour2 = datetime2.strftime("%H:%M")
puts my_date2
puts my_hour2

Convert facebook date into timestamp format

I get birthday from facebook in this format
"12/09/1993"
But in timestamp format is yyyy/mm/dd How I convert facebook data into this with min lines of code?
Here you can see different sort of combinations:
require 'date'
date = "01/07/2016 09:17AM"
DateTime.parse(date).strftime("%A, %b %d")
#=> Friday, Jul 01
DateTime.parse(date).strftime("%m/%d/%Y")
#=> 07/01/2016
DateTime.parse(date).strftime("%m-%e-%y %H:%M")
#=> 07- 1-16 09:17
DateTime.parse(date).strftime("%b %e")
#=> Jul 1
DateTime.parse(date).strftime("%l:%M %p")
#=> 9:17 AM
DateTime.parse(date).strftime("%B %Y")
#=> July 2016
DateTime.parse(date).strftime("%b %d, %Y")
#=> Jul 01, 2016
DateTime.parse(date).strftime("%a, %e %b %Y %H:%M:%S %z")
#=> Fri, 1 Jul 2016 09:17:00 +0200
DateTime.parse(date).strftime("%Y-%m-%dT%l:%M:%S%z")
#=> 2016-07-01T 9:17:00+0200
DateTime.parse(date).strftime("%I:%M:%S %p")
#=> 09:17:00 AM
DateTime.parse(date).strftime("%H:%M:%S")
#=> 09:17:00
DateTime.parse(date).strftime("%e %b %Y %H:%M:%S%p")
#=> 1 Jul 2016 09:17:00AM
DateTime.parse(date).strftime("%d.%m.%y")
#=> 01.07.16
DateTime.parse(date).strftime("%A, %d %b %Y %l:%M %p")
#=> Friday, 01 Jul 2016 9:17 AM
Even more combinations here: http://www.foragoodstrftime.com/
Time.parse("12/09/1993").strftime("%Y/%m/%d")
#=> "1993/09/12"
Use strftime
date = "12/09/1993".to_date # => Sun, 12 Sep 1993
date.strftime("%Y/%m/%d") # => "1993/09/12"

Resources