Forum: Ruby Days in a year

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Paul B. (Guest)
on 2008-10-27 16:12
How do I get number of days in a year?
Brian C. (Guest)
on 2008-10-27 16:18
Pål Bergström wrote:
> How do I get number of days in a year?

Enter "number of days in a year" into Google and you'll get some useful
results.

The very first hit gives you
http://en.wikipedia.org/wiki/Leap_year#Algorithm

Google is your friend.
Hélio Rocha (Guest)
on 2008-10-27 16:37
(Received via mailing list)
Just check if the year is leap. In google u'll find something like:

int __isleap <javascript:searchRef('__isleap')>(int
year<javascript:searchRef('year')>)
{
  /* every fourth year is a leap year except for century years that are
   * not divisible by 400. */
/*  return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); */
  return (!(year <javascript:searchRef('year')>%4) &&
((year<javascript:searchRef('year')>%100)
|| !(year <javascript:searchRef('year')>%400)));
}
Todd B. (Guest)
on 2008-10-27 16:46
(Received via mailing list)
On Mon, Oct 27, 2008 at 9:12 AM, Pål Bergström 
<removed_email_address@domain.invalid> wrote:
> How do I get number of days in a year?

You can use the date library...

require 'date'; puts Date.new(2005) - Date.new(2004)
=> 366

Todd
Paul B. (Guest)
on 2008-10-27 19:20
Todd B. wrote:
> On Mon, Oct 27, 2008 at 9:12 AM, P�l Bergstr�m 
<removed_email_address@domain.invalid> wrote:
>> How do I get number of days in a year?
>
> You can use the date library...
>
> require 'date'; puts Date.new(2005) - Date.new(2004)
> => 366
>
> Todd

So there's nothing like this (Rails)

Time.days_in_month()
Michael G. (Guest)
on 2008-10-27 19:31
(Received via mailing list)
On Mon, Oct 27, 2008 at 1:20 PM, Pål Bergström 
<removed_email_address@domain.invalid>
wrote:
>
> So there's nothing like this (Rails)
>
> Time.days_in_month()

You could just "require 'activesupport'"

or look at the code in activesupport...

condensed for your immediate use!

class Time
  COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31,
30, 31, 30, 31]
  class << self
    # Return the number of days in the given month.
    # If no year is specified, it will use the current year.
    def days_in_month(month, year = now.year)
      return 29 if month == 2 && ::Date.gregorian_leap?(year)
      COMMON_YEAR_DAYS_IN_MONTH[month]
    end
  end
end
Paul B. (Guest)
on 2008-10-28 11:33
A stupid question in the first place. I've done like this; I check if
Feb in a particular year has 29 days or not, so it gives 365 or 366
days. Simple. :-)
Brian C. (Guest)
on 2008-10-28 12:19
Pål Bergström wrote:
> A stupid question in the first place. I've done like this; I check if
> Feb in a particular year has 29 days or not, so it gives 365 or 366
> days. Simple. :-)

And notice how days_in_month is implemented in activesupport:

          def days_in_month(month, year = now.year)
            return 29 if month == 2 && ::Date.gregorian_leap?(year)
            COMMON_YEAR_DAYS_IN_MONTH[month]
          end

So all you need is:

irb(main):001:0> require 'date'
=> true
irb(main):002:0> Date.gregorian_leap?(2008) ? 366 : 365
=> 366
irb(main):003:0> Date.gregorian_leap?(2009) ? 366 : 365
=> 365
Jan F. (Guest)
on 2008-10-28 14:26
(Received via mailing list)
Brian C. <removed_email_address@domain.invalid> wrote:
> So all you need is:
>
> irb(main):001:0> require 'date'
> => true
> irb(main):002:0> Date.gregorian_leap?(2008) ? 366 : 365 => 366
> irb(main):003:0> Date.gregorian_leap?(2009) ? 366 : 365 => 365

Or ask the day of year of December 31st:

require 'date'
DateTime.new(2008, 12, 31).yday # => 366
DateTime.new(2009, 12, 31).yday # => 366

Best regards,
Jan F.
Jan F. (Guest)
on 2008-10-28 14:30
(Received via mailing list)
Brian C. <removed_email_address@domain.invalid> wrote:
> So all you need is:
>
> irb(main):001:0> require 'date'
> => true
> irb(main):002:0> Date.gregorian_leap?(2008) ? 366 : 365 => 366
> irb(main):003:0> Date.gregorian_leap?(2009) ? 366 : 365 => 365

Or ask the day of year of December 31st:

require 'date'

DateTime.new(2008, 12, 31).yday # => 366

DateTime.new(2009, 12, 31).yday # => 365

Best regards,
Jan F.
Paul B. (Guest)
on 2008-10-28 17:45
Jan F. wrote:
> Brian C. <removed_email_address@domain.invalid> wrote:

> Or ask the day of year of December 31st:
>
> require 'date'
>
> DateTime.new(2008, 12, 31).yday # => 366
>
> DateTime.new(2009, 12, 31).yday # => 365
>
> Best regards,
> Jan F.

That was smart. Thanks!
This topic is locked and can not be replied to.