Are there any chronic variations of the format options that he analyzes? (Ruby)

I need to say chronic, that the date format is day-month-year, is this possible? The data that I turn to chronic can also be words today / yesterday / 2 days ago.

Chronic now gives me 2 Dec 2010instead of 12 Feb 2010from12-02-2010

The only solution I can think of is to exchange the day and month before passing the string to the chronic one.

require 'chronic'   

puts "12-02-2010 = #{Chronic.parse('12-02-2010')}"  #should be 12 Feb 2010


puts "yesteday = #{Chronic.parse('yesterday')}" #working ok
puts "Today = #{Chronic.parse('today')}"        #working ok
+3
source share
2 answers

I found this question today, 20 months after its request. There seems to be a way to indicate an exchange of months and days. Just use the parameter :endian_precedence:

: endian_precedence (Array) - : [: middle,: little] - , "03/04/2011" . , :endian_precedence [:little, :middle]

:

Chronic.parse('12-02-2010').strftime('%d %b %Y')   #=> 02 Dec 2010 
Chronic.parse('12-02-2010', :endian_precedence => [:little, :median]).strftime('%d %b %Y') #=> 12 Feb 2010

, !

+15

. chronic.parse . strftime , .

puts Chronic.parse('today').strftime('%d %b %Y') #=> 23 Feb 2010

, - , . - , , .

: Chronic pre_normalize, .

""

puts Chronic.parse('12-02-2010').strftime('%d %b %Y') #=> 02 Dec 2010

module Chronic
  class << self
    alias chronic__pre_normalize pre_normalize  

    def pre_normalize(text)
      text = text.split(/[^\d]/).reverse.join("-") if text =~ /^\d{1,2}[^\d]\d{1,2}[^\d]\d{4}$/
      text = chronic__pre_normalize(text)
      return text
    end
  end
end
puts Chronic.parse('12-02-2010').strftime('%d %b %Y') #=> 12 Feb 2010
+3

Source: https://habr.com/ru/post/1734042/


All Articles