Convert day of year to month, day

R

Rob Redmon

Hi,

What's the "ruby way" to go from a date/time format that is "Year
Day_of_Year time" to "Year Month Day". E.g. I want to convert
year=2008, day_of_year=095, frequently represented as "2008-095" to it's
more usual format "2008-04-04". I've been playing with the Time class
and it's methods and found nifty ways to get yday, e.g.
Time.now.utc.yday, but how do I easily go backwards w/o writing my own
simple converter?

R
 
J

James Gray

What's the "ruby way" to go from a date/time format that is "Year
Day_of_Year time" to "Year Month Day". E.g. I want to convert
year=2008, day_of_year=095, frequently represented as "2008-095" to
it's
more usual format "2008-04-04". I've been playing with the Time class
and it's methods and found nifty ways to get yday, e.g.
Time.now.utc.yday, but how do I easily go backwards w/o writing my own
simple converter?

How's this?
=> "2008-04-04"

James Edward Gray II
 
A

ara.t.howard

Hi,

What's the "ruby way" to go from a date/time format that is "Year
Day_of_Year time" to "Year Month Day". E.g. I want to convert
year=2008, day_of_year=095, frequently represented as "2008-095" to
it's
more usual format "2008-04-04". I've been playing with the Time class
and it's methods and found nifty ways to get yday, e.g.
Time.now.utc.yday, but how do I easily go backwards w/o writing my own
simple converter?


hi rob-

james example is good - you might find this method quite useful with
julian days to:

p Date.ordinal(2008,1).ctime #=> "Tue Jan 1 00:00:00 2008"

cheers

a @ http://codeforpeople.com/
 
R

Rob Redmon

James said:
How's this?

=> "2008-04-04"

James Edward Gray II

Good idea, but doesn't work for me:
irb(main):065:0> Date.strptime("2008-095", "%Y-%J").to_s
ArgumentError: invalid date
from /usr/lib/ruby/1.8/date.rb:650:in `new_with_hash'
from /usr/lib/ruby/1.8/date.rb:675:in `strptime'
from (irb):65
from :0

Maybe my version of Ruby (1.8.1) is too old. The computer in question
is managed by the network pirates and unlikely to be upgraded. This is
a noob thought, but is there a way for me to locally upgrade the Date
class, ala a Ruby Gem?

R
 
R

Rob Redmon

ara.t.howard said:
hi rob-

james example is good - you might find this method quite useful with
julian days to:

p Date.ordinal(2008,1).ctime #=> "Tue Jan 1 00:00:00 2008"

cheers

a @ http://codeforpeople.com/


Wicked, thanks Ara!

Caution to the wind; I'm building my own 1.8.6 to experiment with.

Rob
 
A

ara.t.howard

Maybe my version of Ruby (1.8.1) is too old. The computer in question
is managed by the network pirates and unlikely to be upgraded. This
is
a noob thought, but is there a way for me to locally upgrade the Date
class, ala a Ruby Gem?

yyyy, jjj, *ignored = yourdate.split '-'

date = Date.ordinal Integer(yyyy), Integer(jjj)

p date

a @ http://codeforpeople.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top