我正在使用Quickstart和API 参考对Google Tasks 应用程序进行原型设计。检索任务没问题。问题是截止日期存储在哪个时区?我根据文档将到期日期检索为 DateTime,然后将其转换为我的默认(本地)时区中的显示值。
这是我观察到的:截止日期,例如“Wed, Oct 4”(如果您在 Google 日历上将任务移至 10 月 4 日,您会得到什么)在 UTC中被检索为“Wed, Oct 4 12:00am” ,这样当我将其转换为我的本地时区 (PDT) 时,我会得到“Tue, Oct 3 5:00pm”。
这让我认为 Google Tasks 以 UTC 存储所有 Task 日期和时间,然后将它们解释为您当地时区的日期和时间(换句话说,如果我在纽约,它仍会存储 10 月 4 日星期三 12:00am在 UTC。这与我的观察一致,即如果您更改时区,Google 日历界面上的任务到期日期不会更改。
有人可以确认或否认吗?
这是我的代码片段,基本上遵循快速入门。正在提取...
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;
}
计算要显示的日期时间:
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);
}
以及我现在转换为 UTC 以获得正确日期的日期时间转换本身:
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());
}
更新:有明确的证据表明 Google Tasks 无论如何都使用 UTC,这是计划于 10 月 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"