rails_timezones.patch

Artem Vasiliev, 2008-07-29 15:09

Download (3.85 KB)

View differences:

app/controllers/application.rb (working copy)
18 18
require 'uri'
19 19

  
20 20
class ApplicationController < ActionController::Base
21
  before_filter :user_setup, :check_if_login_required, :set_localization
21
  before_filter :user_setup, :check_if_login_required, :set_localization, 
22
    :set_time_zone
23

  
22 24
  filter_parameter_logging :password
23 25
  
24 26
  include Redmine::MenuManager::MenuController
......
28 30
    require_dependency "repository/#{scm.underscore}"
29 31
  end
30 32
  
33
  def set_time_zone
34
    Time.zone = User.current.time_zone if User.current
35
  end
36

  
31 37
  def current_role
32 38
    @current_role ||= User.current.role_for_project(@project)
33 39
  end
app/helpers/application_helper.rb (working copy)
88 88
  def format_time(time, include_date = true)
89 89
    return nil unless time
90 90
    time = time.to_time if time.is_a?(String)
91
    zone = User.current.time_zone
92
    local = zone ? time.in_time_zone(zone) : (time.utc? ? time.utc_to_local : time)
93 91
    @date_format ||= (Setting.date_format.blank? || Setting.date_format.size < 2 ? l(:general_fmt_date) : Setting.date_format)
94 92
    @time_format ||= (Setting.time_format.blank? ? l(:general_fmt_time) : Setting.time_format)
95
    include_date ? local.strftime("#{@date_format} #{@time_format}") : local.strftime(@time_format)
93
    format = include_date ? "#{@date_format} #{@time_format}" : @time_format    
94
    time.strftime(format)
96 95
  end
97 96
  
98 97
  # Truncates and returns the string as a single line
config/environment.rb (working copy)
43 43
  # config.active_record.observers = :cacher, :garbage_collector
44 44
  config.active_record.observers = :message_observer
45 45

  
46
  # Make Active Record use UTC-base instead of local time
47
  # config.active_record.default_timezone = :utc
46
  #turn on Rails' support of time zones. Place your server's timezone here, 
47
  #  use rake time:zones:local to list seem-to-be suitable timezones.
48
  config.time_zone = 'Moscow'
48 49
  
49 50
  # Use Active Record's schema dumper instead of SQL when creating the test database
50 51
  # (enables use of different database adapters for development and test environments)
lib/tasks/convert_database_times_to_utc.rake (revision 0)
1
#  Be sure to correct LOCAL_UTC_OFFSET and back up database prior to running.
2
namespace :redmine do
3
  desc 'Migrates existing time values in database to UTC.'
4
  task :convert_database_times_to_utc => :environment do
5
    LOCAL_UTC_OFFSET = 1.hours #Time offset of server that Redmine was running on. 1.hours is for e.g. BST.
6
    [Issue, Journal, Version, Message, Attachment, Project, Changeset, News, 
7
      TimeEntry, WikiPage, WikiContent, WikiContent::Version].each do |klass|
8
        puts "\n\nUpdating #{klass} timestamps to UTC"
9
        time_columns = klass.columns.select do |c| 
10
          [:datetime, :timestamp].include?(c.type)
11
        end
12
        klass.all.each do |obj|
13
          modified = false
14
          time_columns.each do |c|
15
            obj.send("#{c.name}=", obj.send("#{c.name}")) - LOCAL_UTC_OFFSET
16
            modified = true
17
          end
18
          if modified
19
            raise "error saving #{obj}" if !obj.save_with_validation(false)
20
            print '.'
21
          end
22
        end
23
    end
24
  end
25
end