change integer into date

Discussion in 'Ruby' started by Pen Ttt, Dec 31, 2010.

  1. Pen Ttt

    Pen Ttt Guest

    require 'date'
    biaozun=Date.new(1970,1,1)
    puts biaozun+1262222200/86400
    =>2009-12-31
    i have change 1262222200 into date,2009-12-31
    1.can i change 2009-12-31 into 1262222200?
    2.is there better way to change 1262222200 into date,2009-12-31

    --
    Posted via http://www.ruby-forum.com/.
     
    Pen Ttt, Dec 31, 2010
    #1
    1. Advertising

  2. Pen Ttt

    Nathan Clark Guest

    > require 'date'
    > =C2=A0biaozun=3DDate.new(1970,1,1)
    > =C2=A0puts biaozun+1262222200/86400
    > =3D>2009-12-31
    > i have change =C2=A01262222200 into date,2009-12-31
    > 1.can i change =C2=A02009-12-31 =C2=A0into =C2=A01262222200?
    > 2.is there better way to change =C2=A01262222200 into date,2009-12-31


    Methods for dealing with Unix times are in the Time class, rather than
    the Date class.

    irb> time =3D Time.at(1262222200)
    =3D> 2009-12-30 20:16:40 -0500
    irb> time.to_i
    =3D> 1262222200
    irb> time =3D Time.new(2010, 12, 31)
    =3D> 2010-12-31 00:00:00 -0500
    irb> time.to_i
    =3D> 1293771600

    Unfortunately, there doesn't seem to be any good way to convert from
    Date to Time, other than
    Time.new(date_object.to_s).
     
    Nathan Clark, Dec 31, 2010
    #2
    1. Advertising

  3. These might help?

    Date#jd gives you the Julian Day Number of a date,
    and the Julian Day Number of 1970-01-01 (Gregorian) is 2440588,
    so you can do things like the following.
    There is also the DateTime class, which I have not used until this post.
    "Date" also adds Time#to_date and Time#to_datetime, but I haven't tried those.

    IRB in Ruby 1.9.1

    require "date"
    biaozun = Date.new( 1970, 1, 1 ) #=> #<Date: 1970-01-01 ...>
    duration_secs = 1_262_222_200 #=> 1262222200
    duration_days_rational = duration_secs.quo( 86400 ) #=> (6311111/432)
    duration_days_integer = duration_secs.div( 86400 ) #=> 14609
    duration_days_integer2 = duration_secs / 86400 #=> 14609
    date2 = biaozun + duration_days_integer #=> #<Date: 2009-12-31 ...>
    exit
    unix_date_zero_jd = biaozun.jd #=> 2440588

    d2jd = date2.jd #=> 2455197
    dur2_secs = (d2jd - 2440588) * 86400 #=> 1262217600
    ut = Time.at( dur2_secs ) #=> 2009-12-31 00:00:00 +0000

    dt = DateTime.new( 2010, 12, 31, 13, 42 )
    #=> #<DateTime: 2010-12-31T13:42:00+00:00 ...>

    t = dt.to_time #=> 2010-12-31 13:42:00 +0000
    t2 = date2.to_time #=> 2009-12-31 00:00:00 +0000
     
    Colin Bartlett, Dec 31, 2010
    #3
  4. Pen Ttt

    Pen Ttt Guest

    in my irb,
    pt@pt:~$ ruby -v
    ruby 1.8.7 (2010-06-23 patchlevel 299) [i686-linux]
    pt@pt:~$ irb
    irb(main):001:0> time = Time.new(2010, 12, 31)
    ArgumentError: wrong number of arguments (3 for 0)
    from (irb):1:in `initialize'
    from (irb):1:in `new'
    from (irb):1
    from :0

    it's wrong for the expression: Time.new(2010, 12, 31)

    --
    Posted via http://www.ruby-forum.com/.
     
    Pen Ttt, Jan 1, 2011
    #4
  5. On Dec 31 2010, 5:42 pm, Pen Ttt <> wrote:
    > in my irb,
    > pt@pt:~$ ruby -v
    > ruby 1.8.7 (2010-06-23 patchlevel 299) [i686-linux]
    > pt@pt:~$ irb
    > irb(main):001:0> time = Time.new(2010, 12, 31)
    > ArgumentError: wrong number of arguments (3 for 0)
    >   from (irb):1:in `initialize'
    >   from (irb):1:in `new'
    >   from (irb):1
    >   from :0
    >


    Skyes-MacBook-Pro-15:perl sshaw$ ruby19 -ve'p Time.new 2011,1,1'
    ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.2.0]
    2011-01-01 00:00:00 -0800
    Skyes-MacBook-Pro-15:perl sshaw$ ruby -ve'p Time.local 2011,1,1'
    ruby 1.8.7 (2008-08-11 patchlevel 72) [universal-darwin10.0]
    Sat Jan 01 00:00:00 -0800 2011
     
    Skye Shaw!@#$, Jan 1, 2011
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    1
    Views:
    631
    Matthew Speed
    Nov 8, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,332
    Michael Borgwardt
    May 30, 2004
  3. Matt
    Replies:
    2
    Views:
    550
    Pete Becker
    Nov 8, 2003
  4. Matt
    Replies:
    3
    Views:
    747
    Richard Heathfield
    Nov 8, 2003
  5. Randy Kramer
    Replies:
    12
    Views:
    396
    Robert Klemme
    Oct 25, 2007
Loading...

Share This Page