jira.codehaus.org

  • Log In Access more options
    • Online Help
    • Keyboard Shortcuts
    • About JIRA
    • JIRA Credits
    • What?s New
  • Dashboards Access more options (Alt+d)
  • Projects Access more options (Alt+p)
  • Issues Access more options (Alt+i)
  • JRuby
  • JRUBY-5255

Date#strftime fails to honor "%3N" format

  • Log In
  • Views
    • XML
    • Word
    • Printable

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Trivial Trivial
  • Resolution: Fixed
  • Affects Version/s: JRuby 1.5.5
  • Fix Version/s: JRuby 1.6RC1
  • Component/s: Ruby 1.8.7
  • Labels:
    None
  • Testcase included:
    yes

Description

jirb in 1.8 mode

irb(main):003:0> require 'date'
=> true
irb(main):004:0> x=DateTime.now()
=> #<DateTime: 21215812251739/8640000,11/48,2299161>

%3N the length for N is ignored

irb(main):005:0> x.strftime("%Y%m%d %H:%M:%S.%3N")
=> "20101203 13:18:37.390000000"
irb(main):006:0> x.strftime("%Y%m%d %H:%M:%S.%N")
=> "20101203 13:18:37.390000000"
irb(main):007:0> x=Time.now()
=> Fri Dec 03 13:20:22 +0530 2010
irb(main):008:0> x.strftime("%Y%m%d %H:%M:%S.%N")
=> "20101203 13:20:22.944915345"

%3N not recognised for time objcet but %N is recognised as above

irb(main):009:0> x.strftime("%Y%m%d %H:%M:%S.%3N")
=> "20101203 13:20:22.%3N"

The default db format string is not used and does not have the fraction part of the second string %N in it

the datetime object insertion looses the fraction part of the second
for insert stmt in mssql the following format needs the %3N to work and

   def quote(value, column = nil)
      return value.quoted_id if value.respond_to?(:quoted_id)

      case value
      when String, ActiveSupport::Multibyte::Chars
        value = value.to_s
        if column && column.type == :binary
          "'#{quote_string(JdbcSpec::MsSQL::Column.string_to_binary(value))}'" # ' (for ruby-mode)
        elsif column && [:integer, :float].include?(column.type)
          value = column.type == :integer ? value.to_i : value.to_f
          value.to_s
        else
          "'#{quote_string(value)}'" # ' (for ruby-mode)
        end
      when TrueClass             then '1'
      when FalseClass            then '0'
      when Time, DateTime        then "'#{value.strftime("%Y%m%d %H:%M:%S.%3N")[0,21]}'"
      when Date                  then "'#{value.strftime("%Y%m%d")}'"
      else                       super
      end
    end

Issue Links

relates to

Bug - A problem which impairs or prevents the functions of the product. JRUBY-3871 strftime doesn't support %h

  • Major - Major loss of function.
  • Closed - The issue is considered finished, the resolution is correct. Issues which are not closed can be reopened.

Activity

Ascending order - Click to sort in descending order
  • All
  • Comments
  • Work Log
  • History
  • Activity
Hide
Permalink
Hiro Asari added a comment - 07/Jan/11 12:27 AM

I reformatted the description. I assume that the last "and" before the code snippet was unintentionally left behind.

Show
Hiro Asari added a comment - 07/Jan/11 12:27 AM I reformatted the description. I assume that the last "and" before the code snippet was unintentionally left behind.
Hide
Permalink
Hiro Asari added a comment - 07/Jan/11 12:44 AM

I cannot reproduce this.

$ jruby -S irb
jruby-1.5.5 :001 > RUBY_DESCRIPTION
 => "jruby 1.5.5 (ruby 1.8.7 patchlevel 249) (2010-11-10 4bd4200) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_22) [x86_64-java]" 
jruby-1.5.5 :002 > require 'date'
 => true 
jruby-1.5.5 :003 > x=DateTime.now
 => #<DateTime: 7857820079669/3200000,-5/24,2299161> 
jruby-1.5.5 :004 > x.strftime("%Y%m%d %H:%M:%S.%3N")
 => "20110107 01:35:51.063000000" 

I've checked some older versions of JRuby, including 1.2.0 and 1.5.1, as well as the trunk, but none of them exhibited the behavior described in this ticket.

Show
Hiro Asari added a comment - 07/Jan/11 12:44 AM I cannot reproduce this.
$ jruby -S irb
jruby-1.5.5 :001 > RUBY_DESCRIPTION
 => "jruby 1.5.5 (ruby 1.8.7 patchlevel 249) (2010-11-10 4bd4200) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_22) [x86_64-java]" 
jruby-1.5.5 :002 > require 'date'
 => true 
jruby-1.5.5 :003 > x=DateTime.now
 => #<DateTime: 7857820079669/3200000,-5/24,2299161> 
jruby-1.5.5 :004 > x.strftime("%Y%m%d %H:%M:%S.%3N")
 => "20110107 01:35:51.063000000" 
I've checked some older versions of JRuby, including 1.2.0 and 1.5.1, as well as the trunk, but none of them exhibited the behavior described in this ticket.
Hide
Permalink
Hiro Asari added a comment - 07/Jan/11 12:48 AM

OK, I take that back. It is not DateTime, but rather Time object that exhibits the bad behavior.

Show
Hiro Asari added a comment - 07/Jan/11 12:48 AM OK, I take that back. It is not DateTime, but rather Time object that exhibits the bad behavior.
Hide
Permalink
Geeta added a comment - 07/Jan/11 1:05 AM

ok. thanks.

Show
Geeta added a comment - 07/Jan/11 1:05 AM ok. thanks.
Hide
Permalink
Hiro Asari added a comment - 07/Jan/11 8:20 AM

OK, it's coming back to me now.

"%3N" is a newer addition to MRI 1.9, and we haven't implemented it yet. See JRUBY-3871.

Show
Hiro Asari added a comment - 07/Jan/11 8:20 AM OK, it's coming back to me now. "%3N" is a newer addition to MRI 1.9, and we haven't implemented it yet. See JRUBY-3871.
Hide
Permalink
Hiro Asari added a comment - 07/Jan/11 8:23 AM

Well, it's partially implemented.

$ jruby -v --1.9 -e 'p Time.now.strftime("%3N")'
jruby 1.6.0.dev (ruby 1.9.2 trunk 136) (2011-01-07 0277975) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_22) [darwin-x86_64-java]
"164148000"
Show
Hiro Asari added a comment - 07/Jan/11 8:23 AM Well, it's partially implemented.
$ jruby -v --1.9 -e 'p Time.now.strftime("%3N")'
jruby 1.6.0.dev (ruby 1.9.2 trunk 136) (2011-01-07 0277975) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_22) [darwin-x86_64-java]
"164148000"
Hide
Permalink
Hiro Asari added a comment - 09/Jan/11 10:35 PM

Pushed the fix in the master. Note that JVM does not guarantee time precision below milliseconds, so it doesn't make much sense to pass anything greater than 3. You can do it, but the 4th decimal digit onward are all 0.

[system]:~ $ jruby -v --1.9 -e 'p Time.now.strftime("%N")'
jruby 1.6.0.RC1 (ruby 1.9.2 trunk 136) (2011-01-09 2f483be) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_22) [darwin-x86_64-java]
"964"
[system]:~ $ jruby -v --1.9 -e 'p Time.now.strftime("%3N")'
jruby 1.6.0.RC1 (ruby 1.9.2 trunk 136) (2011-01-09 2f483be) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_22) [darwin-x86_64-java]
"817"
[system]:~ $ jruby -v --1.9 -e 'p Time.now.strftime("%6N")'
jruby 1.6.0.RC1 (ruby 1.9.2 trunk 136) (2011-01-09 2f483be) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_22) [darwin-x86_64-java]
"827000"
[system]:~ $ jruby -v --1.9 -e 'p Time.now.strftime("%9N")'
jruby 1.6.0.RC1 (ruby 1.9.2 trunk 136) (2011-01-09 2f483be) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_22) [darwin-x86_64-java]
"712000000"
[system]:~ $ jruby -v --1.9 -e 'p Time.now.strftime("%12N")'
jruby 1.6.0.RC1 (ruby 1.9.2 trunk 136) (2011-01-09 2f483be) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_22) [darwin-x86_64-java]
"829000000000"
Show
Hiro Asari added a comment - 09/Jan/11 10:35 PM Pushed the fix in the master. Note that JVM does not guarantee time precision below milliseconds, so it doesn't make much sense to pass anything greater than 3. You can do it, but the 4th decimal digit onward are all 0.
[system]:~ $ jruby -v --1.9 -e 'p Time.now.strftime("%N")'
jruby 1.6.0.RC1 (ruby 1.9.2 trunk 136) (2011-01-09 2f483be) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_22) [darwin-x86_64-java]
"964"
[system]:~ $ jruby -v --1.9 -e 'p Time.now.strftime("%3N")'
jruby 1.6.0.RC1 (ruby 1.9.2 trunk 136) (2011-01-09 2f483be) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_22) [darwin-x86_64-java]
"817"
[system]:~ $ jruby -v --1.9 -e 'p Time.now.strftime("%6N")'
jruby 1.6.0.RC1 (ruby 1.9.2 trunk 136) (2011-01-09 2f483be) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_22) [darwin-x86_64-java]
"827000"
[system]:~ $ jruby -v --1.9 -e 'p Time.now.strftime("%9N")'
jruby 1.6.0.RC1 (ruby 1.9.2 trunk 136) (2011-01-09 2f483be) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_22) [darwin-x86_64-java]
"712000000"
[system]:~ $ jruby -v --1.9 -e 'p Time.now.strftime("%12N")'
jruby 1.6.0.RC1 (ruby 1.9.2 trunk 136) (2011-01-09 2f483be) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_22) [darwin-x86_64-java]
"829000000000"
Hide
Permalink
Geeta added a comment - 10/Jan/11 1:05 AM

Hi,
Thanks.
I need this fix in jruby 1.8 as well.
I am using jruby with active record and sql server as database.

The jdbc_adapter (jdbc_mssql.rb in directory C:\JRuby\jruby-1.2.0\1.2.0\lib\ruby\gems\1.8\gems\activerecord-jdbc-adapter-0.9\lib\jdbc_adapter) is not supporting DateTime format "%Y%m%d %H:%M:%S.%3N" whereas Time and Date format are supported.
when DateTime then "'#{value.strftime("%Y%m%d %H:%M:%S.%3N")}'"
when Time then "'#{value.strftime("%Y%m%d %H:%M:%S")}'"
when Date then "'#{value.strftime("%Y%m%d")}
So currently we have workaround as format
when DateTime then "'#{value.strftime("%Y%m%d %H:%M:%S.%3N")[0,21]}'"

Show
Geeta added a comment - 10/Jan/11 1:05 AM Hi, Thanks. I need this fix in jruby 1.8 as well. I am using jruby with active record and sql server as database. The jdbc_adapter (jdbc_mssql.rb in directory C:\JRuby\jruby-1.2.0\1.2.0\lib\ruby\gems\1.8\gems\activerecord-jdbc-adapter-0.9\lib\jdbc_adapter) is not supporting DateTime format "%Y%m%d %H:%M:%S.%3N" whereas Time and Date format are supported. when DateTime then "'#{value.strftime("%Y%m%d %H:%M:%S.%3N")}'" when Time then "'#{value.strftime("%Y%m%d %H:%M:%S")}'" when Date then "'#{value.strftime("%Y%m%d")} So currently we have workaround as format when DateTime then "'#{value.strftime("%Y%m%d %H:%M:%S.%3N")[0,21]}'"
Hide
Permalink
Hiro Asari added a comment - 10/Jan/11 1:19 AM

I believe this is fixed in 1.8 mode.

[system]:~ $ jruby -v -rdate -e 'p DateTime.now.strftime("%Y%m%d %H:%M:%S.%3N")'
jruby 1.6.0.RC1 (ruby 1.8.7 patchlevel 330) (2011-01-09 2f483be) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_22) [darwin-x86_64-java]
"20110110 02:12:13.345000000"

Time#strftime doesn't support %3N in MRI, either, and JRuby's behavior matches it (except that JRuby currently prints %, which I'll take a look later).

[system]:~ $ jruby -v -e 'p Time.now.strftime("%Y%m%d %H:%M:%S.%3N")'
jruby 1.6.0.RC1 (ruby 1.8.7 patchlevel 330) (2011-01-09 2f483be) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_22) [darwin-x86_64-java]
"20110110 02:14:51.%3N"
[system]:~ $ ruby -v -e 'p Time.now.strftime("%Y%m%d %H:%M:%S.%3N")'
ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
"20110110 02:15:30.3N"
Show
Hiro Asari added a comment - 10/Jan/11 1:19 AM I believe this is fixed in 1.8 mode.
[system]:~ $ jruby -v -rdate -e 'p DateTime.now.strftime("%Y%m%d %H:%M:%S.%3N")'
jruby 1.6.0.RC1 (ruby 1.8.7 patchlevel 330) (2011-01-09 2f483be) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_22) [darwin-x86_64-java]
"20110110 02:12:13.345000000"
Time#strftime doesn't support %3N in MRI, either, and JRuby's behavior matches it (except that JRuby currently prints %, which I'll take a look later).
[system]:~ $ jruby -v -e 'p Time.now.strftime("%Y%m%d %H:%M:%S.%3N")'
jruby 1.6.0.RC1 (ruby 1.8.7 patchlevel 330) (2011-01-09 2f483be) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_22) [darwin-x86_64-java]
"20110110 02:14:51.%3N"
[system]:~ $ ruby -v -e 'p Time.now.strftime("%Y%m%d %H:%M:%S.%3N")'
ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
"20110110 02:15:30.3N"

People

  • Assignee:
    Hiro Asari
    Reporter:
    Geeta
Vote (0)
Watch (0)

Dates

  • Created:
    12/Dec/10 11:00 PM
    Updated:
    10/Jan/11 1:19 AM
    Resolved:
    10/Jan/11 1:19 AM
  • Atlassian JIRA (v5.0.4#731-sha1:3aa7374)
  • Report a problem
  • Powered by a free Atlassian JIRA open source license for Codehaus. Try JIRA - bug tracking software for your team.