Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Time (leap year??)

Reply
Thread Tools

Time (leap year??)

 
 
csjasnoch@wisc.edu
Guest
Posts: n/a
 
      08-31-2005
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?

 
Reply With Quote
 
 
 
 
csjasnoch@wisc.edu
Guest
Posts: n/a
 
      08-31-2005
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 weekday hash generator. So I send the year
and month and get all weekdays for every day of the month.

 
Reply With Quote
 
 
 
 
David A. Black
Guest
Posts: n/a
 
      08-31-2005
Hi --

On Thu, 1 Sep 2005, 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



 
Reply With Quote
 
Kirk Haines
Guest
Posts: n/a
 
      08-31-2005
On Wednesday 31 August 2005 2:06 pm, 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


 
Reply With Quote
 
csjasnoch@wisc.edu
Guest
Posts: n/a
 
      08-31-2005
Thanks Kirk. That should get me to where I need.

 
Reply With Quote
 
Kirk Haines
Guest
Posts: n/a
 
      08-31-2005
On Wednesday 31 August 2005 2:26 pm, 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


 
Reply With Quote
 
Gavin Kistner
Guest
Posts: n/a
 
      09-01-2005
On Aug 31, 2005, at 2:11 PM, 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is time.time() < time.time() always true? flamesrock Python 8 11-24-2006 06:51 AM
Re: interpreting the fractional portion of time.clock() vs time.time(0measurements Peter Hansen Python 0 02-22-2006 02:02 PM
Re: interpreting the fractional portion of time.clock() vs time.time()measurements Peter Hansen Python 0 02-22-2006 12:03 AM
time.clock() or time.time() peterbe@gmail.com Python 8 08-05-2005 01:51 PM
delta time = time stop - time start engsol Python 2 01-26-2004 12:06 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57