-
Notifications
You must be signed in to change notification settings - Fork 3.4k
[WEB-2843]chore: handled the cycle date time using project timezone #6187
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,100 @@ | ||||||||||
| import pytz | ||||||||||
| from plane.db.models import Project | ||||||||||
| from datetime import datetime, time | ||||||||||
| from datetime import timedelta | ||||||||||
|
|
||||||||||
| def user_timezone_converter(queryset, datetime_fields, user_timezone): | ||||||||||
| # Create a timezone object for the user's timezone | ||||||||||
| user_tz = pytz.timezone(user_timezone) | ||||||||||
|
|
||||||||||
| # Check if queryset is a dictionary (single item) or a list of dictionaries | ||||||||||
| if isinstance(queryset, dict): | ||||||||||
| queryset_values = [queryset] | ||||||||||
| else: | ||||||||||
| queryset_values = list(queryset) | ||||||||||
|
|
||||||||||
| # Iterate over the dictionaries in the list | ||||||||||
| for item in queryset_values: | ||||||||||
| # Iterate over the datetime fields | ||||||||||
| for field in datetime_fields: | ||||||||||
| # Convert the datetime field to the user's timezone | ||||||||||
| if field in item and item[field]: | ||||||||||
| item[field] = item[field].astimezone(user_tz) | ||||||||||
sriramveeraghanta marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
|
|
||||||||||
| # If queryset was a single item, return a single item | ||||||||||
| if isinstance(queryset, dict): | ||||||||||
| return queryset_values[0] | ||||||||||
| else: | ||||||||||
| return queryset_values | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def convert_to_utc(date, project_id, is_end_date=False): | ||||||||||
| """ | ||||||||||
| Converts a start date string to the project's local timezone at 12:00 AM | ||||||||||
| and then converts it to UTC for storage. | ||||||||||
| Args: | ||||||||||
| date (str): The date string in "YYYY-MM-DD" format. | ||||||||||
| project_id (int): The project's ID to fetch the associated timezone. | ||||||||||
| Returns: | ||||||||||
| datetime: The UTC datetime. | ||||||||||
| """ | ||||||||||
| # Retrieve the project's timezone using the project ID | ||||||||||
| project = Project.objects.get(id=project_id) | ||||||||||
sriramveeraghanta marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
| project_timezone = project.timezone | ||||||||||
| if not date or not project_timezone: | ||||||||||
| raise ValueError("Both date and timezone must be provided.") | ||||||||||
|
|
||||||||||
| # Parse the string into a date object | ||||||||||
| start_date = datetime.strptime(date, "%Y-%m-%d").date() | ||||||||||
|
|
||||||||||
| # Get the project's timezone | ||||||||||
| local_tz = pytz.timezone(project_timezone) | ||||||||||
|
|
||||||||||
| # Combine the date with 12:00 AM time | ||||||||||
| local_datetime = datetime.combine(start_date, time.min) | ||||||||||
|
|
||||||||||
| # Localize the datetime to the project's timezone | ||||||||||
| localized_datetime = local_tz.localize(local_datetime) | ||||||||||
|
|
||||||||||
| # If it's an end date, subtract one minute | ||||||||||
| if is_end_date: | ||||||||||
| localized_datetime -= timedelta(minutes=1) | ||||||||||
|
Comment on lines
+62
to
+63
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Re-evaluate the logic for 'is_end_date' adjustment Subtracting one minute from the start of the end date may lead to unexpected results, such as the end date being the previous day. Consider setting the time to the end of the day to accurately represent the end date: # Instead of subtracting one minute
-localized_datetime -= timedelta(minutes=1)
+localized_datetime = localized_datetime.replace(hour=23, minute=59, second=59, microsecond=999999)📝 Committable suggestion
Suggested change
|
||||||||||
|
|
||||||||||
| # Convert the localized datetime to UTC | ||||||||||
| utc_datetime = localized_datetime.astimezone(pytz.utc) | ||||||||||
|
|
||||||||||
| # Return the UTC datetime for storage | ||||||||||
| return utc_datetime | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def convert_utc_to_project_timezone(utc_datetime, project_id): | ||||||||||
| """ | ||||||||||
| Converts a UTC datetime (stored in the database) to the project's local timezone. | ||||||||||
| Args: | ||||||||||
| utc_datetime (datetime): The UTC datetime to be converted. | ||||||||||
| project_id (int): The project's ID to fetch the associated timezone. | ||||||||||
| Returns: | ||||||||||
| datetime: The datetime in the project's local timezone. | ||||||||||
| """ | ||||||||||
| # Retrieve the project's timezone using the project ID | ||||||||||
| project = Project.objects.get(id=project_id) | ||||||||||
| project_timezone = project.timezone | ||||||||||
| if not project_timezone: | ||||||||||
| raise ValueError("Project timezone must be provided.") | ||||||||||
|
|
||||||||||
| # Get the timezone object for the project's timezone | ||||||||||
| local_tz = pytz.timezone(project_timezone) | ||||||||||
|
|
||||||||||
| # Convert the UTC datetime to the project's local timezone | ||||||||||
| if utc_datetime.tzinfo is None: | ||||||||||
| # Localize UTC datetime if it's naive (i.e., without timezone info) | ||||||||||
| utc_datetime = pytz.utc.localize(utc_datetime) | ||||||||||
|
|
||||||||||
| # Convert to the project's local timezone | ||||||||||
| local_datetime = utc_datetime.astimezone(local_tz) | ||||||||||
|
|
||||||||||
| return local_datetime | ||||||||||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.