[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Is there a decent canonical way to get total days in a month from say Time class?

Xeno Campanoli

6/11/2009 1:03:00 AM

I suppose I could flip through local until I generate an exception, but somehow
I would think there would be a set of class methods to get:

daysofmonth = Time.daysofmonth(y,m)
daysofyear = Time.daysofyear(y)
leapday = Time.leapday?(y)
leapseconds = Time.leapseconds?(y)

??
xc

4 Answers

Xeno Campanoli

6/11/2009 1:19:00 AM

0

Xeno Campanoli wrote:
> I suppose I could flip through local until I generate an exception, but
> somehow I would think there would be a set of class methods to get:

Here's a fairly simple sequence I found on the net using Date (attributions to
kelyar, jgwong, and timmorgan, whoever they are):

#!/usr/bin/ruby
#

require 'date'

d0 = Date.new(2009,6,1)
puts "trace d0: #{d0}"

d1 = d0 >> 1
puts "trace d1: #{d1}"

d2 = d1 - 1
puts "trace d2: #{d2}"

d3 = d2.day
puts "trace d3: #{d3}"
---snip---
the above are my own illustration of what the authors show in their blog posts.

>
> daysofmonth = Time.daysofmonth(y,m)
> daysofyear = Time.daysofyear(y)
> leapday = Time.leapday?(y)
> leapseconds = Time.leapseconds?(y)
>
> ??
> xc
>
>


botp

6/11/2009 2:12:00 AM

0

On Thu, Jun 11, 2009 at 9:19 AM, Xeno Campanoli<xeno.campanoli@gmail.com> w=
rote:
> Xeno Campanoli wrote:
>>
>> I suppose I could flip through local until I generate an exception, but
>> somehow I would think there would be a set of class methods to get:
>
> Here's a fairly simple sequence I found on the net using Date (attributio=
ns
> to kelyar, jgwong, and timmorgan, whoever they are):
>
> #!/usr/bin/ruby
> #
>
> require 'date'
>
> d0 =3D Date.new(2009,6,1)
> puts "trace d0: =A0#{d0}"

you could jump to end by using negative day

> d0 =3D Date.new(2009,12,-1)
=3D> #<Date: 2009-12-31 (4910393/2,0,2299161)>
> d0.day
=3D> 31

Xeno Campanoli

6/11/2009 3:03:00 AM

0

botp wrote:
> On Thu, Jun 11, 2009 at 9:19 AM, Xeno Campanoli<xeno.campanoli@gmail.com> wrote:
>> Xeno Campanoli wrote:
>>> I suppose I could flip through local until I generate an exception, but
>>> somehow I would think there would be a set of class methods to get:
>> Here's a fairly simple sequence I found on the net using Date (attributions
>> to kelyar, jgwong, and timmorgan, whoever they are):
>>
>> #!/usr/bin/ruby
>> #
>>
>> require 'date'
>>
>> d0 = Date.new(2009,6,1)
>> puts "trace d0: #{d0}"
>
> you could jump to end by using negative day
>
>> d0 = Date.new(2009,12,-1)
> => #<Date: 2009-12-31 (4910393/2,0,2299161)>
>> d0.day
> => 31
>
>
That is nice. Thanks.

Michael Kohl

6/11/2009 10:53:00 AM

0

On Thu, Jun 11, 2009 at 3:02 AM, Xeno Campanoli<xeno.campanoli@gmail.com> w=
rote:
> I suppose I could flip through local until I generate an exception, but
> somehow I would think there would be a set of class methods to get:
>
> =A0 =A0 =A0 =A0daysofmonth =3D Time.daysofmonth(y,m)

Do you want to get the maximum number of days for a given year/month
combination? Here you go:

def days_of_month(year=3DTime.now.year, m=3D1)
(Date.new(year,12,31)<<12-month).day
end

days_of_month(2004, 2) # =3D> 29
days_of_month(2003, 2) # =3D> 28

Michael

--=20
Blog: http://citiz...
Twitter: http://twitter.com/...