Podio:在设置DateTime字段值

时间:2016-10-22 01:39:05

标签: ruby datetime podio

使用Podio API创建新项目或更新现有项目,并将DateTime字段值设置为:2016-10-21 14:15:00(例如)。哪个时区将用于存储此DateTime?

E.g。 请求

app_id = <some app with title and date fields>
content = {'title' => 'Date set to "14:15"',
           'date'  => {'start' => '2016-10-21 14:15:00', 
                       'end'   => '2016-10-21 15:00:00'}}
item = Podio::Item.create(app_id, 'fields' => content)

结果

'start_date_utc' => 2016-10-21
'end'            => 2016-10-21 15:00:00
'end_date'       => 2016-10-21
'end_date_utc'   => 2016-10-21
'start_time_utc' => 12:15:00
'start_time'     => 14:15:00
'start_date'     => 2016-10-21
'start'          => 2016-10-21 14:15:00
'end_time'       => 15:00:00
'end_time_utc'   => 13:00:00
'end_utc'        => 2016-10-21 13:00:00
'start_utc'      => 2016-10-21 12:15:00

哪个好,因为我看到时间值14:15与我设置14:15相同,但我如何控制并设置特定的时区到此DateTime场?

1 个答案:

答案 0 :(得分:6)

看起来Podio API很聪明,并且知道我的时区。

以下是一些包含请求和结果的示例。 将DateTime字段设置为14:15:00,将其作为不同的用户和app。

进行身份验证
content = {'date' => {'start' => '2016-10-21 14:15:00'}}
Podio.client.authenticate_with_credentials(<user A>, <pass>)
item_created_by_userA = Podio::Item.create(app_id, 'fields' => content)

Podio.client.authenticate_with_credentials(<user B>, <pass>)
item_created_by_userB = Podio::Item.create(app_id, 'fields' => content)

Podio.client.authenticate_with_app(<app ID>, <app token>)
item_created_by_app = Podio::Item.create(app_id, 'fields' => content)

然后设置值为:

item_created_by_userA:
'start'     => 2016-10-21 14:15:00
'start_utc' => 2016-10-21 12:15:00

item_created_by_userB:
'start'     => 2016-10-21 14:15:00
'start_utc' => 2016-10-21 21:15:00

item_created_by_app:
'start'     => 2016-10-21 14:15:00
'start_utc' => 2016-10-21 14:15:00

然后,值2016-10-21 14:15:00被API视为2016-10-21 14:15:00 +0200,因为userA时区设置为 UTC + 02 ,同样的值被API视为2016-10-21 14:15:00 -0700因为userB时区是 UTC-07 (在Podio中,在帐户设置中)。如果以app身份验证,则假设时区为 UTC

所以,如果我想设置值2016-10-21 14:15:00 +0800(假设我想设置吉隆坡的时区),那么我必须先将它转换为我自己的时区(无论在Podio帐户设置中设置什么) ,然后发送到Podio API,如下:

date_as_str  = "2016-10-22 14:15:00 +08:00"  # trying to set value with UTC+08
date_with_tz = DateTime.parse(date_as_str).in_time_zone("Europe/Copenhagen") # when Copenhagen is userA's timezone
date_to_send = date_with_tz.strftime('%Y-%m-%d %H:%M:%S')
content = {'date' => {'start' => date_to_send}}
Podio.client.authenticate_with_credentials(<user A>, <pass>)
item_created_by_userA = Podio::Item.create(app_id, 'fields' => content)