タイムスタンプ関数

タイムスタンプ関数

名前 説明
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 TIMESTAMP1970-01-01 00:00:00 UTC からのマイクロ秒数に変換します。
TIMESTAMP_TO_UNIX_MILLIS TIMESTAMP1970-01-01 00:00:00 UTC からのミリ秒数に変換します。
TIMESTAMP_TO_UNIX_SECONDS TIMESTAMP1970-01-01 00:00:00 UTC からの秒数に変換します。

CURRENT_TIMESTAMP

構文:

current_timestamp() -> TIMESTAMP

説明:

リクエスト時間 input1970-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 引数が指定されている場合、切り捨ては指定されたタイムゾーンのカレンダー境界に基づいて行われます(例: 日単位の切り捨てでは、指定されたタイムゾーンの午前 0 時まで切り捨てます)。切り捨てでは夏時間が考慮されます。

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

説明:

input1970-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

説明:

input1970-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

説明:

input1970-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 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

構文:

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

説明:

timestampunit 単位で amount を加算します。amount 引数は負の数にできます。その場合、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 から unit 単位で amount を減算します。amount 引数は負の数にできます。その場合、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

説明:

input1970-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

説明:

input1970-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

説明:

input1970-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 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()
)