I'm trying to output the timezone of a ruby DateTime object:
DateTime.parse('2012/05/23').strftime('%Z')
This outputs "+00:00". According to the documentation, it should return GMT.
Am I doing something wrong, or have I found a bug?
The DateTime class does not seem to support zone data as zone names. The Time class however does this correctly. So either do this:
require 'date'
require 'time'
Time.parse('...').strftime('%Z')
Or if you already have your data in DateTime format then:
Time.parse(DateTime.parse('...').to_s).strftime('%Z')
Related
Laravel carbon is returning time in UTC when printing Carbon::now() but doesn't give the correct time while using shiftTimezone and setTimezone
Below are the results:
Carbon::now(); //"2021-07-20T07:30:29.775871Z"
Carbon::now()->timezoneName; //UTC
Carbon::now()->shiftTimezone('Asia/Kolkata'); //"2021-07-20T02:00:29.452997Z"
Carbon::now()->shiftTimezone('+05:30'); //"2021-07-20T02:00:29.452997Z"
Carbon::now()->shiftTimezone('Asia/Kolkata'); //"2021-07-20T07:30:29.775871Z"
In case of shiftTimeZone, it should add 05:30 hours to the time. But what it actually does is reduces 05:30 from the UTC time.
And for setTimezone it is taking the same time as UTC.
Any idea where I'm getting it wrong?
In my application, I never use shiftTimezone or setTimezone.
I always go with something like the following:
Carbon::now()->timezone('Asia/Kolkata');
It returns me the date in Asia/Kolkata timezone, without any issue.
What you put in comments here is not the content of the instance, it's an UTC ISO-8601 string as it's converted to be rendered in JSON output for instance:
echo Carbon::now()->format('Y-m-d H:i:s.u p'); // 2021-07-20 09:36:08.596951 Z
echo Carbon::now()->shiftTimezone('Asia/Kolkata')->format('Y-m-d H:i:s.u p'); // 2021-07-20 09:36:08.596951 +05:30
echo Carbon::now()->shiftTimezone('+05:30')->format('Y-m-d H:i:s.u p'); // 2021-07-20 09:36:08.596951 +05:30
echo Carbon::now()->shiftTimezone('Asia/Kolkata')->format('Y-m-d H:i:s.u p'); // 2021-07-20 09:36:08.596951 +05:30
JSON is converted to UTC because it's a standard way to exchange datetime information, timezone should be a separated information and you only need it to force a timezone, but 99% you want the user/client/browser to use its own timezone so new Date('2021-07-20T09:36:08.596951Z') in JavaScript or equivalent in other languages will work just fine and apply the current device timezone, which is the correct way for proper localization. Very specific cases where you want to display a date in a timezone that IS NOT the user device timezone, then you should pass "Asia/Kolkata" explicitly in your JSON output.
I have a date like this:
Date.today - 7
I tried to convert it into a string:
#last_week = strftime((Date.today - 7), '%Y-%m-%d')
But I get the error "undefined method `strftime'". What am I doing wrong?
You can do it like this:
#last_week = (Date.today - 7).strftime('%Y-%m-%d')
This is what you want, but don't do it.:
module Kernel
def strftime(date, format)
date.strftime(format)
end
end
for the reason, see below comments~~~~~
You are trying to use strftime() as though it was a standalone function. In Ruby, there is no such function. The correct way to do this is to call the method Date#strftime().
Here's an example to format today's date as a string:
Date.today.strftime("%m/%d/%y")
Now that you know how to get a date and format the date to a printable string, you can address your specific code need, which is
#last_week = (Date.today - 7).strftime("%Y-%m-%d")
This will give you the date formatted string "2016-04-28" (or thereabouts, depending on when you run the code).
There is no method as strftime on Kernel (although there is such instance method on Date), but you are trying to call such method.
Addition by #Keith Bennett
You are not calling strftime with an explicit object to receive the method, so the Ruby runtime defaults to calling the method on self, which, in this context, is the top level object, an instance of Object, which inherits from BasicObject and includes the Kernel module. None of these contain a strftime method. However, the Date method does have strftime defined. So you can do what you want to do by calling strftime on the calculated Date instance.
I am building an iOS app using Rubymotion.
I get data from a Rails 3.2.8 API and I want to convert the timestamp I get (2013-01-24T23:42:59Z) to 2013-01-24 23:42:59. How can I do that with Ruby?
What is this format called (2013-01-24T23:42:59Z)?
Perhaps it is called ISO 8601. You can accept this form and turn it into a time object by doing this:
require "time"
Time.iso8601("2013-01-24T23:42:59Z")
# => 2013-01-24 23:42:59 UTC
Google Calendar API(v2)'s time-related query is required to be RFC3339-formatted. When I looked up Time class after 'require "time"', I could not see rfc3339 method.
If you are using ActiveRecord, you can use the to_datetime method to convert the time to a DateTime object.
Time.now.to_datetime.rfc3339 #=> "2014-11-06T10:40:54+11:00"
See:
http://www.ruby-doc.org/stdlib-2.4.1/libdoc/date/rdoc/Time.html.
Does this help? http://www.ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/DateTime.html#method-i-rfc3339
DateTime.parse('2001-02-03T04:05:06.123456789+07:00').rfc3339(9)
#=> "2001-02-03T04:05:06.123456789+07:00"
The way I chose to do this was Time.now.utc.strftime('%FT%TZ')
#=> "2013-08-15T06:13:28Z" which is perfect for an HTML5 type='datetime' input field.
A website mentioned that RFC3339 is most common date format in RSS feeds, so that the conversion method is implemented as #xmlschema, but not #rfc3339.
i am using datepicker to insert date on my form, which of course returns date to the controller as a string.
the issue is when I try to convert this string to date, I get an error "invalid date"
Date.parse(params[:abc][:date])
To verify if I'm doing it right, I wrote the following ruby program:
string_date = "06/18/2011"
date = Date.parse(string_date)
puts date
This works perfectly fine. But when I try the same thing in my rails controller, it gives invalid date error.
please help.
When I do this in my Rails 3.1 console:
date = Date.parse("06/18/2011")
I get an "ArgumentError: invalid date" exception. However, it works fine with an ISO-8601 date:
date = Date.parse('2011-06-18')
So perhaps you're having a locale problem. Your script could be using your standard locale setting (which is probably some US locale judging by the date format) but your server is probably using something else.
Try changing the jQuery datepicker date format to something standard and unambiguous by adding this:
dateFormat: 'yy-mm-dd'
to the datepicker's options. The datepicker's default is 'mm/dd/yy' and Ruby's Date class doesn't like seem to like it without a special locale setting.