Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange output when using #stamp("29/01/2013") #21

Closed
ghiculescu opened this issue Jan 29, 2013 · 10 comments
Closed

Strange output when using #stamp("29/01/2013") #21

ghiculescu opened this issue Jan 29, 2013 · 10 comments

Comments

@ghiculescu
Copy link

Hi. I'm calling #stamp with today's date as the parameter, and getting some weird output. It seems to work correctly when 31/12 is used as the stamp parameter, as suggested in the readme. That said, I thought I'd report it because the output - "28/13/2013" - doesn't contain any valid months, so I'm not sure how it came about.

1.9.3p286 :015 > d = Date.new(2013, 01, 28)
 => Mon, 28 Jan 2013 
 1.9.3p286 :017 > d.stamp "29/01/2013"
 => "28/13/2013" 
1.9.3p286 :016 > d.stamp "31/12/2013"
 => "28/01/2013" 

I'm using stamp 0.5.0, rails 3.2.11, and ruby 1.9.3.

@jeremyw
Copy link
Owner

jeremyw commented Feb 3, 2013

Stamp is outputting the 2-digit year, which is 13. I've been thinking about a couple ways to handle cases like this:

  1. Disallow ambiguous examples. Stamp would raise an exception if an ambiguous value is encountered.
  2. Implement a smarter language parser. Maybe a two-pass algorithm that would determine, on the second pass in this example, that '2013' is the year, so '01' must be the month.

Any thoughts?

@ghiculescu
Copy link
Author

Ahhh, I understand. So it seems that the issue came about here:

TWO_DIGIT_DATE_SUCCESSION = {
  :month => TWO_DIGIT_DAY_EMITTER,
  :day   => TWO_DIGIT_YEAR_EMITTER,
  :year  => TWO_DIGIT_MONTH_EMITTER
}

The first part was a day, so Stamp assumed the second part would be a year because it couldn't find any definite matches (right?). Maybe there's some room for improvement here; eg. if the day (previous_part) was two digits, and the current part is two digits, then expect the current part to be a month (unless it's obviously not, eg. if it's greater than 12). Just thinking out loud.

If nothing else, option 1 would work. I didn't realise I was giving Stamp ambiguous values until I went back over the docs.

@ghiculescu
Copy link
Author

Another example I just noticed:

1.9.3p286 :048 > Date.today.stamp '18/06/2012'
=> "04/13/2013"  # incorrect
1.9.3p286 :049 > Date.today.stamp '18/12/2012'
 => "04/02/2013"  # correct
1.9.3p286 :050 > Date.today.stamp '31/12/2012'
 => "04/02/2013"  # correct

@amencarini
Copy link

@jeremyw I'd personally prefer solution 1 :)

By the way I ended up here because I had the same problem as @ghiculescu

> Date.today.stamp("30-03-2013")
=> "30-13-2013"

@jeremyw
Copy link
Owner

jeremyw commented Mar 30, 2013

I think I prefer solution 1 as well but may not have time to tackle it soon. I'd love to see a pull request!

@amencarini
Copy link

I'm looking into it, but I think we should agree on what we would consider ambiguous.
Raising an exception during translation instead of using TWO_DIGIT_DATE_SUCCESSION[previous_part] could be an easy way to prevent any ambiguous pattern but that would also kill legitimate ones currently working like "Jan 12, 1999".

@amencarini
Copy link

By the way, I found a (bad) way to overcome this issue.

I've changed Stamp::Emitters::TwoDigit field from an attr_reader to an attr_accessor and then:

# emitters/composite.rb
result = ''
year_emitters = @emitters.select{|e| e.field == :year}
year_emitters.first.field = :month if year_emitters.size > 1
@emitters.each { |e| result << e.format(target).to_s }
result

Implementing some mechanism similar to TWO_DIGIT_DATE_SUCCESSION should two fields refer to the same date part would certainly help. By the way @jeremyw, with the above change all tests are green, but I'm not sure you want me to pull request something like that :)

@tomhicks
Copy link

Hi,

Just jumping in here because I had this idea a while back and just got around to looking it up on github to see if anyone had already done this. My idea was that you just learn a single date where all the parts are unambiguous, and I think the easiest to remember is Saturday 1st February 2003 16:05:06. Learning that one date seems easier to me than learning all the crazy date part constants crap.

Thoughts on the single date idea?

Also, as I'm not a Ruby guy I was thinking of implementing this in PHP and JS. Not really sure what the github protocol would be on that? Separate repos connected only by name?

@jeremyw
Copy link
Owner

jeremyw commented Sep 2, 2013

@tomhicks I'm against the "golden date" idea just because you have to remember something specific, like the strftime directives. Don't let that dissuade you from trying to implement it, though. Just my opinion. :)

@jeremyw
Copy link
Owner

jeremyw commented Aug 21, 2014

This issue is resolved in version 0.6.0. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants