[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Convert day of year to month, day

Rob Redmon

4/4/2008 2:39:00 PM

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
--
Posted via http://www.ruby-....

7 Answers

James Gray

4/4/2008 3:01:00 PM

0

On Apr 4, 2008, at 9:38 AM, Rob Redmon wrote:

> 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?

>> require "date"
=> true
>> Date.strptime("2008-095", "%Y-%j").to_s
=> "2008-04-04"

James Edward Gray II

ara.t.howard

4/4/2008 3:15:00 PM

0


On Apr 4, 2008, at 8:38 AM, Rob Redmon wrote:
> 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


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://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Rob Redmon

4/4/2008 3:20:00 PM

0

James Gray wrote:
> On Apr 4, 2008, at 9:38 AM, Rob Redmon wrote:
>
>> 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?
>
> >> require "date"
> => true
> >> Date.strptime("2008-095", "%Y-%j").to_s
> => "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
--
Posted via http://www.ruby-....

Rob Redmon

4/4/2008 3:24:00 PM

0

ara.t.howard wrote:
> On Apr 4, 2008, at 8:38 AM, Rob Redmon wrote:
>> 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
>
>
> 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://codeforp...


Wicked, thanks Ara!

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

Rob
--
Posted via http://www.ruby-....

ara.t.howard

4/4/2008 3:26:00 PM

0


On Apr 4, 2008, at 9:20 AM, Rob Redmon wrote:
> 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://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




ara.t.howard

4/4/2008 3:32:00 PM

0


On Apr 4, 2008, at 9:24 AM, Rob Redmon wrote:
> Caution to the wind; I'm building my own 1.8.6 to experiment with.

put it on NFS - then just use it everywhere - bugger sysadmins

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Alex Wayne

4/4/2008 4:32:00 PM

0

James Gray wrote:
> How's this?
>
> >> require "date"
> => true
> >> Date.strptime("2008-095", "%Y-%j").to_s
> => "2008-04-04"
>
> James Edward Gray II

Now that is an awesome little method I didn't know about. Thanks James.
--
Posted via http://www.ruby-....