Google日历API将日期保存为UTC而不是本地时间。

huangapple go评论55阅读模式
英文:

Google calendar api saves dates as UTC and not local

问题

以下是翻译好的部分:

我正在开发一个带有API的应用程序,通过该API,您可以在Google日历中创建事件。我遇到的问题是事件的日期(已正确添加)不是使用我的时区(布鲁塞尔),而是使用UTC。

所以,如果我想在我的时区中创建一个8:45的事件,它会保存为我的日历中的6:45。

这是我目前的代码:

var reservation = new Event
{
    Summary = GenerateReservationSummary(reservationInput, selectedCares),
    Start = new EventDateTime
    {
        DateTimeDateTimeOffset = reservationInput.ReservationDate,
        TimeZone = TimeZone // 字符串设置为"Europe/Brussels"
    },
    End = new EventDateTime
    {
        DateTimeDateTimeOffset = GetEndDateTime(reservationInput.ReservationDate, selectedCares),
        TimeZone = TimeZone
    },
    Reminders = null // 没有提醒
};

在调试时,我能够确保ReservationDate实际上设置为8:45,这确实是情况,并且DateTimeKind也设置为Local

我尝试了多种方法,如下所示,但没有一种方法有效。

new DateTimeOffset(date, TimeSpan.FromHours(2))
new DateTimeOffset(date, TimeZoneInfo.Local.GetUtcOffset(date))

我错过了什么?

另外,我使用一个带有表单的Angular前端应用程序,该表单发送到API,我在我的Angular组件中有正确的日期,但在API调用负载中以UTC发送。如何修复我的问题(前端或后端)?

谢谢!

英文:

I am developing an app with an API, via which you can create an event in a google calendar. The issue I have is that the date of the event (which is correctly added) is not using my Timzone (Brussels) but UTC.

So if I want to create an event at 8:45 in my Timzone, it's saved as 6:45 in my calendar.

This is the code I actually have :

var reservation = new Event
{
    Summary = GenerateReservationSummary(reservationInput, selectedCares),
	Start = new EventDateTime
	{
		DateTimeDateTimeOffset = reservationInput.ReservationDate,
		TimeZone = TimeZone // string set as "Europe/Brussels"
	},
	End = new EventDateTime
	{
		DateTimeDateTimeOffset = GetEndDateTime(reservationInput.ReservationDate, selectedCares),
		TimeZone = TimeZone
	},
	Reminders = null // No reminders
};

When debugging, I was able to ensure that the ReservationDate was actually set at 8:45, which is the case, and DateTimeKind is also set at Local.

I tried multiple things such as the followings, but none of them worked.

new DateTimeOffset(date, TimeSpan.FromHours(2))
new DateTimeOffset(date, TimeZoneInfo.Local.GetUtcOffset(date))

What am I missing ?

Also, I use an Angular front-end app with the form that is sent to the API, and I have the right date in my angular component, but it is sent as UTC in the api call payload. What's the best way to fix my issue (front or back) ?

Thanks !

答案1

得分: 2

默认情况下,事件的时间使用日历本身的时区。

因此,在创建事件时设置时区会让事情变得有点混淆。

英文:

By default the time of an event uses the timezone of the calendar itself.

So by setting the timezone when you create the event you tend to end up confusing things a little.

huangapple
  • 本文由 发表于 2023年7月23日 21:29:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76748498.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定