Skip to content

feat(firestore): Add timestamp expressions#7826

Open
yvonnep165 wants to merge 8 commits intomainfrom
yvonne/timestamp-expressions
Open

feat(firestore): Add timestamp expressions#7826
yvonnep165 wants to merge 8 commits intomainfrom
yvonne/timestamp-expressions

Conversation

@yvonnep165
Copy link
Contributor

Add new timestamp expressions timestamp_diff and timestamp_extract and integration tests for the expressions

The expression timestamp_trunc was already ported in the repo with integration tests. Amend the example usage documentation for standalone methods.

Ported from firebase/firebase-js-sdk#9728

@yvonnep165 yvonnep165 requested a review from a team as a code owner March 19, 2026 18:20
@product-auto-label product-auto-label bot added the api: firestore Issues related to the Firestore API. label Mar 19, 2026
@yvonnep165 yvonnep165 requested review from dlarocque and milaGGL March 19, 2026 18:21
Comment on lines +2462 to +2481
timestampDiff(
start: string | Expression,
unit: 'microsecond' | 'millisecond' | 'second' | 'minute' | 'hour' | 'day'
): FunctionExpression;
timestampDiff(
start: string | Expression,
unit:
| 'microsecond'
| 'millisecond'
| 'second'
| 'minute'
| 'hour'
| 'day'
| Expression
): FunctionExpression {
return new FunctionExpression(
'timestamp_diff',
[this, fieldOrExpression(start), valueToDefaultExpr(unit)],
);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move the union string union values into a type?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since existing timestampAdd and timestampSub expressions use the same unit value, will do refactoring in a separate PR after this one.

Comment on lines +2483 to +2501
/**
* @beta
* Creates an expression that extracts a specified part from this timestamp expression.
*
* @example
* ```typescript
* // Extract the year from the 'createdAt' field.
* field('createdAt').timestampExtract('year')
* ```
*
* @param part - The part to extract from the timestamp (e.g., "year", "month", "day").
* @param timezone - The timezone to use for extraction. Valid values are from
* the TZ database (e.g., "America/Los_Angeles") or in the format "Etc/GMT-1."
* @returns A new `Expression` representing the extracted part as an integer.
*/
timestampExtract(
part: firestore.Pipelines.TimePart,
timezone?: string | Expression
): FunctionExpression;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should add documentation explaining how this expression behaves when timestamp is not passed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add "Defaults to "UTC" if not specified." in the timezone parameter discription

part: firestore.Pipelines.TimePart | Expression,
timezone?: string | Expression
): FunctionExpression {
const internalPart = isString(part) ? part.toLowerCase() : part;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove this line, since TimePart should already be lower case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TimePart includes all the values from TimeGranularity and at this moment TimeGranularity still have value that's not lower case like isoWeek, which I plan to fix when refactoring the unit variables as mentioned above. We can remove it after I change all the values to lower case?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: firestore Issues related to the Firestore API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants