時間戳記函式

時間戳記函式

名稱 說明
CURRENT_TIMESTAMP 產生與要求時間相應的 TIMESTAMP
TIMESTAMP_TRUNC TIMESTAMP 截斷至指定精細程度。
UNIX_MICROS_TO_TIMESTAMP 將自 1970-01-01 00:00:00 UTC 算起的微秒數轉換為 TIMESTAMP
UNIX_MILLIS_TO_TIMESTAMP 將自 1970-01-01 00:00:00 UTC 起算的毫秒數轉換為 TIMESTAMP
UNIX_SECONDS_TO_TIMESTAMP 將自 1970-01-01 00:00:00 UTC 起的秒數轉換為 TIMESTAMP
TIMESTAMP_ADD TIMESTAMP 中加上時間間隔
TIMESTAMP_SUB TIMESTAMP 中減去時間間隔
TIMESTAMP_TO_UNIX_MICROS TIMESTAMP 轉換為自 1970-01-01 00:00:00 UTC 算起的微秒數
TIMESTAMP_TO_UNIX_MILLIS TIMESTAMP 轉換為自 1970-01-01 00:00:00 UTC 算起的毫秒數
TIMESTAMP_TO_UNIX_SECONDS TIMESTAMP 轉換為自 1970-01-01 00:00:00 UTC 以來的秒數

CURRENT_TIMESTAMP

語法:

current_timestamp() -> TIMESTAMP

說明:

取得要求時間 input 開始時的時間戳記 (解讀為自 1970-01-01 00:00:00 UTC 起算的微秒數)。

這在查詢中是穩定的,多次呼叫時一律會解析為相同的值。

TIMESTAMP_TRUNC

語法:

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

說明:

將時間戳記截斷至指定精細程度。

granularity 引數必須是字串,且為下列其中一項:

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

如果提供 timezone 引數,截斷作業會以指定時區的日曆界線為準 (例如,截斷日期會截斷至指定時區的午夜)。截斷時間時會考量日光節約時間。

如未提供 timezone,系統會根據 UTC 日曆界線截斷。

timezone 引數應為 tz 資料庫中時區的字串表示法,例如 America/New_York。您也可以指定 GMT 的偏移量,使用自訂時間偏移。

範例:

timestamp granularity timezone timestamp_trunc(timestamp, granularity, timezone)
2000-01-01 10:20:30:123456 UTC 「second」 未提供 2001-01-01 10:20:30 UTC
1997-05-31 04:30:30 UTC "day" 未提供 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) 未提供 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
2026-01-24 20:00:00 UTC 「month」 「GMT+06:32:43」 2026-01-01T06:32:43 UTC

UNIX_MICROS_TO_TIMESTAMP

語法:

unix_micros_to_timestamp(input: INT64) -> TIMESTAMP

說明:

input (解讀為自 1970-01-01 00:00:00 UTC 算起的微秒數) 轉換為 TIMESTAMP。如果無法將 input 轉換為有效的 TIMESTAMP,則會擲回 error

範例:

input unix_micros_to_timestamp(input)
0L 1970-01-01 00:00:00 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

語法:

unix_millis_to_timestamp(input: INT64) -> TIMESTAMP

說明:

input (解讀為自 1970-01-01 00:00:00 UTC 算起的毫秒數) 轉換為 TIMESTAMP。如果無法將 input 轉換為有效的 TIMESTAMP,則會擲回 error

範例:

input unix_millis_to_timestamp(input)
0L 1970-01-01 00:00:00 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

語法:

unix_seconds_to_timestamp(input: INT64) -> TIMESTAMP

說明:

input (解讀為自 1970-01-01 00:00:00 UTC 算起的秒數) 轉換為 TIMESTAMP。如果無法將 input 轉換為有效的 TIMESTAMP,則會擲回 error

範例:

input unix_seconds_to_timestamp(input)
0L 1970-01-01 00:00:00 UTC
60L 世界標準時間 1970-01-01 00:01:00
-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

語法:

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

說明:

timestamp 新增 unitamountamount 引數可以是負數,在這種情況下,這相當於 TIMESTAMP_SUB

unit 引數必須是字串,且為下列其中一項:

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

如果產生的時間戳記不符合 TIMESTAMP 範圍,就會擲回錯誤。

範例:

timestamp unit amount timestamp_add(timestamp, unit, amount)
2025-02-20 00:00:00 UTC 「minute」 2L 2025-02-20 00:02:00 UTC
2025-02-20 00:00:00 UTC 「hour」 -4L 2025-02-19 20:00:00 UTC
2025-02-20 00:00:00 UTC "day" 5L 2025-02-25 00:00:00 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

語法:

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

說明:

timestamp 減去 amountunitamount 引數可以是負數,在這種情況下,這等同於 TIMESTAMP_ADD

unit 引數必須是字串,且為下列其中一項:

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

如果產生的時間戳記不符合 TIMESTAMP 範圍,就會擲回錯誤。

範例:

timestamp unit amount timestamp_sub(timestamp, unit, amount)
2026-07-04 00:00:00 UTC 「minute」 40L 2026-07-03 23:20:00 UTC
2026-07-04 00:00:00 UTC 「hour」 -24L 2026-07-05 00:00:00 UTC
2026-07-04 00:00:00 UTC "day" 3L 2026-07-01 00:00:00 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

語法:

timestamp_to_unix_micros(input: TIMESTAMP) -> INT64

說明:

input 轉換為自 1970-01-01 00:00:00 UTC 算起的微秒數。會將較高層級的精確度向下捨入至微秒的開頭。

範例:

input timestamp_to_unix_micros(input)
1970-01-01 00:00:00 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

語法:

timestamp_to_unix_millis(input: TIMESTAMP) -> INT64

說明:

input 轉換為自 1970-01-01 00:00:00 UTC 算起的毫秒數。會截斷較高層級的精確度,並向下捨入至毫秒的開頭。

範例:

input timestamp_to_unix_millis(input)
1970-01-01 00:00:00 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

語法:

timestamp_to_unix_seconds(input: TIMESTAMP) -> INT64

說明:

input 轉換為自 1970-01-01 00:00:00 UTC 起的秒數。將較高層級的精確度向下捨入至秒數開頭,藉此截斷較高層級的精確度。

範例:

input timestamp_to_unix_seconds(input)
1970-01-01 00:00:00 UTC 0L
世界標準時間 1970-01-01 00:01:00 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()
)