为什么 Rails Weekday 索引与 Ruby 的不同?

发布于 2024-12-09 15:32:11 字数 1160 浏览 1 评论 0原文

在 Rails 3.0.10 中

ruby-1.9.2-p180 :010 > Time::DAYS_INTO_WEEK
 => {:monday=>0, :tuesday=>1, :wednesday=>2, :thursday=>3, :friday=>4, :saturday=>5, :sunday=>6} 

ruby-1.9.2-p180 :011 > Date.today
 => Mon, 10 Oct 2011 
ruby-1.9.2-p180 :012 > Date.today.wday
 => 1 

星期一在时间映射中为 0,在日期映射中为 1。
https://github.com/rails /rails/blob/master/activesupport/lib/active_support/core_ext/time/calculations.rb

我觉得从周日开始,因为零是更安全、更常见、更容易被其他人理解的方式做事。我希望了解历史/推理,以防我忽略某些事情。

我的用例:我将星期几明确存储在我的数据库中。我认为存储为整数比存储“星期一”、“星期二”等更有效且更容易使用。

PS 我有一个好主意,可以做如下的事情。从方法内的工作日字符串来回映射(隐藏整数)。

ruby-1.9.2-p180 :010 > Time::DAYS_INTO_WEEK
 => {:monday=>0, :tuesday=>1, :wednesday=>2, :thursday=>3, :friday=>4, :saturday=>5, :sunday=>6} 
Time::DAYS_INTO_WEEK[day_of_week.downcase.to_sym]
Time::DAYS_INTO_WEEK.to_a.select{|k, v| v == start_day }.first.first.to_s.capitalize

in Rails 3.0.10

ruby-1.9.2-p180 :010 > Time::DAYS_INTO_WEEK
 => {:monday=>0, :tuesday=>1, :wednesday=>2, :thursday=>3, :friday=>4, :saturday=>5, :sunday=>6} 

And

ruby-1.9.2-p180 :011 > Date.today
 => Mon, 10 Oct 2011 
ruby-1.9.2-p180 :012 > Date.today.wday
 => 1 

So, Monday is 0 in the Time mapping, and 1 in the Date mapping.
https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/time/calculations.rb

I feel like starting with Sunday as zero is the safer, more common, more likely to be understood by someone else way to do things. I was hoping to understand the history / reasoning in case I overlook something.

My use case: I'm storing day of week explicitly in my db. I figured storing as an integer would be more efficient and easier to work with than storing "Monday", "Tuesday", etc.

PS I had the great idea to do something like the following. Map back and forth from weekday strings inside methods (hiding the integer).

ruby-1.9.2-p180 :010 > Time::DAYS_INTO_WEEK
 => {:monday=>0, :tuesday=>1, :wednesday=>2, :thursday=>3, :friday=>4, :saturday=>5, :sunday=>6} 
Time::DAYS_INTO_WEEK[day_of_week.downcase.to_sym]
Time::DAYS_INTO_WEEK.to_a.select{|k, v| v == start_day }.first.first.to_s.capitalize

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇柔作态 2024-12-16 15:32:11

我不确定这个问题有绝对正确的答案。我认为扎巴注意到了美国/加拿大和国际上星期数的区别。

关于历史推理,David Heinemeier Hansson (DHH) 似乎于 2005 年 2 月 21 日做出了以下承诺:
https://github.com/rails/rails/commit/25f8a25c3ea107dcd0688307ac0ce19c4306f6b4

提交消息明确指出,有意将一周的开始定为星期一而不是星期日。考虑到 David 是 Rails 的创建者,他固执己见,而且当时仍然住在丹麦,所以他忽略了 Ruby 的日期和时间类考虑星期日第 0 天这一事实是有道理的。

(后来提交 1c5a6944d38e6818d254f272057b513b038b2270 移动了 days_into_week变成你现在看到的常数DAYS_INTO_WEEK;后来,相同的常量被添加到提交 bc1bcddede0c300e9c88f76a66a152814b734981 中的 Date 类中。

I'm not sure there is a absolutely correct answer to this question. I think Zabba was onto something regarding the difference between how days of weeks are numbered in the US/Canada and Internationally.

Regarding historical reasoning, it looks like David Heinemeier Hansson (DHH) made the following commit on February 21, 2005:
https://github.com/rails/rails/commit/25f8a25c3ea107dcd0688307ac0ce19c4306f6b4

The commit message clearly states that it was intentional to make beginning of week be a Monday and not a Sunday. Considering that David is opinionated, the creator of Rails, and was still living in Denmark at the time, it sort of makes sense that he'd ignore the fact that Ruby's Date and Time classes consider Sunday day 0.

(later commit 1c5a6944d38e6818d254f272057b513b038b2270 moved days_into_week into the constant you see now DAYS_INTO_WEEK; and later the same constant was added to the Date class in commit bc1bcddede0c300e9c88f76a66a152814b734981).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文