datetime icon indicating copy to clipboard operation
datetime copied to clipboard

How to parse and format ISO-8601

Open ghost opened this issue 7 years ago • 6 comments

I'm excited to use this library, as I suspect it may help with many of my Emacs datetime issues. I had a couple questions about how to make some transformations, and I'm hoping you might be willing to help.

  1. One common scenario is I have a timestamp like this and I want to get the UTC version:
>>> ts = "2018-06-12T22:10:00+03:00"
>>> pendulum.parse(ts).in_tz('UTC').to_iso8601_string()
'2018-06-12T19:10:00Z'
  1. Or I have an org-mode format string and I want the ISO format.
>>> ts = "[2018-06-12 Tue 22:10]"
>>> pendulum.from_format(ts, "[YYYY-MM-DD ddd HH:mm]", tz='America/New_York'
    ).to_iso8601_string()
'2018-06-12T22:10:00-04:00'

I couldn't figure out how to do these in datetime.el. Would you be willing to show how to do this, if that functionality is available? Thanks very much!

ghost avatar Jun 13 '18 22:06 ghost

Unfortunately, parsing is not implemented yet, only formatting and matching.

doublep avatar Jun 19 '18 17:06 doublep

Partially implemented in 0.6. However, your first scenario cannot be done with 'datetime' yet (without hacks at least), because timezone has to be fixed at the time parser function is generated. So, not closing the issue yet.

doublep avatar Sep 29 '18 19:09 doublep

I tried to parse a log entry using your other project logview and stumbled into this issue. Is there any chance of supporting this in the near future? Keep up the great work :+1:

bertschneider avatar Apr 16 '20 19:04 bertschneider

I should implement this eventually... In the meantime a workaround is to include timezone into the pattern literally, e.g. like in this example:

(funcall (datetime-parser-to-float 'java "ccc dd MMM HH:mm:ss 'CEST' yyyy") "Sat 18 Apr 17:55:59 CEST 2020")

Of course, this will help only if you ever use one timezone. In your case, only if the whole log contains entries in the same timezone, which does sound likely.

doublep avatar Apr 18 '20 15:04 doublep

Thx for the hint I will try that out :+1:

bertschneider avatar Apr 21 '20 17:04 bertschneider

Many years later the original request is finally doable with datetime 0.9:

(let ((parser           (datetime-parser-to-float 'java "yyyy-MM-dd'T'HH:mm:ssXXX"))
      (formatter-as-utc (datetime-float-formatter 'java "yyyy-MM-dd'T'HH:mm:ssXXX")))
  (funcall formatter-as-utc (funcall parser "2018-06-12T22:10:00+03:00")))
==> "2018-06-12T19:10:00Z"

(let ((parser    (datetime-parser-to-float 'java "'['yyyy-MM-dd E HH:mm']'"       :timezone 'America/New_York))
      (formatter (datetime-float-formatter 'java "'['yyyy-MM-dd'T'HH:mm:ssXXX']'" :timezone 'America/New_York)))
  (funcall formatter (funcall parser "[2018-06-12 Tue 22:10]")))
==> "[2018-06-12T22:10:00-04:00]"

See disclaimers in the documentation about apparent unneeded complexity.

I'm still not closing this issue, though, because timezone names (as opposed to offsets) are still not fully supported.

doublep avatar Sep 17 '23 19:09 doublep