Project

General

Profile

Patch #20473 ยป 0001-Use-dates-according-to-the-user-s-timezone-in-querie.patch

Holger Just, 2015-08-04 18:51

View differences:

app/models/query.rb
751 751
    when "w"
752 752
      # = this week
753 753
      first_day_of_week = l(:general_first_day_of_week).to_i
754
      day_of_week = Date.today.cwday
754
      day_of_week = User.current.today.cwday
755 755
      days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week)
756 756
      sql = relative_date_clause(db_table, db_field, - days_ago, - days_ago + 6, is_custom_filter)
757 757
    when "lw"
758 758
      # = last week
759 759
      first_day_of_week = l(:general_first_day_of_week).to_i
760
      day_of_week = Date.today.cwday
760
      day_of_week = User.current.today.cwday
761 761
      days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week)
762 762
      sql = relative_date_clause(db_table, db_field, - days_ago - 7, - days_ago - 1, is_custom_filter)
763 763
    when "l2w"
764 764
      # = last 2 weeks
765 765
      first_day_of_week = l(:general_first_day_of_week).to_i
766
      day_of_week = Date.today.cwday
766
      day_of_week = User.current.today.cwday
767 767
      days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week)
768 768
      sql = relative_date_clause(db_table, db_field, - days_ago - 14, - days_ago - 1, is_custom_filter)
769 769
    when "m"
770 770
      # = this month
771
      date = Date.today
771
      date = User.current.today
772 772
      sql = date_clause(db_table, db_field, date.beginning_of_month, date.end_of_month, is_custom_filter)
773 773
    when "lm"
774 774
      # = last month
775
      date = Date.today.prev_month
775
      date = User.current.today.prev_month
776 776
      sql = date_clause(db_table, db_field, date.beginning_of_month, date.end_of_month, is_custom_filter)
777 777
    when "y"
778 778
      # = this year
779
      date = Date.today
779
      date = User.current.today
780 780
      sql = date_clause(db_table, db_field, date.beginning_of_year, date.end_of_year, is_custom_filter)
781 781
    when "~"
782 782
      sql = sql_contains("#{db_table}.#{db_field}", value.first)
......
848 848
    end
849 849
  end
850 850

  
851
  def date_for_user_time_zone(y, m, d)
852
    if tz = User.current.time_zone
853
      tz.local y, m, d
854
    else
855
      Time.local y, m, d
856
    end
857
  end
858

  
851 859
  # Returns a SQL clause for a date or datetime field.
852 860
  def date_clause(table, field, from, to, is_custom_filter)
853 861
    s = []
854 862
    if from
855 863
      if from.is_a?(Date)
856
        from = Time.local(from.year, from.month, from.day).yesterday.end_of_day
864
        from = date_for_user_time_zone(from.year, from.month, from.day).yesterday.end_of_day
857 865
      else
858 866
        from = from - 1 # second
859 867
      end
......
864 872
    end
865 873
    if to
866 874
      if to.is_a?(Date)
867
        to = Time.local(to.year, to.month, to.day).end_of_day
875
        to = date_for_user_time_zone(to.year, to.month, to.day).end_of_day
868 876
      end
869 877
      if self.class.default_timezone == :utc
870 878
        to = to.utc
......
876 884

  
877 885
  # Returns a SQL clause for a date or datetime field using relative dates.
878 886
  def relative_date_clause(table, field, days_from, days_to, is_custom_filter)
879
    date_clause(table, field, (days_from ? Date.today + days_from : nil), (days_to ? Date.today + days_to : nil), is_custom_filter)
887
    date_clause(table, field, (days_from ? User.current.today + days_from : nil), (days_to ? User.current.today + days_to : nil), is_custom_filter)
880 888
  end
881 889

  
882 890
  # Returns a Date or Time from the given filter value
    (1-1/1)