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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] datetime.datetime from TimePoint? #196

Closed
movermeyer opened this issue Nov 13, 2021 · 2 comments
Closed

[Question] datetime.datetime from TimePoint? #196

movermeyer opened this issue Nov 13, 2021 · 2 comments
Assignees

Comments

@movermeyer
Copy link

movermeyer commented Nov 13, 2021

Hey everyone 馃憢,

I just found out about this project and was hoping to add metomi/isodatetime to ciso8601's parser benchmarks.

Is there a way to get a datetime.datetime object from a TimePoint object?

(In my case, I'm trying to do an equivalency comparison with a datetime)

I took a quick look through this project's code, but was unsuccessful.

@MetRonnie
Copy link
Contributor

Hi, I don't think we have a method for doing that, but there are a couple of ways of achieving it currently:

  1. Converting to seconds since unix epoch

    >>> from metomi.isodatetime.data import TimePoint
    >>> import datetime
    >>> timepoint = TimePoint(year=2010, month_of_year=3, hour_of_day=10, time_zone_hour=-3)
    >>> secs = int(timepoint.get("seconds_since_unix_epoch"))
    >>> datetime.fromtimestamp(secs)
    datetime.datetime(2010, 3, 1, 13, 0)

    Caveat: This loses time zone info (but does convert to UTC).

  2. Using strftime/strptime

    >>> dump_format = "%Y%m%dT%H%M%S%z"
    >>> tp_str = timepoint.strftime(dump_format)
    >>> datetime.strptime(tp_str, dump_format)
    datetime.datetime(2010, 3, 1, 10, 0, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=75600)))

    (I've no idea why the time zone ends up weirdly for this example, but 75600 seconds minus 1 day does equal -3 hours)

Note: the above applies to isodateime 2.0.2 (current version). In the next version (likely to be called isodatetime 2.1.0), the TimePoint.get() method is going away and you will have to do secs = timepoint.seconds_since_unix_epoch instead. The next version will also contain a number of significant bug fixes and TimePoints will no longer be mutable as they are in 2.0.2.

@movermeyer
Copy link
Author

movermeyer commented Nov 15, 2021

Thanks for the response. 馃憦

I ended up writing a quick and dirty comparison function, that works well enough for my use case (the use case has very relaxed requirements).

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

2 participants