azimlord
Hi guys,
I’m having some trouble with utc_datetime.
So I have a form that accept start_time and end_time which both in utc_datetime and inside a bookings table.
The problem is that I’m in Malaysia so the booking that is created will be in UTC and when I convert it to "Asia/Kuala_Lumpur" using timex, it will show a different time.
Timex.Timezone.convert(booking.end_time, "Asia/Kuala_Lumpur")
For example:
start_time: 2019-09-08 22:00:00
end_time: 2019-09-08 23:00:00
when convert it using timex to "Asia/Kuala_Lumpur"
start_time: 2019-09-09 06:00:00
end_time: 2019-09-09 07:00:00
Now I see that I have 2 options:
- Use
naive_datetimeinstead ofutc_datetimeand store the timezone in a separate column - Manipulate the
start_timeandend_timefrom the form and shift it to the UTC timezone which I already tried and failed.
changeset
|> put_change(:start_time, Timex.shift(Timex.to_datetime(start_time, "Asia/Kuala_Lumpur"), "Etc/UTC"))
|> put_change(:end_time, Timex.shift(Timex.to_datetime(end_time, "Asia/Kuala_Lumpur"), "Etc/UTC"))
I’m not sure if I did it correctly or not and I’m not sure which is the best way
Thanks for your help in advance!
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
dimitarvp
I am not sure I understand your problem exactly.
azimlord
I’m sorry you don’t understand my question
I wanted to store the time correctly if using
utc_datetimeI have a model that looks like this
And I have a form which looks like this
But when I store it to the database and show it on a page it will look like this
2019-09-08 22:00:00ZAnd I try to set it to the local time and shift it to UTC time using
Timexwhich I show from my initial postSo when I want to display it, I will convert it to display the correct time.
For example:
From the form it, I will select the time
start_time: 2019-09-08 20:00:00end_time: 2019-09-08 21:00:00And convert it to UTC and store it into the database, which will change the time to:
start_time: 2019-09-08 12:00:00Zend_time: 2019-09-08 13:00:00ZWhere ever in the world, I will show the correct time depending on the timezone. For my timezone which is
"Asia/Kuala_Lumpur"will convert it back tostart_time: 2019-09-08 20:00:00end_time: 2019-09-08 21:00:00This how I do it currently in elixir but does not work
I’m also not sure if this is a proper way to do it or not.
So I thought of another way which using the
naive_datetimeinstead ofutc_datetimeand have another column in my table where it will store the timezone as well. The model should look like thisHope you understand
LostKobrakai
The problem you’re facing is that the submitted value of a
datetime_local_inputdoes not include timezone information at all. It’ll send the server a (“naive”) iso8601 datetime without timezone information. It’s also unlikely to change. Yourutc_datetimefield will then assume it’sUTC, because that column is meant to only ever hold datetimes withUTCset as timezone.I’ve spent some time to code up and document how I’d handle those cases and hide a bit of the boilerplate necessary. Since elixir 1.8 elixir can finally handle the problem on it’s own with a timezone database configured.
azimlord
I’m sorry if this is a stupid question
From the library that you make is to avoid the issue that mentions in this article How to save datetimes for future events - (when UTC is not the right answer). Correct?
I guess, with your solution, I need to add another 2 field, which is
with that, then my changeset just need to do this
before storing it into the database
and for displaying
will get the correct
datetimeBut I’m not sure how to get the
original_offset. Can you help me with this?And I’m guessing that the
time_zonewill be the timezone that I wanted. eg:"Asia/Kuala_Lumpur"If there’s anything wrong with what I’m saying, please do correct me.
LostKobrakai
You have it mostly correct.
I’d suggest a virtual field for the input separate to the field storing the utc_datetime, but this should work. Also you don’t need to
require TzDatetime. There are no macros involved here so just calling the functions is enough.The
timezonefield you’ll need to set somehow either by letting the user submit the value or adding it to the changeset based on other information your system has.As for the
original_offset: You shouldn’t need to care about that at all. It’s set byhandle_datetime/2and read byoriginal_datetime/2. The latter will return different values if there’s a missmatch detected between the stored offset and the offset based on your current timezone database.azimlord
Having a bit issue
So I separate the field for input and the datetime like you suggested.
When I try to create a booking, the changes are as below
But it returns an error
Not sure what is going
LostKobrakai
Elixir itself only ships with a timezone database supporting UTC and nothing else. This is because updating the timezone db shouldn’t really be bound to the release cycles of elixir the language. Currently the only implementation for full timezone support ships with the
tzdatalibrary, which you’d need to install / configure.azimlord
Nice. I works! Thank you!
Convert the
start_timeandend_timeusingTimex, shows the correct timeThank you very much!
LostKobrakai
Please use
original_datetime/2to convert the utc_datetime back to your original timezone. It mostly does shift the timezone just like timex does, but you’ll be made aware if the timezone definitions changed to cause a different offset to the time when the datetime was stored. This will hardly ever happen for datetimes in the past, but is a valid problem to look for in future datetimes.azimlord
Noted. Changed the conversion using
original_datetime/2.Is there any other information that I should be aware of regarding the solution?
What ‘should I do’ and what ‘I should not do’?