Funkcje sygnatury czasowej
| Nazwa | Opis |
CURRENT_TIMESTAMP
|
Generuje TIMESTAMP odpowiadającą czasowi żądania.
|
TIMESTAMP_TRUNC
|
Obcina wartość TIMESTAMP do określonego poziomu szczegółowości.
|
UNIX_MICROS_TO_TIMESTAMP
|
Przekształca liczbę mikrosekund od 1970-01-01 00:00:00 UTC na TIMESTAMP.
|
UNIX_MILLIS_TO_TIMESTAMP
|
Przekształca liczbę milisekund od 1970-01-01 00:00:00 UTC na TIMESTAMP.
|
UNIX_SECONDS_TO_TIMESTAMP
|
Przekształca liczbę sekund od 1970-01-01 00:00:00 UTC na TIMESTAMP.
|
TIMESTAMP_ADD
|
Dodaje przedział czasu do TIMESTAMP
|
TIMESTAMP_SUB
|
Odejmuje przedział czasu od TIMESTAMP
|
TIMESTAMP_TO_UNIX_MICROS
|
Przekształca wartość TIMESTAMP na liczbę mikrosekund od 1970-01-01 00:00:00 UTC.
|
TIMESTAMP_TO_UNIX_MILLIS
|
Przekształca wartość TIMESTAMP na liczbę milisekund od 1970-01-01 00:00:00 UTC.
|
TIMESTAMP_TO_UNIX_SECONDS
|
Przekształca wartość TIMESTAMP na liczbę sekund od 1970-01-01 00:00:00 UTC
|
TIMESTAMP_DIFF
|
Zwraca liczbę całkowitą określonych interwałów unit między dwiema datami TIMESTAMP.
|
TIMESTAMP_EXTRACT
|
Wyodrębnia z TIMESTAMP określony part (np. rok, miesiąc, dzień).
|
CURRENT_TIMESTAMP
Składnia:
current_timestamp() -> TIMESTAMP
Opis:
Pobiera sygnaturę czasową na początku czasu żądania input (interpretowaną jako liczba mikrosekund od 1970-01-01 00:00:00 UTC).
Jest ona stała w ramach zapytania i zawsze zwraca tę samą wartość, jeśli jest wywoływana wielokrotnie.
TIMESTAMP_TRUNC
Składnia:
timestamp_trunc(timestamp: TIMESTAMP, granularity: STRING[, timezone: STRING]) -> TIMESTAMP
Opis:
Obcina sygnaturę czasową do określonego stopnia szczegółowości.
Argument granularity musi być ciągiem znaków i mieć jedną z tych wartości:
microsecondmillisecondsecondminutehourdayweekweek([weekday])monthquarteryearisoyear
Jeśli podany jest argument timezone, obcięcie będzie oparte na granicach kalendarza danej strefy czasowej (np. obcięcie do dnia spowoduje obcięcie do północy w danej strefie czasowej). Obcięcie będzie uwzględniać czas letni.
Jeśli nie podasz wartości timezone, obcinanie będzie się odbywać na podstawie granic kalendarza UTC.
Argument timezone powinien być ciągiem znaków reprezentującym strefę czasową z bazy danych tz, np. America/New_York. Możesz też użyć niestandardowego przesunięcia czasowego, określając przesunięcie od GMT.
Przykłady:
timestamp |
granularity |
timezone |
timestamp_trunc(timestamp, granularity, timezone) |
|---|---|---|---|
| 2000-01-01 10:20:30:123456 UTC | „second” | Nie podano | 2001-01-01 10:20:30 UTC |
| 1997-05-31 04:30:30 UTC | „day” | Nie podano | 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) | Nie podano | 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 | „miesiąc” | „GMT+06:32:43” | 2026-01-01T06:32:43 UTC |
UNIX_MICROS_TO_TIMESTAMP
Składnia:
unix_micros_to_timestamp(input: INT64) -> TIMESTAMP
Opis:
Konwertuje wartość input (interpretowaną jako liczbę mikrosekund od 1970-01-01 00:00:00 UTC) na wartość TIMESTAMP. Zwraca błąd error, jeśli nie można przekonwertować wartości input na prawidłową wartość TIMESTAMP.
Przykłady:
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 |
Node.js
const result = await db.pipeline() .collection("documents") .select( field("createdAtMicros").unixMicrosToTimestamp().as("createdAtString") ) .execute();
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() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("documents") .select(unixMicrosToTimestamp(field("createdAtMicros")).as("createdAtString")) .execute() .get();
UNIX_MILLIS_TO_TIMESTAMP
Składnia:
unix_millis_to_timestamp(input: INT64) -> TIMESTAMP
Opis:
Przekształca wartość input (interpretowaną jako liczbę milisekund od 1970-01-01 00:00:00 UTC) w wartość TIMESTAMP. Zwraca błąd error, jeśli nie można przekonwertować wartości input na prawidłową wartość TIMESTAMP.
Przykłady:
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 |
Node.js
const result = await db.pipeline() .collection("documents") .select( field("createdAtMillis").unixMillisToTimestamp().as("createdAtString") ) .execute();
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() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("documents") .select(unixMillisToTimestamp(field("createdAtMillis")).as("createdAtString")) .execute() .get();
UNIX_SECONDS_TO_TIMESTAMP
Składnia:
unix_seconds_to_timestamp(input: INT64) -> TIMESTAMP
Opis:
Konwertuje wartość input (interpretowaną jako liczbę sekund od 1970-01-01 00:00:00 UTC) na wartość TIMESTAMP. Zwraca błąd error, jeśli nie można przekonwertować wartości input na prawidłową wartość TIMESTAMP.
Przykłady:
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 |
Node.js
const result = await db.pipeline() .collection("documents") .select( field("createdAtSeconds").unixSecondsToTimestamp().as("createdAtString") ) .execute();
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() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("documents") .select(unixSecondsToTimestamp(field("createdAtSeconds")).as("createdAtString")) .execute() .get();
TIMESTAMP_ADD
Składnia:
timestamp_add(timestamp: TIMESTAMP, unit: STRING, amount: INT64) -> TIMESTAMP
Opis:
Dodaje amount unit z timestamp. Argument amount może być ujemny. W takim przypadku jest on równoważny funkcji TIMESTAMP_SUB.
Argument unit musi być ciągiem znaków i mieć jedną z tych wartości:
microsecondmillisecondsecondminutehourday
Zwraca błąd, jeśli wynikowa sygnatura czasowa nie mieści się w zakresie TIMESTAMP.
Przykłady:
timestamp |
unit |
amount |
timestamp_add(timestamp, unit, amount) |
|---|---|---|---|
| 2025-02-20 00:00:00 UTC | „minuta” | 2L | 2025-02-20 00:02:00 UTC |
| 2025-02-20 00:00:00 UTC | „godzina” | -4L | 2025-02-19 20:00:00 UTC |
| 2025-02-20 00:00:00 UTC | „day” | 5L | 2025-02-25 00:00:00 UTC |
Node.js
const result = await db.pipeline() .collection("documents") .select( field("createdAt").timestampAdd("day", 3653).as("expiresAt") ) .execute();
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() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("documents") .select(timestampAdd(field("createdAt"), "day", 3653).as("expiresAt")) .execute() .get();
TIMESTAMP_SUB
Składnia:
timestamp_sub(timestamp: TIMESTAMP, unit: STRING, amount: INT64) -> TIMESTAMP
Opis:
Odejmuje od timestamp wartość amount równą unit. Argument amount może być ujemny. W takim przypadku jest on równoważny funkcji TIMESTAMP_ADD.
Argument unit musi być ciągiem znaków i mieć jedną z tych wartości:
microsecondmillisecondsecondminutehourday
Zwraca błąd, jeśli wynikowa sygnatura czasowa nie mieści się w zakresie TIMESTAMP.
Przykłady:
timestamp |
unit |
amount |
timestamp_sub(timestamp, unit, amount) |
|---|---|---|---|
| 2026-07-04 00:00:00 UTC | „minuta” | 40L | 2026-07-03 23:20:00 UTC |
| 2026-07-04 00:00:00 UTC | „godzina” | -24L | 2026-07-05 00:00:00 UTC |
| 2026-07-04 00:00:00 UTC | „day” | 3L | 2026-07-01 00:00:00 UTC |
Node.js
const result = await db.pipeline() .collection("documents") .select( field("expiresAt").timestampSubtract("day", 14).as("sendWarningTimestamp") ) .execute();
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() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("documents") .select(timestampSubtract(field("expiresAt"), "day", 14).as("sendWarningTimestamp")) .execute() .get();
TIMESTAMP_TO_UNIX_MICROS
Składnia:
timestamp_to_unix_micros(input: TIMESTAMP) -> INT64
Opis:
Konwertuje input na liczbę mikrosekund od 1970-01-01 00:00:00 UTC. Obcina wyższe poziomy precyzji, zaokrąglając w dół do początku mikrosekundy.
Przykłady:
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 |
Node.js
const result = await db.pipeline() .collection("documents") .select( field("dateString").timestampToUnixMicros().as("unixMicros") ) .execute();
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() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("documents") .select(timestampToUnixMicros(field("dateString")).as("unixMicros")) .execute() .get();
TIMESTAMP_TO_UNIX_MILLIS
Składnia:
timestamp_to_unix_millis(input: TIMESTAMP) -> INT64
Opis:
Przekształca wartość input na liczbę milisekund od 1970-01-01 00:00:00 UTC. Obcina wyższe poziomy precyzji, zaokrąglając w dół do początku milisekundy.
Przykłady:
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 |
Node.js
const result = await db.pipeline() .collection("documents") .select( field("dateString").timestampToUnixMillis().as("unixMillis") ) .execute();
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() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("documents") .select(timestampToUnixMillis(field("dateString")).as("unixMillis")) .execute() .get();
TIMESTAMP_TO_UNIX_SECONDS
Składnia:
timestamp_to_unix_seconds(input: TIMESTAMP) -> INT64
Opis:
Przekształca wartość input w liczbę sekund od 1970-01-01 00:00:00 UTC. Obcina wyższe poziomy precyzji, zaokrąglając w dół do początku sekundy.
Przykłady:
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 |
Node.js
const result = await db.pipeline() .collection("documents") .select( field("dateString").timestampToUnixSeconds().as("unixSeconds") ) .execute();
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() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("documents") .select(timestampToUnixSeconds(field("dateString")).as("unixSeconds")) .execute() .get();
TIMESTAMP_DIFF
Składnia:
timestamp_diff(end: TIMESTAMP, start: TIMESTAMP, unit: STRING) -> INT64
Opis:
Zwraca liczbę całkowitą określonych interwałów unit między dwiema datami TIMESTAMP.
- Zwraca wartość ujemną, jeśli
endwystępuje przedstart. - Obcina ułamkowe jednostki. Na przykład
timestamp_diff("2021-01-01 00:00:01", "2021-01-01 00:00:00", "minute")zwraca0.
Argument unit musi być ciągiem znaków i mieć jedną z tych wartości:
microsecondmillisecondsecondminutehourday
Przykłady:
end |
start |
unit |
timestamp_diff(end, start, unit) |
|---|---|---|---|
| 2026-07-04 00:01:00 UTC | 2026-07-04 00:00:00 UTC | „second” | 60L |
| 2026-07-04 00:00:00 UTC | 2026-07-05 00:00:00 UTC | „day” | -1L |
| 2026-07-04 00:00:59 UTC | 2026-07-04 00:00:00 UTC | „minuta” | 0L |
TIMESTAMP_EXTRACT
Składnia:
timestamp_extract(timestamp: TIMESTAMP, part: STRING[, timezone: STRING]) -> INT64
Opis:
Wyodrębnia z daty timestamp określony part (np. rok, miesiąc, dzień).
Argument part musi być ciągiem znaków i mieć jedną z tych wartości:
microsecondmillisecondsecondminutehourdaydayofweek: zwraca wartość od 1 (niedziela) do 7 (sobota).dayofyearweek: zwraca numer tygodnia w roku, zaczynając od 1 w przypadku pierwszej niedzieli w roku.week([weekday]): zwraca numer tygodnia w roku, zaczynając od określonegoweekday.monthquarteryearisoweek: zwraca numer tygodnia zgodnie z normą ISO 8601.isoyear: zwraca rok numeracji tygodni zgodnie z normą ISO 8601.
Jeśli podasz argument timezone, wyodrębnianie będzie oparte na kalendarzu danej strefy czasowej. Wyodrębnianie będzie uwzględniać czas letni.
Jeśli nie podasz wartości timezone, wyodrębnianie będzie oparte na wartości UTC.
Argument timezone powinien być ciągiem znaków reprezentującym strefę czasową z bazy danych stref czasowych, np. America/New_York. Możesz też użyć niestandardowego przesunięcia czasowego, określając przesunięcie od GMT.
Przykłady:
timestamp |
part |
timezone |
timestamp_extract(timestamp, part, timezone) |
|---|---|---|---|
| 2025-02-20 10:20:30 UTC | „year” | Nie podano | 2025 |
| 2025-02-20 10:20:30 UTC | „day” | Nie podano | 20 |
| 2025-12-31 23:59:59 UTC | „year” | „Asia/Tokyo” | 2026 |