In French, hours are formatted as "8 h 40" and not as "8:40". But in an arrow's format string, _h_ displays directly the hours. Would it be possible to add a key (such as _!_) to force _h_ to be displayed normally ("hh !h mm")?
Interesting. This will be a little more than a quick fix, as I'll have to move a little bit more formatting logic into the locale itself. Should be straightforward, though.
This sounds like a useful feature. Is using a ! standard for escaping literal characters? Could you link to some other libraries that do this?
Hi Ben,
! is not really conventional. MomentJS uses brackets to escape characters:
moment().format('[today] DDDD'); // 'today Sunday'
I think it will be a better idea, it's safer and easy to remember.
That definitely looks more convenient for escaping long strings (compare with !t!o!d!a!y). Unfortunately, the Python re module doesn't natively support atomic matching, so it makes for some messy regular expressions in dealing with bracket escaping. I've added a quick fix here. In particular, this line is what concerns me. I've included a more concise solution above it, but it's not preferable as it allows for backtracking between the parentheses and might behave badly on long escaped sequences. (See here to read about atomic matching and this StackOverflow post to understand the workaround.)
If you guys are OK with this (stylistically), then I'll go ahead and add some documentation and tests and submit a pull request.
NOTE: I've also removed an accidental duplicate line from the parser.
This seems to be the right approach in general (using square brackets). The implementation in moment.js (https://github.com/moment/moment/blob/develop/moment.js#L29) is similar, and looks sane to me. I'll probably tweak / test the regex a bit on merge to make sure it's as efficient as possible.
That's interesting. I hadn't thought to look at the moment.js implementation, but upon inspection it looks like their regex doesn't allow for literal brackets within an escaped sequence. The regex in the commit I've linked to does so, and without exponential backtracking. We may or may not want to refine it to make sure that it's not doing superfluous checks though. I don't really know what nesting and literal [ escaping behavior _should_ be, but I feel like it should at least allow for literal brackets themselves.
:+1:
Got the same problem today, and had to to d.datetime.strftime. Which sucked cause it doesn't handle unicode properly in 2.7. BTW, why do we have different operators to format with arrow and datetime ? It should be the same. %stuff works well, and identify easily what's marker and what's not.
Same issue here - I'd like to format as "[date] at [time]", e.g. "Fri 01 May 2015 at 16:32", but the 'a' gets translated into AM/PM.
:+1: for square bracket support.
I like the bracket approach. I don't have time at the moment to make the PR for it though, does anyone else. I'd happily review and merge a PR with this.
:+1:
:+1:
I wouldn't mind throwing a \ before each escaped character, but I would love to have this feature in whatever form.
馃憤 this would be amazing
:+1:
馃憤
Time to tackle this one :+1:
Seems like #290 could be used to help with this process.
This now basically works for parsing but not for formatting.
Python 3.6.7 (default, Oct 25 2018, 09:16:13)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import arrow
>>> date_format = "YYYY-MM-DD h [h] m"
>>> my_date = "2018-03-09 8 h 40"
>>> arw=arrow.get(my_date, date_format)
<Arrow [2018-03-09T08:40:00+00:00]>
>>> arw.format(date_format)
'2018-03-09 8 [8] 40'
But it's not documented anywhere on https://arrow.readthedocs.io/en/latest/ so that needs to be fixed.
EDIT: updated that this does not working for formatting.
Most helpful comment
Hi Ben,
!is not really conventional. MomentJS uses brackets to escape characters:I think it will be a better idea, it's safer and easy to remember.