For Programmers: Free Programming Magazines  


Home > Archive > Ruby > August 2005 > Time (leap year??)









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Time (leap year??)
csjasnoch@wisc.edu

2005-08-31, 7:00 pm

Is there a simple way to see if a year is a leap year (rather than
writting an algorithm I wanted to check if it is already implimented
first)

It would be kewl if there was just a check on a Time var


Like,

now = Time.new
flag = now.leapyear?

csjasnoch@wisc.edu

2005-08-31, 7:00 pm

I know I could do somethine like this (and it works for future years)


now = Time.new

leap = now.year%4

if(learp == 0)
flag = true



#############

But if I were to use mktime and involved past dates I would have to be
weary of the millenias.

How does it work
Every 4 years, every hundred but not every millenia.. Or is it also not
every 100?

FYI I am trying to make a wday hash generator. So I send the year
and month and get all wdays for every day of the month.

David A. Black

2005-08-31, 7:00 pm

Hi --

On Thu, 1 Sep 2005, csjasnoch@wisc.edu wrote:

> Is there a simple way to see if a year is a leap year (rather than
> writting an algorithm I wanted to check if it is already implimented
> first)
>
> It would be kewl if there was just a check on a Time var
>
>
> Like,
>
> now = Time.new
> flag = now.leapyear?


Yes:

irb(main):023:0> Date.today.leap?
=> false
irb(main):024:0> (Date.today << 12).leap?
=> true


David

--
David A. Black
dblack@wobblini.net


Michael Kelly

2005-08-31, 7:00 pm

Try:

now =3D DateTime.now
flag =3D Date.leap?( now.year )

--=20
Michael Kelly=20
Sr. Software Engineer=20
Eleven Wireless Inc. - The Possibilities are Wireless=20
http://www.elevenwireless.com=20



-----Original Message-----
From: csjasnoch@wisc.edu [mailto:csjasnoch@wisc.edu]=20
Sent: Wednesday, August 31, 2005 1:06 PM
To: ruby-talk ML
Subject: Time (leap year??)


Is there a simple way to see if a year is a leap year (rather than
writting an algorithm I wanted to check if it is already implimented
first)

It would be kewl if there was just a check on a Time var


Like,

now =3D Time.new
flag =3D now.leapyear?




csjasnoch@wisc.edu

2005-08-31, 7:00 pm

Wow that was fast:)
Thanx

Kirk Haines

2005-08-31, 7:00 pm

On Wednesday 31 August 2005 2:06 pm, csjasnoch@wisc.edu wrote:
> Is there a simple way to see if a year is a leap year (rather than
> writting an algorithm I wanted to check if it is already implimented
> first)
>
> It would be kewl if there was just a check on a Time var


irb(main):001:0> require 'date'
=> true
irb(main):002:0> d = Date.parse(Time.now.to_s)
=> #<Date: 4907227/2,0,2299161>
irb(main):003:0> d.to_s
=> "2005-08-31"
irb(main):004:0> d.leap?
=> false


Kirk Haines


csjasnoch@wisc.edu

2005-08-31, 7:00 pm

Where could I find descriptions of the DateTime class?

I only really know about the Time class....

Does it have all of the same members and methods?

csjasnoch@wisc.edu

2005-08-31, 7:00 pm

Thanks Kirk. That should get me to where I need.

Michael Kelly

2005-08-31, 7:00 pm

I usually go here: http://www.ruby-doc.org/stdlib/.

Look in the Date package.

-=3Dmichael=3D-

--=20
Michael Kelly=20
Sr. Software Engineer=20
Eleven Wireless Inc. - The Possibilities are Wireless=20
http://www.elevenwireless.com=20



-----Original Message-----
From: csjasnoch@wisc.edu [mailto:csjasnoch@wisc.edu]=20
Sent: Wednesday, August 31, 2005 1:21 PM
To: ruby-talk ML
Subject: Re: Time (leap year??)


Where could I find descriptions of the DateTime class?

I only really know about the Time class....

Does it have all of the same members and methods?




Levin Alexander

2005-08-31, 7:00 pm

csjasnoch@wisc.edu <csjasnoch@wisc.edu> wrote:
> Where could I find descriptions of the DateTime class?


<http://www.ruby-doc.org/core/classes/Date.html>

The sidebar from ruby-doc.org are really helpful to quickly look up
class definitions: <http://www.ruby-doc.org/docbar/>

-Levin


Kirk Haines

2005-08-31, 7:00 pm

On Wednesday 31 August 2005 2:26 pm, csjasnoch@wisc.edu wrote:
> Thanks Kirk. That should get me to where I need.


As you saw, there are more concise ways to do a check for today, but that
general pattern will work for any date that you need to parse. If you are
going from Time to Date a lot, you might want to define something like this:

class Time
def to_date
Date.new(year,month,day)
end
end

Take a look at the difference between Date/DateTime and Time classes. They
are very different. Date/time is stored in a completely different format,
and for the most part they offer completely different sets of methods.


Kirk Haines


Gavin Kistner

2005-08-31, 9:56 pm

On Aug 31, 2005, at 2:11 PM, csjasnoch@wisc.edu wrote:
> How does it work
> Every 4 years, every hundred but not every millenia.. Or is it also
> not
> every 100?


"Every year divisible by 4 is a leap year.
But every year divisible by 100 is NOT a leap year
Unless the year is also divisible by 400, then it is still a leap year."
-- http://www.timeanddate.com/date/leapyear.html

So to calculate it on your own:
irb(main):001:0> def is_leap?( year ); year % 4 == 0 && ( year % 100 !
= 0 || year % 400 == 0 ); end
=> nil
irb(main):002:0> is_leap? 1996
=> true
irb(main):003:0> is_leap? 1997
=> false
irb(main):004:0> is_leap? 2000
=> true
irb(main):005:0> is_leap? 2100
=> false
irb(main):006:0> is_leap? 2300
=> false
irb(main):007:0> is_leap? 2400
=> true



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com