go - Instagram 媒体 ID 到时间戳的转换

标签 go time instagram unix-timestamp date-conversion

Instagram 在这篇博文中解释了他们如何创建媒体 ID

https://instagram-engineering.com/sharding-ids-at-instagram-1cf5a71e5a5c

Each of our IDs consists of: 41 bits for time in milliseconds (gives us 41 years of IDs with a custom epoch) 13 bits that represent the logical shard ID 10 bits that represent an auto-incrementing sequence, modulus 1024. This means we can generate 1024 IDs, per shard, per millisecond.

our ‘epoch’ begins on January 1st, 2011 not sure if that's the actual production value or only for the example

如何从媒体 ID 取回时间戳?

我有这两个媒体 ID,我知道时间戳,但我需要从其他媒体中提取它

2384288897814875714 2020-08-26T13:43:27Z

2383568809444681765 2020-08-25T13:52:46Z

最佳答案

playground

package main

import (
    "fmt"
    "time"
)

const (
    instaEpoch int64 = 1314220021721
    mediaID int64 = 2384288897814875714
)

func main()  {
    extractedTimestamp := mediaID >> (64-41)
    timeFromMediaID := extractedTimestamp + instaEpoch
    fmt.Println(time.Unix(timeFromMediaID/1000,0).UTC())
}

输出:

2020-08-26 13:43:27 +0000 UTC

您只需右移 id 即可取回时间戳。然后你必须将毫秒添加到 instagram 正在使用的时代。

关于go - Instagram 媒体 ID 到时间戳的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63683015/

相关文章:

go - 是否可以在设计用于 go generate 的模板上使用 gofmt?

for-loop - 可以在 'for .. range' 循环中的每次迭代后添加 final 语句(就像在标准 for 循环中一样)吗?

java - 将字符串转换为时间并计算差异

python - 如何在没有 token 或访问控制的情况下从 userId(pk) 获取 instagram 用户个人资料详细信息?

json - 解码到相同的结构但不同的 json 名称

algorithm - 实时显示一段时间内最常见/重复的元素

精度更高的 Java 时间间隔百分比

java - 等待执行功能而不卡住进程

html - 无法显示代码笔图标

ios - 在应用程序中同时使用 AFNetworking 和 ASIHTTPRequest 是否有一些好处?