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
To datetime with timezone #226
Conversation
@@ -39,7 +39,8 @@ defimpl Timex.Protocol, for: DateTime do | |||
def to_date(date), do: DateTime.to_date(date) | |||
|
|||
@spec to_datetime(DateTime.t, timezone :: Types.valid_timezone) :: DateTime.t | {:error, term} | |||
def to_datetime(%DateTime{} = d, _timezone), do: d | |||
def to_datetime(%DateTime{time_zone: time_zone} = d, timezone) when time_zone == timezone, do: d |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can just do to_datetime(%DateTime{time_zone: timezone} = d, timezone), do: d
which will only match if both the provided timezone and the one on the DateTime are the same.
You're definitely correct, your changes look good except for the one tweak I suggested. I'll get this merged once that's updated. Thanks for the PR! |
1 similar comment
@bitwalker thanks for the hint! I didn't know that. I just found out that |
Yeah could you add it to the |
@bitwalker OK, done |
Thanks again! |
This changes the behavior of
to_datetime
when called with aDateTime
struct.I'm writing an application that needs to convert datetimes to other timezones and I didn't know about
Timex.Timezone.convert/2
I was surprised by the current behavior:
I was even more surprised by:
I think the more logical thing to do is to convert the date if the timezone differs from the one in the
DateTime
struct.What do you think? Is there something I am overlooking?