Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

[java-core] Timestamp.of(java.sql.Timestamp) is one second late for a time less than a second before the epoch

未关闭 适合新手
#14,636 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
java
领域
backend

调研方向

从 sdk-platform-java/java-core/google-cloud-core/src/main/java/com/google/cloud/Timestamp.java 开始,查看 issue 中链接的 Timestamp.of(java.sql.Timestamp) 方法。将它的秒数计算与 Timestamp.of(Date) 以及 epoch 附近的复现值进行比较。负的亚秒案例转换正确且与 Timestamp.of(Date) 一致时,即表示完成;issue 没有指定测试文件。

由索引模型根据 Issue 内容生成。

描述

Issue Details

Timestamp.of(java.sql.Timestamp) returns a time one second late for a java.sql.Timestamp between one second before the epoch and the epoch:

https://github.com/googleapis/google-cloud-java/blob/9de611fe5633e9f43c6081754c834cabcd0317bb/sdk-platform-java/java-core/google-cloud-core/src/main/java/com/google/cloud/Timestamp.java#L123-L136

The method takes the whole seconds as getTime() / 1000 and borrows a second only when that quotient is negative. For getTime() in (-1000, 0) the quotient is 0, so no second is borrowed, while getNanos() holds the positive fraction of the second before the epoch. new java.sql.Timestamp(-500) (1969-12-31T23:59:59.500Z) becomes 1970-01-01T00:00:00.500Z. The borrow was added for googleapis/java-core#159, whose example (-1001) has a nonzero quotient, so this range was left out. Math.floorDiv(getTime(), 1000) gives the right seconds for every value, as Timestamp.of(Date) already does through ofTimeMicroseconds.

Found while building an Apache Flink connector on google-cloud-firestore (flink-gcp/flink-connector-gcp).

Environment

  • OS Type and Version: macOS 26 (arm64)
  • Java Version and JDK Vendor: Eclipse Temurin 17.0.20

Dependencies

  • Libraries-Bom: 26.90.0
    • google-cloud-core 2.77.0

The affected code is identical in 2.77.0 and on main.

Reproducer

package repro;

import com.google.cloud.Timestamp;

public class SqlTimestampProbe {
  public static void main(String[] args) {
    for (long millis : new long[] {-1500, -500, -1, 0, 500}) {
      java.sql.Timestamp sql = new java.sql.Timestamp(millis);
      System.out.println(millis + " ms (" + sql.toInstant() + "): Timestamp.of(sql) = " + Timestamp.of(sql)
          + ", Timestamp.of(Date) = " + Timestamp.of(new java.util.Date(millis)));
    }
  }
}

Logs and Stack Trace

-1500 ms (1969-12-31T23:59:58.500Z): Timestamp.of(sql) = 1969-12-31T23:59:58.500000000Z, Timestamp.of(Date) = 1969-12-31T23:59:58.500000000Z
-500 ms (1969-12-31T23:59:59.500Z): Timestamp.of(sql) = 1970-01-01T00:00:00.500000000Z, Timestamp.of(Date) = 1969-12-31T23:59:59.500000000Z
-1 ms (1969-12-31T23:59:59.999Z): Timestamp.of(sql) = 1970-01-01T00:00:00.999000000Z, Timestamp.of(Date) = 1969-12-31T23:59:59.999000000Z
0 ms (1970-01-01T00:00:00Z): Timestamp.of(sql) = 1970-01-01T00:00:00Z, Timestamp.of(Date) = 1970-01-01T00:00:00Z
500 ms (1970-01-01T00:00:00.500Z): Timestamp.of(sql) = 1970-01-01T00:00:00.500000000Z, Timestamp.of(Date) = 1970-01-01T00:00:00.500000000Z

Behavior

Deterministic: any java.sql.Timestamp with -1000 < getTime() < 0.

主要语言
Java
星标
2.1k
派生
1.2k
平均合并
2 天 21 小时
30 天内合并 PR
139

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

googleapis/google-cloud-java 的其他 Issue

查看 googleapis/google-cloud-java 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。