如何让“一周”从某一天而不是周一开始?

时间:2022-10-21 12:54:10

In an application am building, i'm trying to make the week starts with Saturday. In ruby on rails, by default, the week begins on Monday.

在一个正在建设的应用程序中,我试图让这一周从周六开始。在ruby on rails中,默认情况下,一周从周一开始。

So If you have any trick or a patch to make it work for me!

所以如果你有什么诡计或补丁让它为我工作!

Thanks in advance!

提前谢谢!

3 个解决方案

#1


9  

I've managed to make a pull request into rails and now you can pass symbol argument to beginning_of_week method. For example beginning_of_week(:sunday) will give you a Sunday assuming that week starts on Sunday. The same for end_of_week method. But you have to wait until rails 3.2 release in case you are not on the bleeding edge.

我已经成功地向rails发出拉入请求,现在您可以将符号参数传递给beginning_of_week方法。例如,从周日开始,周日就会给你一个星期天的假设。end_of_week方法也是如此。但是,您必须等到rails 3.2发布时,以防您处于危险边缘。

See this for more info: https://github.com/rails/rails/pull/3547

更多信息请参见本文:https://github.com/rails/rails/pull/3547

UPDATE: Now I'm waiting for the new PR to be accepted, it makes possible to set default week start in your rails app config. See this for more info https://github.com/rails/rails/pull/5339

更新:现在我正在等待新的PR被接受,它可以在你的rails应用程序配置中设置默认的week start。更多信息请参见本文https://github.com/rails/rails/pull/5339

UPDATE:

更新:

Merged!

合并!

rafaelfranca merged commit 5428de1 into rails:master from gregolsen:week_start_config 4 months ago

Closed
rafaelfranca closed the pull request 4 months ago

关闭的拉弗朗卡在4个月前关闭了拉皮请求

#2


4  

You could throw this into an initializer to make beginning_of_week return Sunday:

您可以将它放入初始化器中,使beginning_of_week return Sunday:

module ActiveSupport #:nodoc:
  module CoreExtensions #:nodoc:
    module Time #:nodoc:
      module Calculations
        def beginning_of_week
          (self - self.wday.days).midnight
        end
      end
    end
  end
end

It may be safer however for you to define your own method and leave the stock one intact:

但是,对您来说,更安全的做法是定义您自己的方法,并保持库存方法不变:

module ActiveSupport #:nodoc:
  module CoreExtensions #:nodoc:
    module Time #:nodoc:
      module Calculations
        def traditional_beginning_of_week
          (self - self.wday.days).midnight
        end
      end
    end
  end
end

#3


2  

You can try replacing Date#wday and Time#wday methods with your own. I think Rails' support methods like beginning_of_week etc. rely on wday and will work out of the box.

您可以尝试用您自己的方法替换Date#wday和Time#wday方法。我认为Rails的支持方法,比如beginning_of_week等,都是基于wday的,而且是开箱即用的。

Here's some code, but it's definitely just an idea, neither tested nor recommended thing to do:

这里有一些代码,但它肯定只是一个想法,既没有测试也没有推荐:

require 'activesupport'
#=> true
Time.now.wday
#=> 4
Time.now.beginning_of_week
#=> 2010-04-19 00:00:00 0200

class Time
  alias_method :orig_wday, :wday
  def wday 
    (self.orig_wday + 2) % 7
  end
end

Time.now.wday
#=> 6
Time.now.beginning_of_week
#=> 2010-04-17 00:00:00 0200

#1


9  

I've managed to make a pull request into rails and now you can pass symbol argument to beginning_of_week method. For example beginning_of_week(:sunday) will give you a Sunday assuming that week starts on Sunday. The same for end_of_week method. But you have to wait until rails 3.2 release in case you are not on the bleeding edge.

我已经成功地向rails发出拉入请求,现在您可以将符号参数传递给beginning_of_week方法。例如,从周日开始,周日就会给你一个星期天的假设。end_of_week方法也是如此。但是,您必须等到rails 3.2发布时,以防您处于危险边缘。

See this for more info: https://github.com/rails/rails/pull/3547

更多信息请参见本文:https://github.com/rails/rails/pull/3547

UPDATE: Now I'm waiting for the new PR to be accepted, it makes possible to set default week start in your rails app config. See this for more info https://github.com/rails/rails/pull/5339

更新:现在我正在等待新的PR被接受,它可以在你的rails应用程序配置中设置默认的week start。更多信息请参见本文https://github.com/rails/rails/pull/5339

UPDATE:

更新:

Merged!

合并!

rafaelfranca merged commit 5428de1 into rails:master from gregolsen:week_start_config 4 months ago

Closed
rafaelfranca closed the pull request 4 months ago

关闭的拉弗朗卡在4个月前关闭了拉皮请求

#2


4  

You could throw this into an initializer to make beginning_of_week return Sunday:

您可以将它放入初始化器中,使beginning_of_week return Sunday:

module ActiveSupport #:nodoc:
  module CoreExtensions #:nodoc:
    module Time #:nodoc:
      module Calculations
        def beginning_of_week
          (self - self.wday.days).midnight
        end
      end
    end
  end
end

It may be safer however for you to define your own method and leave the stock one intact:

但是,对您来说,更安全的做法是定义您自己的方法,并保持库存方法不变:

module ActiveSupport #:nodoc:
  module CoreExtensions #:nodoc:
    module Time #:nodoc:
      module Calculations
        def traditional_beginning_of_week
          (self - self.wday.days).midnight
        end
      end
    end
  end
end

#3


2  

You can try replacing Date#wday and Time#wday methods with your own. I think Rails' support methods like beginning_of_week etc. rely on wday and will work out of the box.

您可以尝试用您自己的方法替换Date#wday和Time#wday方法。我认为Rails的支持方法,比如beginning_of_week等,都是基于wday的,而且是开箱即用的。

Here's some code, but it's definitely just an idea, neither tested nor recommended thing to do:

这里有一些代码,但它肯定只是一个想法,既没有测试也没有推荐:

require 'activesupport'
#=> true
Time.now.wday
#=> 4
Time.now.beginning_of_week
#=> 2010-04-19 00:00:00 0200

class Time
  alias_method :orig_wday, :wday
  def wday 
    (self.orig_wday + 2) % 7
  end
end

Time.now.wday
#=> 6
Time.now.beginning_of_week
#=> 2010-04-17 00:00:00 0200