doublep / datetime

Library for parsing, formatting, matching and recoding timestamps and date-time format strings.
GNU General Public License v3.0
19 stars 3 forks source link

How to parse and format ISO-8601 #2

Open ghost opened 6 years ago

ghost commented 6 years ago

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'

2) 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!

doublep commented 6 years ago

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

doublep commented 6 years ago

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.

bertschneider commented 4 years ago

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:

doublep commented 4 years ago

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.

bertschneider commented 4 years ago

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

doublep commented 1 year ago

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.