Can someone explain why this IF statement is returning false if the current UTC time is 16:21 and the function is to return true if the current UTC time is >=09:00:00 && <=17:00:00
Have tried separate IF statements and extending the time bracket beyond reasonable doubt
Most likely because you're comparing a UTC time object to a string representation of the time. utcTime == string will not evaluate to true since a UTC time object and a string cannot be the same, and the UTC time converted to a string would not take the same format (as you can see when you printed the time.
The solution would be to instead create two UTC time objects with their values set to 09:00:00 and 17:00:00 respectively and comparing the current time to those instead.
= utcNow('HH:mm:ss') >= `09:00:00` && utcNow('HH:mm:ss') < `17:00:00`
Solution to question. Needed to enter time format similar to that of UTC format.
Related
I want to be able to parse a date string which might be in PST or PDT
according to daylight savings. This means that half a year the date will
be PST and the other half the date will be PDT.
This is my code now:
DateTime.parse('2016-02-21 10:00:02 PST/PDT')
This will only parse the date as PST (GMT-8).
How can I parse a date+time in PST/PDT automatically?
Thanks!
Just a note:
DateTime.parse('2016-02-21 10:00:02 PST/PDT') will always parse to PST (Standard Time), just like DateTime.parse('2016-02-21 10:00:02 PDT') will always parse to PDT (Daylight time). This is because DateTime & Time libraries are expecting that the timezone is explicit, rather than 'PST/PDT' which is saying 'I could be x or y'. If it was smarter it could work out that both of these where in the same zone and that they were daylight savings equivalents, but sadly not at this moment.
A few options:
1) Use a timezone gem to translate the time into local zones
2) Manually put in PST or PDT depending on year
I tend to store all date/times in UTC and translate as I need them.
I use the TZInfo gem to display/calculate based on local time. It uses timezones from here
recorded_time = Time.now.getutc
tz = TZInfo::Timezone.get('US/Pacific')
local_created_at = tz.utc_to_local(recorded_time)
Now this will not solve your issue if you have data already stored in DateTime already or if your inbound data is already marked up this way. If it is, I would suggest you parse it based on date. You can create a look up table using this data: http://www.timeanddate.com/time/zone/usa/los-angeles
EDIT: Just realised the TZinfo is able to solve this for you:
tz = TZInfo::Timezone.get('US/Pacific')
=> #<TZInfo::DataTimezone: US/Pacific>
tz.local_to_utc(Time.parse('2016-07-21 10:00:02 PST/PDT'))
=> 2016-07-21 17:00:02 UTC
tz.local_to_utc(Time.parse('2016-02-21 10:00:02 PST/PDT'))
=> 2016-02-21 18:00:02 UTC
I need to get time and date from database date like "2015-08-27T12:09:36Z". I tried but not get any solutions where I get date and time in different variable.
I need to get it in Ruby. No rails in my application.
I used below code but not getting.
Time.strptime("2015-08-27T12:09:36Z", "%Y-%m-%dT%H:%M:%S%z").in_time_zone
Any one have a experience in it?
Thanks
I don't have enough reputations to comment so am posting comment as answer, are you looking for this
Time.now.strftime('%Y-%m-%dT%H:%M:%SZ')
Which will give the pattern you asked for. Z represent the time zone if you use
%z - Time zone as hour and minute offset from UTC (e.g. +0900)
%:z - hour and minute offset from UTC with a colon (e.g. +09:00)
%::z - hour, minute and second offset from UTC (e.g. +09:00:00)
%Z - Time zone abbreviation name
Check for more
http://apidock.com/ruby/Time/strftime
My Updated answer after your comment
require 'date'
DateTime.parse("2015-08-27T12:09:36Z").strftime('%Y-%m-%dT%H:%M:%SZ')
In your code change Time.strptime('') to DateTime.strptime('')
First, you need to require 'date'. Ruby has built-in Date and Time classes without that require, but the library provides more functionality.
If you have a string retrieved from the database in ISO-8601 format, and you want to turn it into a date and time, just use DateTime.iso8601(string) to get a DateTime object. You can extract the date and time components however you like after that.
irb(main):001:0> require 'date' #=> true
irb(main):002:0> dt = DateTime.iso8601("2015-08-27T12:09:36Z") # DateTime object
irb(main):003:0> d = dt.to_date # Date object
irb(main):004:0> t = dt.to_time # Time object
Calling Date.today in Ruby returns the current date. However, what timezone is it in? I assume UTC, but I want to make sure. The documentation doesn't state either way.
You can get away by using
Time.now.utc.to_date
in ruby
TL;DR: Date.today uses the system’s local time zone. If you require it be in UTC, instead get the date from a UTC time, e.g. Time.now.utc.to_date.
Dates do not have timezones, since they don't represent a time.
That said, as for how it calculates the current day, let's look at this extract from the code for Date.today:
time_t t;
struct tm tm;
// ...
if (time(&t) == -1)
rb_sys_fail("time");
if (!localtime_r(&t, &tm))
rb_sys_fail("localtime");
It then proceeds to use use tm to create the Date object. Since tm contains the system's local time using localtime(), Date.today therefore uses the system's local time, not UTC.
You can always use Time#utc on any Time convert it in-place to UTC, or Time#getutc to return a new equivalent Time object in UTC. You could then call Time#to_date on that to get a Date. So: some_time.getutc.to_date.
If you’re using ActiveSupport’s time zone support (included with Rails), note that it is completely separate from Ruby’s time constructors and does not affect them (i.e. it does not change how Time.now or Date.today work). See also ActiveSupport extensions to Time.
An instance of Date is represented as an Astronomical Julian Day Number; it has no fractional part of a day. While a Julian Day is relative to GMT - so technically an instance of Date should be considered to be in GMT - for most purposes you can ignore that and treat it as having no timezone. You would only care about a time zone if you converted it to a DateTime or Time.
ActiveSupport's tools for date conversion let you specify whether you want local time or UTC.
E.g.:
>> t = Date.today.to_time
=> Wed Apr 18 00:00:00 -0400 2012
>> t.zone
=> "EDT"
>> t = Date.today.to_time(:utc)
=> Wed Apr 18 00:00:00 UTC 2012
>> t.zone
=> "UTC"
You can use
Date.current
To get the current date on the configured timezone.
If your rails applications is configured to run in time zone UTC then just use Time.zone.today, otherwise force it by using Time.now.utc.to_date
Ruby
# Using local timezone
Date.today
Ruby on Rails
# Using UTC
Date.yesterday
Date.current
Date.tomorrow
This is specifically nasty because when using e.g. Date.today and Date.yesterday in conjunction, it might produce quite unexpected results depending on the timezone and the current time.
For example, at 00:30 in timezone CET (UTC+1) Date.yesterday will be two days before Date.today. In other cases, Date.today and Date.yesterday can have the same value.
I am learning programming and I choose Ruby as the first language to learn.
I am parsing an XML where dates are in this form: 1240915075 1224855068
How is this format called? How to use that value in a Date or Time object?
Thank you!
This is UNIX time (sometimes called Epoch time). It measures the number of seconds elapsed since January 1, 1970 (The Unix epoch is the time 00:00:00 UTC on 1 January 1970)
Here's an example converter: http://www.esqsoft.com/javascript_examples/date-to-epoch.htm
A stackoverflow question regarding converting integer time using Ruby: Ruby / Rails: convert int to time OR get time from integer?
use the Time.at function to convert e.g.:
t = Time.at(i)
That's Epoch Time (the first one corresponds to Tue Apr 28 2009 11:37:55 GMT+0100).
You can get a datetime out of it, using Time.at, like this:
Time.at(1240915075)
That is a unix timestamp - the number of seconds since jan 1st 1970.
An example of how to use it in Ruby is here:
t = Time.at(1215163257)
puts t.to_date
>> 2008-07-04
How to get UTC timestamp in Ruby?
You could use: Time.now.to_i.
time = Time.now.getutc
Rationale: In my eyes a timestamp is exactly that: A point in time. This can be accurately represented with an object. If you need anything else, a scalar value, e.g. seconds since the Unix epoch, 100-ns intervals since 1601 or maybe a string for display purposes or storing the timestamp in a database, you can readily get that from the object. But that depends very much on your intended use.
Saying that »a true timestamp is the number of seconds since the Unix epoch« is a little missing the point, as that is one way of representing a point in time, but it also needs additional information to even know that you're dealing with a time and not a number. A Time object solves this problem nicely by representing a point in time and also being explicit about what it is.
The default formatting is not very useful, in my opinion. I prefer ISO8601 as it's sortable, relatively compact and widely recognized:
>> require 'time'
=> true
>> Time.now.utc.iso8601
=> "2011-07-28T23:14:04Z"
if you need a human-readable timestamp (like rails migration has) ex. "20190527141340"
Time.now.utc.to_formatted_s(:number) # using Rails
Time.now.utc.strftime("%Y%m%d%H%M%S") # using Ruby
Usually timestamp has no timezone.
% irb
> Time.now.to_i == Time.now.getutc.to_i
=> true
What good is a timestamp with its granularity given in seconds? I find it much more practical working with Time.now.to_f. Heck, you may even throw a to_s.sub('.','') to get rid of the decimal point, or perform a typecast like this: Integer(1e6*Time.now.to_f).
Time.utc(2010, 05, 17)
time = Time.zone.now()
It will work as
irb> Time.zone.now
=> 2017-12-02 12:06:41 UTC
The proper way is to do a Time.now.getutc.to_i to get the proper timestamp amount as simply displaying the integer need not always be same as the utc timestamp due to time zone differences.