Days in a year

How do I get number of days in a year?

···

--
Posted via http://www.ruby-forum.com/.

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

Google is your friend.

···

--
Posted via http://www.ruby-forum.com/\.

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)));

}

···

On Mon, Oct 27, 2008 at 2:12 PM, Pål Bergström <pal@palbergstrom.com> wrote:

How do I get number of days in a year?
--
Posted via http://www.ruby-forum.com/\.

You can use the date library...

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

Todd

···

On Mon, Oct 27, 2008 at 9:12 AM, Pål Bergström <pal@palbergstrom.com> wrote:

How do I get number of days in a year?

Todd Benson wrote:

···

On Mon, Oct 27, 2008 at 9:12 AM, P�l Bergstr�m <pal@palbergstrom.com> 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()
--
Posted via http://www.ruby-forum.com/\.

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

···

On Mon, Oct 27, 2008 at 1:20 PM, Pål Bergström <pal@palbergstrom.com> wrote:

Todd Benson wrote:

On Mon, Oct 27, 2008 at 9:12 AM, P�l Bergstr�m <pal@palbergstrom.com> 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()

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. :slight_smile:

···

--
Posted via http://www.ruby-forum.com/.

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. :slight_smile:

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

···

--
Posted via http://www.ruby-forum.com/\.

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 Friedrich

···

Brian Candler <b.candler@pobox.com> 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 Friedrich

···

Brian Candler <b.candler@pobox.com> 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

Jan Friedrich wrote:

···

Brian Candler <b.candler@pobox.com> 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 Friedrich

That was smart. Thanks!
--
Posted via http://www.ruby-forum.com/\.