I am working on scheduler module, for which I want to know if any X time zone is in midnight currently. Is there any library/gem for this purpose? If not, what would be a good logic to find out?
Your phrasing is a little ambiguous ("any X time zone"), so I'll address another possible interpretation:
If you'd like to check whether "it's midnight somewhere", with "midnight" understood broadly to mean any time from 00:00 to 00:59, a simple one-liner is
ActiveSupport::TimeZone.all.any?{ |time| time.now.hour.zero? }
To check that it's precisely 00:00 (or 00:00:00, although I can't imagine why you would want to do that...), add the appropriate conditions to the boolean block:
ActiveSupport::TimeZone.all.any?{ |time| time.now.hour.zero? && time.now.min.zero? }
You can probably leave off the ActiveSupport:: when calling TimeZone.all from within your Rails app.
[source]
You could do this:
> Time.zone
=> (GMT-05:00) Eastern Time (US & Canada)
> given_time_zone = "Central Time (US & Canada)"
> Time.zone.now.in_time_zone(given_time_zone)
=> Sun, 23 Mar 2014 23:55:57 CDT -05:00
Then you could check if the hour is zero to check if it's within 12 midnight till 1AM:
> Time.zone.now.in_time_zone(given_time_zone).hour == 0
=> false
Related
I am unable to understand the expiry date format of the JWT embedded in my application.
For example: 1473912000
What does this translate to? 1473912000 ms, some x date? Any help will be appreciated!
Like James has pointed out:
The number is the number of seconds since Jan 1 1970.
This is converted into the Date object in a quite straight-forward way (the *1000 part is here because in JS main time unit is millisecond):
const expiryDate = new Date(1473912000*1000);
Then you can use any Date method you please.
Likewise, in Ruby you can use Time.at(1473912000) to create a new Time instance like Maxim has shown.
The number is the number of seconds since Jan 1 1970. It is commonly used on unix systems to represent time. Your time is 2016-09-15 04:00 (UTC)
To convert you can try a web based system http://www.unixtimestamp.com/index.php
This is UNIX time in seconds:
➜ ~ irb
2.2.0 :001 > Time.at(1473912000)
=> 2016-09-15 07:00:00 +0300
I want to find the total UTC offset from a Timezone Object. Below are two examples using TZInfo::Timezone and ActiveSupport::TimeZone. Ultimately, I want to use the ActiveSupport::TimeZone implementation, but can't get it to give me the right answer.
#TZInfo implementation
tz = TZInfo::Timezone.get('America/New_York')
tz.current_period.utc_total_offset / 60 / 60
=> -4 (CORRECT)
# Rails implementation
tz = ActiveSupport::TimeZone.new("Eastern Time (US & Canada)")
tz.utc_offset / 60 / 60
=> -5 (WRONG)
Why does ActiveSupport::TimeZone appear to fail to factor in dst? How do I fix that?
I found this in the ActiveSupport documentation. Basically, it is running tzinfo.current_period.utc_offset instead of tzinfo.current_period.utc_total_offset
def utc_offset
if #utc_offset
#utc_offset
else
tzinfo.current_period.utc_offset if tzinfo && tzinfo.current_period
end
end
So to fully answer my question: I need the following code...
tz = ActiveSupport::TimeZone.new("Eastern Time (US & Canada)")
tz.tzinfo.current_period.utc_total_offset / 60 / 60
Two computer install centos 6.5, kernel is 3.10.44, have different result.
one result is [u'Asia/Shanghai', u'Asia/Urumqi'], and the other is ['Asia/Shanghai', 'Asia/Harbin', 'Asia/Chongqing', 'Asia/Urumqi', 'Asia/Kashgar'].
Is there any config that make the first result same as the second result?
I have following python code:
def get_date():
date = datetime.utcnow()
from_zone = pytz.timezone("UTC")
to_zone = pytz.timezone("Asia/Urumqi")
date = from_zone.localize(date)
date = date.astimezone(to_zone)
return date
def get_curr_time_stamp():
date = get_date()
stamp = time.mktime(date.timetuple())
return stamp
cur_time = get_curr_time_stamp()
print "1", time.strftime("%Y %m %d %H:%M:%S", time.localtime(time.time()))
print "2", time.strftime("%Y %m %d %H:%M:%S", time.localtime(cur_time))
When use this code to get time, the result of one computer(have 2 results) is:
1 2016 04 20 08:53:18
2 2016 04 20 06:53:18
and the other(have 5 results) is:
1 2016 04 20 08:53:18
2 2016 04 20 08:53:18
I don't know why?
You probably just have an outdated version of pytz on the system returning five time zones (or perhaps on both systems). You can find the latest releases here. It's important to stay on top of time zone updates, as the various governments of the world change their time zones often.
Like most systems, pytz gets its data from the tz database. The five time zones for China were reduced to two in version 2014f (corresponding to pytz 2014.6). From the release notes:
China's five zones have been simplified to two, since the post-1970
differences in the other three seem to have been imaginary. The
zones Asia/Harbin, Asia/Chongqing, and Asia/Kashgar have been
removed; backwards-compatibility links still work, albeit with
different behaviors for time stamps before May 1980. Asia/Urumqi's
1980 transition to UTC+8 has been removed, so that it is now at
UTC+6 and not UTC+8. (Thanks to Luther Ma and to Alois Treindl;
Treindl sent helpful translations of two papers by Guo Qingsheng.)
Also, you may wish to read Wikipedia's Time in China article, which explains that the Asia/Urumqui entry is for "Ürümqi Time", which is used unofficially in some parts of the Xinjiang region. This zone is not recognized by the Chinese government, and is considered a politically charged issue. As such, many systems choose to omit the Urumqi time zone, despite it being in listed in the tz database.
In Ruby we can find the current time in a particular Time Zone and determine if it is currently DST in that timezone.
pry(main)> t = Time.now.in_time_zone('America/Los_Angeles')
=> Tue, 02 Sep 2014 18:14:25 PDT -07:00
pry(main)> t.dst?
=> true
I'm looking to find the time difference between t and when t.dst? next changes.
Is there a way to figure out when the next DST change occurs?
# pseudo code
dst_time = # Time when next DST occurs
local_time = Time.now.in_time_zone('America/Los_Angeles')
time_till_dst_change = dst_time - local_time
You can get this info from the tzinfo gem. It can either parse the system timezone files or use the data from the tzinfo-data gem.
#!/usr/bin/env ruby
require 'tzinfo'
tz = TZInfo::Timezone.get('America/New_York')
puts "Next timezone change is at #{tz.current_period.end_transition.datetime}"
This prints:
Next timezone change is at 2014-11-02T06:00:00+00:00
I have a long value in Rails, 134740800, which is the number of milliseconds since the epoch.
How do I convert that to a date in mm-dd-yyyy format?
I figure the formatting would be done with something like strftime but I can't seem to find the right method to convert the long into a valid date.
secs = 134740800/1000 # millisecs / 1000
t = Time.at(secs)
t.strftime("%m-%d-%Y")
Output
"01-02-1970"
Try this:
require 'date'
DateTime.strptime("1318996912",'%s')
I assume you mean seconds since the epoch.
Time.at seconds_since_epoch
You can also pass a float. If you have milliseconds, divide by 1000.0 first.
You can then call strftime on the returned Time object.
Use Time.at:
irb(main):003:0> Time.at(134740800)
=> Tue Apr 09 08:00:00 -0400 1974
This is an advisory... It's often a good idea to look at how fast some answers run. Here's a simple benchmark:
require 'benchmark'
require 'date'
SECS = 134740800
LOOPS = 1_000_000
puts Time.at(SECS).strftime('%m-%d-%Y')
puts Date.strptime(SECS.to_s, '%s').strftime('%m-%d-%Y')
Benchmark.bm(14) do |x|
x.report('Time.at:') { LOOPS.times { Time.at(SECS) }}
x.report('Date.strptime:') { LOOPS.times { Date.strptime(SECS.to_s, '%s') }}
end
And the output is:
04-09-1974
04-09-1974
user system total real
Time.at: 0.370000 0.020000 0.390000 ( 0.392761)
Date.strptime: 6.320000 0.050000 6.370000 ( 6.373248)