quarta-feira, 15 de dezembro de 2010

Days of week in ruby

0

Em português

I don't know why, nobody need it, I searched at the web, and I'm not found a solution for this, by the way, I implement my own solution.

You can see some explain about the method here (in Pt-Br): http://www.arlindo-correia.com/140101.html
But, I converted the javascript code at http://www.profcardy.com/calculadoras/aplicativos.php?calc=4 to ruby

the result is this monkey patch

class Time
  DAYS_OF_WEEK = [
    :saturday, :sunday, :monday, :tuesday, :wednesday, :thursday, :friday
  ]
  def day_of_week
    day = self.day
    month = self.month
    year = self.year
    if month == 1
      month = 13
      year -= 1
    end
    if month == 2
      month = 14
      year -= 1
    end
    phase1 = ((month + 1)*3)/5
    phase2 = (year/4)
    phase3 = (year/100)
    phase4 = (year/400)
    phase5 = day + (month*2)+phase1+year+phase2-phase3+phase4+2
    phase6 = phase5/7
    result = phase5-(phase6*7)
    DAYS_OF_WEEK[result]
  end
end

and some result by irb (I've used the rails console)
irb(main):001:0> t = Time.now
=> Wed Dec 15 22:05:00 -0200 2010
irb(main):002:0> t.day_of_week
=> :wednesday
irb(main):003:0> t.prev_month.day_of_week
=> :monday

If somebody know another way to do it, I'm listening

0 comentários:

 
Design by ThemeShift | Bloggerized by Lasantha - Free Blogger Templates | Best Web Hosting