I have a schedule model with start date, start hour and start minute, and I need to compute the start datetime from these above fields. Since Odoo server is force in UTC AFAIK, it's impossible for me to compute the datetime right without having a timezone reference. Is there any way to do that without storing timezone?
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
- CRM
- e-Commerce
- Boekhouding
- Voorraad
- PoS
- Project
- MRP
Deze vraag is gerapporteerd
Hi,
Yes, it is not possible to accurately compute the start datetime from the start date, start hour, and start minute fields without storing a timezone reference.
As you mentioned, Odoo server is forced to use UTC, which means that any date or time values stored without a timezone will be assumed to be in UTC. This can lead to incorrect calculations when converting to or from local timezones.
You can make use of the pytz library in Python. This library provides a timezone database that can be used to localize a datetime object to a specific timezone.
Eg:
import pytz
from datetime import datetime, time
# assume start_hour and start_minute are integers representing the start time
start_hour = 10
start_minute = 30
# define a timezone, in this case assuming the user's local timezone
local_tz = pytz.timezone('Europe/London')
# create a time object from the hour and minute values
start_time = time(hour=start_hour, minute=start_minute)
# get the current date in the user's timezone
today = datetime.now(local_tz).date()
# combine the date and time to create a datetime object
start_datetime = local_tz.localize(datetime.combine(today, start_time))
# convert the datetime object to UTC for storage in the database
start_datetime_utc = start_datetime.astimezone(pytz.utc)
By using pytz to localize the datetime to a specific timezone, we can compute the correct UTC value without needing to store an additional timezone reference in the database.
Thanks
Hello Tung Nguyen,
First you need to convert start hour and start minute into time format, for this you can use
start_time = time(hours, minutes)
Here, specify start hour in place of 'hours' and start minute in 'minutes'.
Second, you need to combine this start_time with your start date, for this you can refer this below code
start_datetime = datetime.combine(start_date, start_time)
your_datetime_field_name = start_datetime
This will compute the start datetime.
Hope this is helpful.
Geniet je van het gesprek? Blijf niet alleen lezen, doe ook mee!
Maak vandaag nog een account aan om te profiteren van exclusieve functies en deel uit te maken van onze geweldige community!
AanmeldenGerelateerde posts | Antwoorden | Weergaven | Activiteit | |
---|---|---|---|---|
Take Users Timezone
Opgelost
|
|
3
jul. 25
|
4260 | |
Time & Date Issue
Opgelost
|
|
6
sep. 19
|
11603 | |
|
2
mrt. 16
|
8051 | ||
|
0
mrt. 15
|
8352 | ||
Datetime and Time Zones - OpenERP 7
Opgelost
|
|
1
jan. 24
|
12850 |