Hàm dấu thời gian

Hàm dấu thời gian

Tên Mô tả
CURRENT_TIMESTAMP Tạo một TIMESTAMP tương ứng với thời gian yêu cầu.
TIMESTAMP_TRUNC Cắt bớt một TIMESTAMP theo độ chi tiết nhất định.
UNIX_MICROS_TO_TIMESTAMP Chuyển đổi số lượng micro giây kể từ 1970-01-01 00:00:00 UTC thành TIMESTAMP
UNIX_MILLIS_TO_TIMESTAMP Chuyển đổi số mili giây kể từ 1970-01-01 00:00:00 UTC thành TIMESTAMP
UNIX_SECONDS_TO_TIMESTAMP Chuyển đổi số giây kể từ 1970-01-01 00:00:00 UTC thành TIMESTAMP
TIMESTAMP_ADD Thêm một khoảng thời gian vào TIMESTAMP
TIMESTAMP_SUB Trừ một khoảng thời gian cho TIMESTAMP
TIMESTAMP_TO_UNIX_MICROS Chuyển đổi TIMESTAMP thành số vi giây kể từ 1970-01-01 00:00:00 UTC
TIMESTAMP_TO_UNIX_MILLIS Chuyển đổi TIMESTAMP thành số mili giây kể từ 1970-01-01 00:00:00 UTC
TIMESTAMP_TO_UNIX_SECONDS Chuyển đổi TIMESTAMP thành số giây kể từ 1970-01-01 00:00:00 UTC

CURRENT_TIMESTAMP

Cú pháp:

current_timestamp() -> TIMESTAMP

Nội dung mô tả:

Lấy dấu thời gian ở đầu thời gian yêu cầu input (được diễn giải là số lượng vi giây kể từ 1970-01-01 00:00:00 UTC).

Giá trị này ổn định trong một truy vấn và sẽ luôn phân giải thành cùng một giá trị nếu được gọi nhiều lần.

TIMESTAMP_TRUNC

Cú pháp:

timestamp_trunc(timestamp: TIMESTAMP, granularity: STRING[, timezone: STRING]) -> TIMESTAMP

Nội dung mô tả:

Cắt bớt dấu thời gian xuống một mức độ chi tiết nhất định.

Đối số granularity phải là một chuỗi và là một trong những giá trị sau:

  • microsecond
  • millisecond
  • second
  • minute
  • hour
  • day
  • week
  • week([weekday])
  • month
  • quarter
  • year
  • isoyear

Nếu bạn cung cấp đối số timezone, thì việc cắt bớt sẽ dựa trên ranh giới lịch của múi giờ đã cho (ví dụ: việc cắt bớt theo ngày sẽ cắt bớt đến nửa đêm theo múi giờ đã cho). Việc cắt bớt sẽ tuân theo giờ mùa hè.

Nếu bạn không cung cấp timezone, thì việc cắt bớt sẽ dựa trên ranh giới lịch UTC.

Đối số timezone phải là một chuỗi thể hiện múi giờ trong cơ sở dữ liệu tz, ví dụ: America/New_York. Bạn cũng có thể sử dụng độ lệch thời gian tuỳ chỉnh bằng cách chỉ định độ lệch so với GMT.

Ví dụ:

timestamp granularity timezone timestamp_trunc(timestamp, granularity, timezone)
2000-01-01 10:20:30:123456 UTC "second" Không được cung cấp 2001-01-01 10:20:30 UTC
1997-05-31 04:30:30 UTC "day" Không được cung cấp 1997-05-31 00:00:00 UTC
1997-05-31 04:30:30 UTC "day" "America/Los_Angeles" 1997-05-30 07:00:00 UTC
2001-03-16 04:00:00 UTC "week(friday) Không được cung cấp 2001-03-16 00:00:00 UTC
2001-03-23 04:00:00 UTC "week(friday) "America/Los_Angeles" 2001-03-23 17:00:00 UTC
20:00:00 ngày 24 tháng 1 năm 2026 (giờ UTC) "tháng" "GMT+06:32:43" 2026-01-01T06:32:43 UTC

UNIX_MICROS_TO_TIMESTAMP

Cú pháp:

unix_micros_to_timestamp(input: INT64) -> TIMESTAMP

Nội dung mô tả:

Chuyển đổi input (được diễn giải là số vi giây kể từ 1970-01-01 00:00:00 UTC) thành TIMESTAMP. Trả về một error nếu không thể chuyển đổi input thành TIMESTAMP hợp lệ.

Ví dụ:

input unix_micros_to_timestamp(input)
0L 00:00:00 ngày 1 tháng 1 năm 1970 (giờ UTC)
400123456L 1970-01-01 00:06:40.123456 UTC
-1000000L 1969-12-31 23:59:59 UTC

Web

const result = await execute(db.pipeline()
  .collection("documents")
  .select(
    field("createdAtMicros").unixMicrosToTimestamp().as("createdAtString")
  )
);
Swift
let result = try await db.pipeline()
  .collection("documents")
  .select([
    Field("createdAtMicros").unixMicrosToTimestamp().as("createdAtString")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("documents")
    .select(
        field("createdAtMicros").unixMicrosToTimestamp().alias("createdAtString")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("documents")
    .select(
        field("createdAtMicros").unixMicrosToTimestamp().alias("createdAtString")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("documents")
    .select(
        Field.of("createdAtMicros")
        .unix_micros_to_timestamp()
        .as_("createdAtString")
    )
    .execute()
)

UNIX_MILLIS_TO_TIMESTAMP

Cú pháp:

unix_millis_to_timestamp(input: INT64) -> TIMESTAMP

Nội dung mô tả:

Chuyển đổi input (được diễn giải là số mili giây kể từ 1970-01-01 00:00:00 UTC) thành TIMESTAMP. Trả về một error nếu không thể chuyển đổi input thành TIMESTAMP hợp lệ.

Ví dụ:

input unix_millis_to_timestamp(input)
0L 00:00:00 ngày 1 tháng 1 năm 1970 (giờ UTC)
4000123L 1970-01-01 01:06:40.123 UTC
-1000000L 1969-12-31 23:43:20 UTC

Web

const result = await execute(db.pipeline()
  .collection("documents")
  .select(
    field("createdAtMillis").unixMillisToTimestamp().as("createdAtString")
  )
);
Swift
let result = try await db.pipeline()
  .collection("documents")
  .select([
    Field("createdAtMillis").unixMillisToTimestamp().as("createdAtString")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("documents")
    .select(
        field("createdAtMillis").unixMillisToTimestamp().alias("createdAtString")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("documents")
    .select(
        field("createdAtMillis").unixMillisToTimestamp().alias("createdAtString")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("documents")
    .select(
        Field.of("createdAtMillis")
        .unix_millis_to_timestamp()
        .as_("createdAtString")
    )
    .execute()
)

UNIX_SECONDS_TO_TIMESTAMP

Cú pháp:

unix_seconds_to_timestamp(input: INT64) -> TIMESTAMP

Nội dung mô tả:

Chuyển đổi input (được diễn giải là số giây kể từ 1970-01-01 00:00:00 UTC) thành TIMESTAMP. Trả về một error nếu không thể chuyển đổi input thành TIMESTAMP hợp lệ.

Ví dụ:

input unix_seconds_to_timestamp(input)
0L 00:00:00 ngày 1 tháng 1 năm 1970 (giờ UTC)
60L 1970-01-01 00:01:00 UTC
-300L 1969-12-31 23:55:00 UTC

Web

const result = await execute(db.pipeline()
  .collection("documents")
  .select(
    field("createdAtSeconds").unixSecondsToTimestamp().as("createdAtString")
  )
);
Swift
let result = try await db.pipeline()
  .collection("documents")
  .select([
    Field("createdAtSeconds").unixSecondsToTimestamp().as("createdAtString")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("documents")
    .select(
        field("createdAtSeconds").unixSecondsToTimestamp().alias("createdAtString")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("documents")
    .select(
        field("createdAtSeconds").unixSecondsToTimestamp().alias("createdAtString")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("documents")
    .select(
        Field.of("createdAtSeconds")
        .unix_seconds_to_timestamp()
        .as_("createdAtString")
    )
    .execute()
)

TIMESTAMP_ADD

Cú pháp:

timestamp_add(timestamp: TIMESTAMP, unit: STRING, amount: INT64) -> TIMESTAMP

Nội dung mô tả:

Thêm amount bằng unit từ timestamp. Đối số amount có thể là số âm, trong trường hợp đó, đối số này tương đương với TIMESTAMP_SUB.

Đối số unit phải là một chuỗi và là một trong những giá trị sau:

  • microsecond
  • millisecond
  • second
  • minute
  • hour
  • day

Đưa ra lỗi nếu dấu thời gian kết quả không nằm trong phạm vi TIMESTAMP.

Ví dụ:

timestamp unit amount timestamp_add(timestamp, unit, amount)
2025-02-20 00:00:00 UTC "phút" 2L 2025-02-20 00:02:00 UTC
2025-02-20 00:00:00 UTC "giờ" -4L 20:00:00 ngày 19 tháng 2 năm 2025 (giờ UTC)
2025-02-20 00:00:00 UTC "day" 5L 00:00:00 ngày 25 tháng 2 năm 2025 (giờ UTC)

Web

const result = await execute(db.pipeline()
  .collection("documents")
  .select(
    field("createdAt").timestampAdd("day", 3653).as("expiresAt")
  )
);
Swift
let result = try await db.pipeline()
  .collection("documents")
  .select([
    Field("createdAt").timestampAdd(3653, .day).as("expiresAt")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("documents")
    .select(
        field("createdAt")
          .timestampAdd("day", 3653)
          .alias("expiresAt")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("documents")
    .select(
        field("createdAt").timestampAdd("day", 3653).alias("expiresAt")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("documents")
    .select(Field.of("createdAt").timestamp_add("day", 3653).as_("expiresAt"))
    .execute()
)

TIMESTAMP_SUB

Cú pháp:

timestamp_sub(timestamp: TIMESTAMP, unit: STRING, amount: INT64) -> TIMESTAMP

Nội dung mô tả:

Trừ amount của unit khỏi timestamp. Đối số amount có thể là số âm, trong trường hợp đó, đối số này tương đương với TIMESTAMP_ADD.

Đối số unit phải là một chuỗi và là một trong những giá trị sau:

  • microsecond
  • millisecond
  • second
  • minute
  • hour
  • day

Đưa ra lỗi nếu dấu thời gian kết quả không nằm trong phạm vi TIMESTAMP.

Ví dụ:

timestamp unit amount timestamp_sub(timestamp, unit, amount)
00:00, ngày 4 tháng 7 năm 2026, giờ UTC "phút" 40L 2026-07-03 23:20:00 UTC
00:00, ngày 4 tháng 7 năm 2026, giờ UTC "giờ" -24L 00:00:00 ngày 5 tháng 7 năm 2026 (giờ UTC)
00:00, ngày 4 tháng 7 năm 2026, giờ UTC "day" 3L 00:00:00 ngày 1 tháng 7 năm 2026 theo giờ UTC

Web

const result = await execute(db.pipeline()
  .collection("documents")
  .select(
    field("expiresAt").timestampSubtract("day", 14).as("sendWarningTimestamp")
  )
);
Swift
let result = try await db.pipeline()
  .collection("documents")
  .select([
    Field("expiresAt").timestampSubtract(14, .day).as("sendWarningTimestamp")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("documents")
    .select(
        field("expiresAt")
          .timestampSubtract("day", 14)
          .alias("sendWarningTimestamp")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("documents")
    .select(
        field("expiresAt").timestampSubtract("day", 14).alias("sendWarningTimestamp")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("documents")
    .select(
        Field.of("expiresAt")
        .timestamp_subtract("day", 14)
        .as_("sendWarningTimestamp")
    )
    .execute()
)

TIMESTAMP_TO_UNIX_MICROS

Cú pháp:

timestamp_to_unix_micros(input: TIMESTAMP) -> INT64

Nội dung mô tả:

Chuyển đổi input thành số lượng micro giây kể từ 1970-01-01 00:00:00 UTC. Cắt bớt các mức độ chính xác cao hơn bằng cách làm tròn xuống đầu của vi giây.

Ví dụ:

input timestamp_to_unix_micros(input)
00:00:00 ngày 1 tháng 1 năm 1970 (giờ UTC) 0L
1970-01-01 00:06:40.123456 UTC 400123456L
1969-12-31 23:59:59 UTC -1000000L

Web

const result = await execute(db.pipeline()
  .collection("documents")
  .select(
    field("dateString").timestampToUnixMicros().as("unixMicros")
  )
);
Swift
let result = try await db.pipeline()
  .collection("documents")
  .select([
    Field("dateString").timestampToUnixMicros().as("unixMicros")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("documents")
    .select(
        field("dateString").timestampToUnixMicros().alias("unixMicros")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("documents")
    .select(
        field("dateString").timestampToUnixMicros().alias("unixMicros")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("documents")
    .select(Field.of("dateString").timestamp_to_unix_micros().as_("unixMicros"))
    .execute()
)

TIMESTAMP_TO_UNIX_MILLIS

Cú pháp:

timestamp_to_unix_millis(input: TIMESTAMP) -> INT64

Nội dung mô tả:

Chuyển đổi input thành số mili giây kể từ 1970-01-01 00:00:00 UTC. Cắt bớt các mức độ chính xác cao hơn bằng cách làm tròn xuống đầu của mili giây.

Ví dụ:

input timestamp_to_unix_millis(input)
00:00:00 ngày 1 tháng 1 năm 1970 (giờ UTC) 0L
1970-01-01 01:06:40.123 UTC 4000123L
1969-12-31 23:43:20 -1000000L

Web

const result = await execute(db.pipeline()
  .collection("documents")
  .select(
    field("dateString").timestampToUnixMillis().as("unixMillis")
  )
);
Swift
let result = try await db.pipeline()
  .collection("documents")
  .select([
    Field("dateString").timestampToUnixMillis().as("unixMillis")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("documents")
    .select(
        field("dateString").timestampToUnixMillis().alias("unixMillis")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("documents")
    .select(
        field("dateString").timestampToUnixMillis().alias("unixMillis")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("documents")
    .select(Field.of("dateString").timestamp_to_unix_millis().as_("unixMillis"))
    .execute()
)

TIMESTAMP_TO_UNIX_SECONDS

Cú pháp:

timestamp_to_unix_seconds(input: TIMESTAMP) -> INT64

Nội dung mô tả:

Chuyển đổi input thành số giây kể từ 1970-01-01 00:00:00 UTC. Cắt bớt các mức độ chính xác cao hơn bằng cách làm tròn xuống đầu giây.

Ví dụ:

input timestamp_to_unix_seconds(input)
00:00:00 ngày 1 tháng 1 năm 1970 (giờ UTC) 0L
1970-01-01 00:01:00 UTC 60L
1969-12-31 23:55:00 UTC -300L

Web

const result = await execute(db.pipeline()
  .collection("documents")
  .select(
    field("dateString").timestampToUnixSeconds().as("unixSeconds")
  )
);
Swift
let result = try await db.pipeline()
  .collection("documents")
  .select([
    Field("dateString").timestampToUnixSeconds().as("unixSeconds")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("documents")
    .select(
        field("dateString").timestampToUnixSeconds().alias("unixSeconds")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("documents")
    .select(
        field("dateString").timestampToUnixSeconds().alias("unixSeconds")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("documents")
    .select(Field.of("dateString").timestamp_to_unix_seconds().as_("unixSeconds"))
    .execute()
)