Cookies expiration time format
Asked Answered
C

2

9

I created DB from google chrome's Cookies file and one of the columns in the cookies table is expires_utc. The format of the column is like - 13169399213145330, which is more like unix time. But when i'm trying to use unix time converter, it gives wrong values.

So, what format is that and how i can convert it to actually unix time?

Campaign answered 20/4, 2017 at 11:25 Comment(1)
Use java.time, the modern Java date and time API, for clear code: Instant .parse("1601-01-01T00:00:00Z") .plus(13169399213145330L, ChronoUnit.MICROS) .getEpochSecond(). Obviously extract the 1601-01-01 time into a constant, say, CHROME_COOKIE_EPOCH.Keats
C
15

So, after long researches on this topic, here is the solution:

  • Chrome's cookies timestap's epoch starts 1601-01-01T00:00:00Z (Why? Because.)

So, it's 11644473600 seconds before the UNIX epoch. To convert chrome's timestamp to UNIX, you need to:

  1. Devide the actual timestamp (in my case it's expires_utc column in cookies table) by 1000000 // And someone should explain my why.
  2. Subtract 11644473600
  3. DONE! Now you got UNIX timestamp.
Campaign answered 20/4, 2017 at 12:49 Comment(3)
Thanks for this! About the divide by 1.000.000 - it's probably because it is in millisecondsHatty
actually it's microseconds und by dividing it by 1M you get seconds.Draper
Here's more info on the how and why of the 1601/1/1, it's not random :) #10850217Ashia
L
0

For the second step: it should subtract 163214400000, and got unix timestamp. The following demo code is help to change a expires_utc column time to java.sql.Date.

import java.util.Date;  

public class Main {  
    public static void main(String[] args) {  
        // 假设 expires_utc 是从 1601-01-01T00:00:00Z 开始的微秒数  
        long expiresUtcMicros = 163214400000L; // 示例值  
  
        // 将微秒转换为毫秒  
        long expiresUtcMillis = expiresUtcMicros / 1000;  
  
        // 计算从 1601-01-01 到 1970-01-01 的时间差(毫秒)  
        // 注意:这里需要手动计算或查找这个时间差,因为 Java 标准库不直接支持  
        // 假设这个差值已经计算好(这里是一个大概的估计值,实际值可能不同)  
        long epochDifferenceMillis = 11644473600000L; // 大约是 11644 天  
  
        // 从 expiresUtcMillis 中减去差值,并转换为 Date  
        Date date = new Date(expiresUtcMillis - epochDifferenceMillis);  
  
        // 输出结果  
        System.out.println(date);  
    }  
}  
  
// 注意:epochDifferenceMillis 需要你根据实际情况来计算或查找
Laceration answered 6/8 at 6:39 Comment(2)
Thanks for your contribution. It’s recommended to use java.time, the modern Java date and time API, for all time work and not Date ever. The Date class had bad design problems and has been outdated since java.time came out with Java 1.8, which was 10 years ago.Keats
Exactly, java.util.date package is out of date. Can change to use the time func in java.time. Thanks for your good advice!Laceration

© 2022 - 2024 — McMap. All rights reserved.