Corrected with rubocop cop 'Rails/TimeZone'.

This commit is contained in:
Thorsten Eckel 2015-05-08 12:20:33 +02:00
parent f3a9fdb8b3
commit 409ae23e78
19 changed files with 56 additions and 56 deletions

View file

@ -196,8 +196,6 @@ Style/CommentIndentation:
Enabled: false Enabled: false
Style/GlobalVars: Style/GlobalVars:
Enabled: false Enabled: false
Rails/TimeZone:
Enabled: false
Lint/RescueException: Lint/RescueException:
Enabled: false Enabled: false
Style/ClassVars: Style/ClassVars:

View file

@ -82,9 +82,7 @@ class ApplicationController < ActionController::Base
def session_update def session_update
#sleep 0.6 #sleep 0.6
# on many paralell requests, session got reinitialised if Time. is used, as workaround use DateTime. session[:ping] = Time.zone.now.iso8601
#session[:ping] = Time.now.utc.iso8601
session[:ping] = DateTime.now.iso8601
# check if remote ip need to be updated # check if remote ip need to be updated
if !session[:remote_id] || session[:remote_id] != request.remote_ip if !session[:remote_id] || session[:remote_id] != request.remote_ip

View file

@ -125,8 +125,10 @@ class IcalTicketsController < ApplicationController
event_data = {} event_data = {}
# rubocop:disable Rails/TimeZone
event_data[:dtstart] = Icalendar::Values::DateTime.new( ticket.pending_time ) event_data[:dtstart] = Icalendar::Values::DateTime.new( ticket.pending_time )
event_data[:dtend] = Icalendar::Values::DateTime.new( ticket.pending_time ) event_data[:dtend] = Icalendar::Values::DateTime.new( ticket.pending_time )
# rubocop:enable Rails/TimeZone
event_data[:summary] = "#{ ticket.state.name } ticket: '#{ ticket.title }'" event_data[:summary] = "#{ ticket.state.name } ticket: '#{ ticket.title }'"
event_data[:description] = "T##{ ticket.number }" event_data[:description] = "T##{ ticket.number }"
@ -153,8 +155,10 @@ class IcalTicketsController < ApplicationController
event_data = {} event_data = {}
# rubocop:disable Rails/TimeZone
event_data[:dtstart] = Icalendar::Values::DateTime.new( ticket.escalation_time ) event_data[:dtstart] = Icalendar::Values::DateTime.new( ticket.escalation_time )
event_data[:dtend] = Icalendar::Values::DateTime.new( ticket.escalation_time ) event_data[:dtend] = Icalendar::Values::DateTime.new( ticket.escalation_time )
# rubocop:enable Rails/TimeZone
event_data[:summary] = "ticket escalation: '#{ ticket.title }'" event_data[:summary] = "ticket escalation: '#{ ticket.title }'"
event_data[:description] = "T##{ ticket.number }" event_data[:description] = "T##{ ticket.number }"

View file

@ -29,7 +29,7 @@ class LongPollingController < ApplicationController
# error handling # error handling
if params['data']['timestamp'] if params['data']['timestamp']
log "request spool data > '#{Time.at( params['data']['timestamp'] )}'", client_id log "request spool data > '#{Time.zone.at( params['data']['timestamp'] )}'", client_id
else else
log 'request spool init data', client_id log 'request spool init data', client_id
end end
@ -50,7 +50,7 @@ class LongPollingController < ApplicationController
# send spool:sent event to client # send spool:sent event to client
sleep 0.2 sleep 0.2
log 'send spool:sent event', client_id log 'send spool:sent event', client_id
Sessions.send( client_id, { event: 'spool:sent', data: { timestamp: Time.now.utc.to_i } } ) Sessions.send( client_id, { event: 'spool:sent', data: { timestamp: Time.zone.now.utc.to_i } } )
end end
# receive message # receive message

View file

@ -45,7 +45,7 @@ class SessionsController < ApplicationController
# check logon session # check logon session
logon_session_key = nil logon_session_key = nil
if params['logon_session'] if params['logon_session']
logon_session_key = Digest::MD5.hexdigest( rand(999_999).to_s + Time.new.to_s ) logon_session_key = Digest::MD5.hexdigest( rand(999_999).to_s + Time.zone.now.to_s )
# session = ActiveRecord::SessionStore::Session.create( # session = ActiveRecord::SessionStore::Session.create(
# :session_id => logon_session_key, # :session_id => logon_session_key,
# :data => { # :data => {

View file

@ -29,7 +29,7 @@ returns
role = self.class.activity_stream_support_config[:role] role = self.class.activity_stream_support_config[:role]
updated_at = self.updated_at updated_at = self.updated_at
if force if force
updated_at = Time.new updated_at = Time.zone.now
end end
ActivityStream.add( ActivityStream.add(
o_id: self['id'], o_id: self['id'],

View file

@ -12,7 +12,7 @@ class Job < ApplicationModel
notify_clients_support notify_clients_support
def self.run def self.run
time = Time.new time = Time.zone.now
day_map = { day_map = {
0 => 'sun', 0 => 'sun',
1 => 'mon', 1 => 'mon',
@ -26,11 +26,11 @@ class Job < ApplicationModel
jobs.each do |job| jobs.each do |job|
# only execute jobs, older then 1 min, to give admin posibility to change # only execute jobs, older then 1 min, to give admin posibility to change
next if job.updated_at > Time.now - 1.minutes next if job.updated_at > Time.zone.now - 1.minutes
# check if jobs need to be executed # check if jobs need to be executed
# ignore if job was running within last 10 min. # ignore if job was running within last 10 min.
next if job.last_run_at && job.last_run_at > Time.now - 10.minutes next if job.last_run_at && job.last_run_at > Time.zone.now - 10.minutes
# check day # check day
next if !job.timeplan['days'].include?( day_map[time.wday] ) next if !job.timeplan['days'].include?( day_map[time.wday] )
@ -66,7 +66,7 @@ class Job < ApplicationModel
ticket.save ticket.save
end end
job.last_run_at = Time.now job.last_run_at = Time.zone.now
job.save job.save
end end
true true

View file

@ -27,6 +27,6 @@ class Observer::Ticket::CloseTime < ActiveRecord::Observer
return true if state_type.name != 'closed' return true if state_type.name != 'closed'
# set close_time # set close_time
record.close_time = Time.now record.close_time = Time.zone.now
end end
end end

View file

@ -20,7 +20,7 @@ class Package < ApplicationModel
end end
build_date = REXML::Element.new('build_date') build_date = REXML::Element.new('build_date')
build_date.text = Time.now.utc.iso8601 build_date.text = Time.zone.now.iso8601
build_host = REXML::Element.new('build_host') build_host = REXML::Element.new('build_host')
build_host.text = Socket.gethostname build_host.text = Socket.gethostname

View file

@ -61,10 +61,10 @@ class Scheduler < ApplicationModel
} }
end end
def self._start_job( job, runner, runner_count, try_count = 0, try_run_time = Time.now ) def self._start_job( job, runner, runner_count, try_count = 0, try_run_time = Time.zone.now )
sleep 5 sleep 5
begin begin
job.last_run = Time.now job.last_run = Time.zone.now
job.pid = Thread.current.object_id job.pid = Thread.current.object_id
job.save job.save
logger.info "execute #{job.method} (runner #{runner} of #{runner_count}, try_count #{try_count})..." logger.info "execute #{job.method} (runner #{runner} of #{runner_count}, try_count #{try_count})..."
@ -83,10 +83,10 @@ class Scheduler < ApplicationModel
try_count += 1 try_count += 1
# reset error counter if to old # reset error counter if to old
if try_run_time + ( 60 * 5 ) < Time.now if try_run_time + ( 60 * 5 ) < Time.zone.now
try_count = 0 try_count = 0
end end
try_run_time = Time.now try_run_time = Time.zone.now
# restart job again # restart job again
if try_run_max > try_count if try_run_max > try_count
@ -122,8 +122,8 @@ class Scheduler < ApplicationModel
end end
def self.check( name, time_warning = 10, time_critical = 20 ) def self.check( name, time_warning = 10, time_critical = 20 )
time_warning_time = Time.now - time_warning.minutes time_warning_time = Time.zone.now - time_warning.minutes
time_critical_time = Time.now - time_critical.minutes time_critical_time = Time.zone.now - time_critical.minutes
scheduler = Scheduler.find_by( name: name ) scheduler = Scheduler.find_by( name: name )
if !scheduler if !scheduler
puts "CRITICAL - no such scheduler jobs '#{name}'" puts "CRITICAL - no such scheduler jobs '#{name}'"

View file

@ -9,7 +9,7 @@ class Taskbar < ApplicationModel
private private
def update_last_contact def update_last_contact
self.last_contact = Time.now self.last_contact = Time.zone.now
end end
def set_user def set_user

View file

@ -206,7 +206,7 @@ returns
sql += " #{key} IN (?)" sql += " #{key} IN (?)"
bind.push value bind.push value
elsif value.class == Hash || value.class == ActiveSupport::HashWithIndifferentAccess elsif value.class == Hash || value.class == ActiveSupport::HashWithIndifferentAccess
time = Time.now time = Time.zone.now
if value['area'] == 'minute' if value['area'] == 'minute'
if value['direction'] == 'last' if value['direction'] == 'last'
time -= value['count'].to_i * 60 time -= value['count'].to_i * 60

View file

@ -399,7 +399,7 @@ returns
=end =end
def update_last_login def update_last_login
self.last_login = Time.now self.last_login = Time.zone.now
# reset login failed # reset login failed
self.login_failed = 0 self.login_failed = 0

View file

@ -29,10 +29,10 @@ module SessionHelper
def self.cleanup_expired def self.cleanup_expired
# web sessions # web sessions
ActiveRecord::SessionStore::Session.where('request_type = ? AND updated_at < ?', 1, Time.now - 90.days ).delete_all ActiveRecord::SessionStore::Session.where('request_type = ? AND updated_at < ?', 1, Time.zone.now - 90.days ).delete_all
# http basic auth calls # http basic auth calls
ActiveRecord::SessionStore::Session.where('request_type = ? AND updated_at < ?', 2, Time.now - 2.days ).delete_all ActiveRecord::SessionStore::Session.where('request_type = ? AND updated_at < ?', 2, Time.zone.now - 2.days ).delete_all
end end
def self.get(id) def self.get(id)

View file

@ -33,7 +33,7 @@ returns
session_file = "#{path_tmp}/session" session_file = "#{path_tmp}/session"
# collect session data # collect session data
meta[:last_ping] = Time.new.to_i.to_s meta[:last_ping] = Time.zone.now.to_i.to_s
data = { data = {
user: session, user: session,
meta: meta, meta: meta,
@ -189,7 +189,7 @@ returns
list_of_closed_sessions = [] list_of_closed_sessions = []
clients = Sessions.list clients = Sessions.list
clients.each { |client_id, client| clients.each { |client_id, client|
if !client[:meta] || !client[:meta][:last_ping] || ( client[:meta][:last_ping].to_i + idle_time_in_sec ) < Time.now.to_i if !client[:meta] || !client[:meta][:last_ping] || ( client[:meta][:last_ping].to_i + idle_time_in_sec ) < Time.zone.now.to_i
list_of_closed_sessions.push client_id list_of_closed_sessions.push client_id
Sessions.destory( client_id ) Sessions.destory( client_id )
end end
@ -213,7 +213,7 @@ returns
data = get(client_id) data = get(client_id)
return false if !data return false if !data
path = "#{@path}/#{client_id}" path = "#{@path}/#{client_id}"
data[:meta][:last_ping] = Time.new.to_i.to_s data[:meta][:last_ping] = Time.zone.now.to_i.to_s
content = data.to_json content = data.to_json
File.open( path + '/session', 'wb' ) { |file| File.open( path + '/session', 'wb' ) { |file|
file.write content file.write content
@ -289,7 +289,7 @@ returns
def self.send( client_id, data ) def self.send( client_id, data )
path = "#{@path}/#{client_id}/" path = "#{@path}/#{client_id}/"
filename = "send-#{ Time.new().to_f }" filename = "send-#{ Time.zone.now.to_f }"
check = true check = true
count = 0 count = 0
while check while check
@ -422,11 +422,11 @@ returns
def self.spool_create( msg ) def self.spool_create( msg )
path = "#{@path}/spool/" path = "#{@path}/spool/"
FileUtils.mkpath path FileUtils.mkpath path
file = "#{Time.new.to_f}-#{rand(99_999)}" file = "#{Time.zone.now.to_f}-#{rand(99_999)}"
File.open( path + '/' + file, 'wb' ) { |file| File.open( path + '/' + file, 'wb' ) { |file|
data = { data = {
msg: msg, msg: msg,
timestamp: Time.now.to_i, timestamp: Time.zone.now.to_i,
} }
file.write data.to_json file.write data.to_json
} }
@ -457,7 +457,7 @@ returns
end end
# ignore message older then 48h # ignore message older then 48h
if spool['timestamp'] + (2 * 86_400) < Time.now.to_i if spool['timestamp'] + (2 * 86_400) < Time.zone.now.to_i
to_delete.push "#{path}/#{entry}" to_delete.push "#{path}/#{entry}"
next next
end end
@ -568,7 +568,7 @@ returns
=end =end
def self.thread_client(client_id, try_count = 0, try_run_time = Time.now) def self.thread_client(client_id, try_count = 0, try_run_time = Time.zone.now)
Rails.logger.debug "LOOP #{client_id} - #{try_count}" Rails.logger.debug "LOOP #{client_id} - #{try_count}"
begin begin
Sessions::Client.new(client_id) Sessions::Client.new(client_id)
@ -586,10 +586,10 @@ returns
try_count += 1 try_count += 1
# reset error counter if to old # reset error counter if to old
if try_run_time + ( 60 * 5 ) < Time.now if try_run_time + ( 60 * 5 ) < Time.zone.now
try_count = 0 try_count = 0
end end
try_run_time = Time.now try_run_time = Time.zone.now
# restart job again # restart job again
if try_run_max > try_count if try_run_max > try_count

View file

@ -11,11 +11,11 @@ module Sessions::CacheIn
def self.set( key, value, params = {} ) def self.set( key, value, params = {} )
if params[:expires_in] if params[:expires_in]
@@expires_in[key] = Time.now + params[:expires_in] @@expires_in[key] = Time.zone.now + params[:expires_in]
@@expires_in_ttl[key] = params[:expires_in] @@expires_in_ttl[key] = params[:expires_in]
end end
@@data[ key ] = value @@data[ key ] = value
@@data_time[ key ] = Time.now @@data_time[ key ] = Time.zone.now
end end
def self.expired( key, params = {} ) def self.expired( key, params = {} )
@ -29,14 +29,14 @@ module Sessions::CacheIn
# set re_expire # set re_expire
if params[:re_expire] if params[:re_expire]
if @@expires_in[key] if @@expires_in[key]
@@expires_in[key] = Time.now + @@expires_in_ttl[key] @@expires_in[key] = Time.zone.now + @@expires_in_ttl[key]
end end
return false return false
end end
# check if expired # check if expired
if @@expires_in[key] if @@expires_in[key]
return true if @@expires_in[key] < Time.now return true if @@expires_in[key] < Time.zone.now
return false return false
end end

View file

@ -5,7 +5,7 @@ namespace :test do
desc 'Start browser tests' desc 'Start browser tests'
task :browser, [:opts] => :environment do |_t, args| task :browser, [:opts] => :environment do |_t, args|
start = Time.now() start = Time.zone.now
if !args.opts if !args.opts
args.opts = '' args.opts = ''
end end
@ -14,7 +14,7 @@ namespace :test do
fail 'Failed test. ' + res.inspect if !ok fail 'Failed test. ' + res.inspect if !ok
end end
} }
puts 'All browser tests, elapsed: ' + (Time.now() - start).to_s + ' seconds' puts 'All browser tests, elapsed: ' + (Time.zone.now - start).to_s + ' seconds'
end end
end end

View file

@ -22,15 +22,15 @@ put working hours matrix and timezone in function, returns UTC working hours mat
time_diff = 0 time_diff = 0
if timezone if timezone
begin begin
time_diff = Time.parse(start_time.to_s).in_time_zone(timezone).utc_offset time_diff = Time.zone.parse(start_time.to_s).in_time_zone(timezone).utc_offset
rescue Exception => e rescue Exception => e
Rails.logger.error "Can't fine tomezone #{timezone}" Rails.logger.error "Can't fine tomezone #{timezone}"
Rails.logger.error e.inspect Rails.logger.error e.inspect
Rails.logger.error e.backtrace Rails.logger.error e.backtrace
end end
end end
beginning_of_workday = Time.parse("1977-10-27 #{config['beginning_of_workday']}") beginning_of_workday = Time.zone.parse("1977-10-27 #{config['beginning_of_workday']}")
end_of_workday = Time.parse("1977-10-27 #{config['end_of_workday']}") - 3600 end_of_workday = Time.zone.parse("1977-10-27 #{config['end_of_workday']}") - 3600
config_ok = false config_ok = false
working_hours = {} working_hours = {}
[:Mon, :Tue, :Wed, :Thu, :Fri, :Sat, :Sun].each {|day| [:Mon, :Tue, :Wed, :Thu, :Fri, :Sat, :Sun].each {|day|
@ -43,7 +43,7 @@ put working hours matrix and timezone in function, returns UTC working hours mat
config_ok = true config_ok = true
(0..23).each {|hour| (0..23).each {|hour|
time = Time.parse("1977-10-27 #{hour}:00:00") time = Time.zone.parse("1977-10-27 #{hour}:00:00")
if time >= beginning_of_workday && time <= end_of_workday if time >= beginning_of_workday && time <= end_of_workday
working_hours[day].push true working_hours[day].push true
else else
@ -116,10 +116,10 @@ put working hours matrix and timezone in function, returns UTC working hours mat
def self.business_time_diff(start_time, end_time, config = nil, timezone = '') def self.business_time_diff(start_time, end_time, config = nil, timezone = '')
if start_time.class == String if start_time.class == String
start_time = Time.parse( start_time.to_s + 'UTC' ) start_time = Time.zone.parse( start_time.to_s + 'UTC' )
end end
if end_time.class == String if end_time.class == String
end_time = Time.parse( end_time.to_s + 'UTC' ) end_time = Time.zone.parse( end_time.to_s + 'UTC' )
end end
# if no config is given, just return calculation directly # if no config is given, just return calculation directly
@ -236,7 +236,7 @@ put working hours matrix and timezone in function, returns UTC working hours mat
def self.dest_time(start_time, diff_in_min, config = nil, timezone = '') def self.dest_time(start_time, diff_in_min, config = nil, timezone = '')
if start_time.class == String if start_time.class == String
start_time = Time.parse( start_time.to_s + ' UTC' ) start_time = Time.zone.parse( start_time.to_s + ' UTC' )
end end
return start_time if diff_in_min == 0 return start_time if diff_in_min == 0

View file

@ -95,7 +95,7 @@ EventMachine.run {
if !@clients.include? client_id if !@clients.include? client_id
@clients[client_id] = { @clients[client_id] = {
websocket: ws, websocket: ws,
last_ping: Time.new, last_ping: Time.zone.now,
error_count: 0, error_count: 0,
} }
end end
@ -139,7 +139,7 @@ EventMachine.run {
# error handling # error handling
if data['timestamp'] if data['timestamp']
log 'notice', "request spool data > '#{Time.at(data['timestamp'])}'", client_id log 'notice', "request spool data > '#{Time.zone.at(data['timestamp'])}'", client_id
else else
log 'notice', 'request spool with init data', client_id log 'notice', 'request spool with init data', client_id
end end
@ -164,7 +164,7 @@ EventMachine.run {
# send spool:sent event to client # send spool:sent event to client
log 'notice', 'send spool:sent event', client_id log 'notice', 'send spool:sent event', client_id
@clients[client_id][:websocket].send( '[{"event":"spool:sent","data":{"timestamp":' + Time.now.utc.to_i.to_s + '}}]' ) @clients[client_id][:websocket].send( '[{"event":"spool:sent","data":{"timestamp":' + Time.zone.now.to_i.to_s + '}}]' )
end end
# get session # get session
@ -175,7 +175,7 @@ EventMachine.run {
# remember ping, send pong back # remember ping, send pong back
elsif data['action'] == 'ping' elsif data['action'] == 'ping'
Sessions.touch(client_id) Sessions.touch(client_id)
@clients[client_id][:last_ping] = Time.now @clients[client_id][:last_ping] = Time.zone.now
@clients[client_id][:websocket].send( '[{"action":"pong"}]' ) @clients[client_id][:websocket].send( '[{"action":"pong"}]' )
# broadcast # broadcast
@ -298,7 +298,7 @@ EventMachine.run {
# close unused web socket sessions # close unused web socket sessions
@clients.each { |client_id, client| @clients.each { |client_id, client|
next if ( client[:last_ping] + idle_time_in_sec ) >= Time.now next if ( client[:last_ping] + idle_time_in_sec ) >= Time.zone.now
log 'notice', 'closing idle websocket connection', client_id log 'notice', 'closing idle websocket connection', client_id
@ -324,8 +324,8 @@ EventMachine.run {
if !@options[:v] if !@options[:v]
return if level == 'debug' return if level == 'debug'
end end
puts "#{Time.now}:client(#{ client_id }) #{ data }" puts "#{Time.zone.now}:client(#{ client_id }) #{ data }"
# puts "#{Time.now}:#{ level }:client(#{ client_id }) #{ data }" # puts "#{Time.zone.now}:#{ level }:client(#{ client_id }) #{ data }"
end end
} }