Skip to content

Timezone issue when trying to patch google tasks item #2533

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

Open
longfangsong opened this issue Dec 12, 2024 · 1 comment
Open

Timezone issue when trying to patch google tasks item #2533

longfangsong opened this issue Dec 12, 2024 · 1 comment
Assignees

Comments

@longfangsong
Copy link

longfangsong commented Dec 12, 2024

Thanks for stopping by to let us know something could be better!

PLEASE READ: If you have a support contract with Google, please create an issue in the support console instead of filing on GitHub. This will ensure a timely response.

Please run down the following list and make sure you've tried the usual "quick fixes":

If you are still having issues, please be sure to include as much information as possible:

Environment details

  • OS type and version: macOS 15.1.1 (24B91), arm64
  • Python version: Python 3.13.0
  • pip version: pip 24.2
  • google-api-python-client version: 2.155.0

Steps to reproduce

  1. Patch an item in a google tasks list with api
  2. Read the result

Code example

from googleapiclient.discovery import build
from google.oauth2.credentials import Credentials
creds = Credentials(TOKEN)
service = build('tasks', 'v1', credentials=creds)

result = service.tasks().patch(
            tasklist="ZERNSFhRUWduTVFuOVV0UQ",
            task="elo1NllBMlJ4TERCZ3Nucg",
            body={'title': 'Subitem', 'status': 'needsAction', 'due': '2024-12-14T00:00:00+01:00', 'notes': None, 'parent': 'OXRUelVzcFNjYWphbTl6Wg'},
        ).execute()
print(result)

Note the timestamp is 2024-12-14T00:00:00+01:00.

The result is:

{'kind': 'tasks#task', 'id': 'elo1NllBMlJ4TERCZ3Nucg', 'etag': '"LTExNjY2OTMyNjc"', 'title': 'Subitem', 'updated': '2024-12-12T10:41:34.000Z', 'selfLink': 'https://www.googleapis.com/tasks/v1/lists/ZERNSFhRUWduTVFuOVV0UQ/tasks/elo1NllBMlJ4TERCZ3Nucg', 'parent': 'OXRUelVzcFNjYWphbTl6Wg', 'position': '00000000000000000000', 'status': 'needsAction', 'due': '2024-12-13T00:00:00.000Z', 'links': [], 'webViewLink': 'https://tasks.google.com/task/zZ56YA2RxLDBgsnr?sa=6'}

Note the timestamp is 2024-12-13T00:00:00.000Z.

Stack trace

NA

Making sure to follow these steps will guarantee the quickest resolution possible.

Thanks!

@bamjun
Copy link

bamjun commented May 3, 2025

The issue occurs because the due field was set using a local timezone offset like +01:00 (Europe timezone).
Google Tasks API internally stores the due field in UTC (Z) format.
Even if you send a local time with an offset, Google automatically converts it to UTC when saving.
Therefore, if you want to display the correct time in your local timezone (e.g., Europe/Paris), you need to manually convert it back when reading the API response.

Solution:
When reading the due field from the API response, parse it as UTC and then convert it to your local timezone.

Here’s an example:

from datetime import datetime
from zoneinfo import ZoneInfo  # Python 3.9+ standard library
from googleapiclient.discovery import build
from google.oauth2.credentials import Credentials

# Set up credentials
creds = Credentials(TOKEN)
service = build('tasks', 'v1', credentials=creds)

# Patch the task
result = service.tasks().patch(
    tasklist="ZERNSFhRUWduTVFuOVV0UQ",
    task="elo1NllBMlJ4TERCZ3Nucg",
    body={
        'title': 'Subitem',
        'status': 'needsAction',
        'due': '2024-12-14T00:00:00+01:00',
        'notes': None,
        'parent': 'OXRUelVzcFNjYWphbTl6Wg',
    },
).execute()

# Convert 'due' field from UTC to Europe/Paris timezone
due_from_api = result.get('due')

if due_from_api:
    # Parse the UTC time
    utc_time = datetime.fromisoformat(due_from_api.replace('Z', '+00:00'))
    
    # Convert to Europe/Paris time
    paris_time = utc_time.astimezone(ZoneInfo('Europe/Paris'))
    
    print(f"Due time in Europe/Paris timezone: {paris_time.isoformat()}")

# Print the full result
print(result)

Summary:

  • Input: You can send the due time with a local timezone offset if necessary, but Google will store it in UTC.
  • Output: Always manually convert the UTC time back to your desired local timezone (e.g., Europe/Paris) after reading the API response.

This way, you can correctly display the due time without any timezone confusion.

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

3 participants