【问题标题】:Get time for friends recent checkin获取朋友最近签到的时间
【发布时间】:2023-03-03 06:34:24
【问题描述】:
我正在使用/checkins/recent 端点。我试图获得入住时间。我找到了一个 json 对象createdAt。跟时间有关系吗?
所以,我的问题是如何从最近的签到中获取时间。
【问题讨论】:
标签:
api
geolocation
foursquare
checkin
【解决方案1】:
Foursquare API docs for checkin object 明确指出 createdAt 表示生成对象时的 unix 纪元时间(活动时间)。因此,您可以从纪元获取 UTC 时间。添加时区偏移量,表示获取本地时间需要添加的分钟数。例如,您问题中的示例签入对象来自孟加拉国,因此偏移量为360。
您没有提到该语言,但从您的标签列表中,我假设您使用过 Ruby,所以在 ruby 中执行此操作的简单方法是。
require 'date'
Time.at(your_createdAt_value).utc.to_datetime
#For example from your created at value
Time.at(1389630660).utc.to_datetime
#This will make a datetime object of value 2014-01-13T16:31:00+00:00
要获取本地时间,您可以使用 timeZoneOffset 的值。
#To get local time add in the timeZoneOffSet
Time.at(your_createdAt_value).utc.to_datetime+Rational(timeZoneOffset_value,1440)
#The 1440 value is the number of minutes in a day. Rational calculates
#the fraction of the day by using the two values and adds it to the datetime object.
#for example,
Time.at(1389630660).utc.to_datetime+Rational(360,1440)
#This will make a datetime object of value 2014-01-13T22:31:00+00:00
您可以使用格式化方法将对象格式化为您想要的值。