Opus1217
Opus1217

Reputation: 743

What timezone does Google Tasks use?

I am prototyping a Google Tasks app using the Quickstart and the API reference. No problem retrieving the tasks. The question is what timezone the Due Date is stored in? I retrieve the Due Date as a DateTime per the doc, and then convert it to a displayed value in my default (local) timezone.

Here's what I observe: a due-date such as "Wed, Oct 4" (what you would get if you moved a Task to Oct 4 on the Google Calendar) is retrieved as "Wed, Oct 4 12:00am" in UTC, so that when I convert it to my local timezone (PDT) I get "Tue, Oct 3 5:00pm".

This makes me think that Google Tasks stores all Task dates and times in UTC and then interprets them as dates and times in your local timezone (in other words, if I were in New York, it would still store Wed, Oct 4 12:00am in UTC. This is consistent with my observation that if you change timezones, Task Due Dates don't change on the Google Calendar interface.

Can anybody confirm or deny?

Here's my code snippets, basically following the quickstart. Fetching...

    private List<Task> getTasks() throws IOException {
    List<Task> tasks =
            mService.tasks()
                    .list("@default")
                    .setFields("items/id,items/title,items/notes,items/status,items/due,items/completed")
                    .execute()
                    .getItems();
    return tasks;

}

Computing the date-time to display:

        final boolean isCompleted = "completed".equals(task.getStatus());
    viewHolder.mCompleteCheckbox.setChecked(isCompleted);
    viewHolder.mTitle.setText(task.getTitle());
    if ((task.getNotes() == null) || task.getNotes().isEmpty()) {
        viewHolder.mNotes.setVisibility(GONE);
    } else {
        viewHolder.mNotes.setVisibility(View.VISIBLE);
        viewHolder.mNotes.setText(task.getNotes());
    }
    final DateTime dueOrCompletedDate = isCompleted ? task.getCompleted() : task.getDue();
    if (dueOrCompletedDate == null ) {
        viewHolder.mDueOrCompleted.setVisibility(GONE);
    } else {
        viewHolder.mDueOrCompleted.setVisibility(View.VISIBLE);
        final String dateTimeString = Utils.timeMillisToDefaultShortDateTime(dueOrCompletedDate.getValue());
        viewHolder.mDueOrCompleted.setText(dateTimeString);
    }

and the date-time conversion itself where I am now converting to UTC to get the correct dates:

      public static String dateFormat = "EEE, MMM d";
  public static String timeMillisToDefaultShortTime(final long timeMillis) {
    DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT);
    df.setTimeZone(TimeZone.getTimeZone("UTC"));
    String formattedTime = df.format(timeMillis);
    //TODO: Bit of a hack; Parse out p.m. and PM to hh:mmpm (to save space)
    return formattedTime.replace("p.m.","pm").replace("P.M.","pm").replace("PM","pm").replace(" pm","pm")
            .replace("a.m.","am").replace("A.M.","am").replace("AM","am").replace(" am","am");
  }
  public static String timeMillisToDefaultShortDateTime(final long timeMillis) {
      String defaultShortTime = timeMillisToDefaultShortTime(timeMillis);
      if (defaultShortTime.equals("12:00am")) defaultShortTime = "";
      return timeMillisToDateFormat(timeMillis, dateFormat).concat(" ").concat(defaultShortTime);
  }
  private static String timeMillisToDateFormat(long milliSeconds, String dateFormat)
  {
    // Create a DateFormatter object for displaying date in specified format.
    SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
    //FIXME: Convert to UTC
    formatter.setTimeZone(TimeZone.getTimeZone("UTC"));

    // Create a calendar object that will convert the date and time value in milliseconds to date.
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(milliSeconds);
    return formatter.format(calendar.getTime());
  }

UPDATE: Clear evidence that Google Tasks uses UTC regardless, here's the debug view of a Task scheduled for Oct 3:

    o2 = {Task@5424}  size = 4
 0 = {DataMap$Entry@5501} "due" -> "2017-10-03T00:00:00.000Z"
 1 = {DataMap$Entry@5502} "id" -> "MDg1MjU5NjA3OTE3MzY1OTM2MjM6MDoxNzAwMTU3NzM"
 2 = {DataMap$Entry@5503} "status" -> "needsAction"
 3 = {DataMap$Entry@5504} "title" -> "xxxxx"

Upvotes: 2

Views: 1801

Answers (1)

ReyAnthonyRenacia
ReyAnthonyRenacia

Reputation: 17651

Since you mentioned Google Calendar, I think this answers that question:

Daylight savings time

Google Calendar uses Coordinated Universal Time (UTC) to help avoid issues with daylight savings time.

When events are created, they're converted into UTC, but you'll always see them in your local time.

If an area switches their time zone, events created before we knew about the change might be in the wrong time zone.

Upvotes: 1

Related Questions