Skip to content

Commit 8422b15

Browse files
committed
add currentTimestamp()
1 parent 2fb7a14 commit 8422b15

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import Foundation
16+
17+
/// An expression that represents a server-side timestamp.
18+
///
19+
/// `CurrentTimestamp` is used to generate a timestamp on the server.
20+
/// This is useful for recording current date and time.
21+
///
22+
/// Example:
23+
/// ```swift
24+
/// CurrentTimestamp().as("createdAt")
25+
/// ```
26+
public class CurrentTimestamp: FunctionExpression, @unchecked Sendable {
27+
public init() {
28+
super.init("current_timestamp", [])
29+
}
30+
}

Firestore/Swift/Tests/Integration/PipelineTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3157,6 +3157,21 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
31573157
}
31583158
}
31593159

3160+
func testCurrentTimestampWorks() async throws {
3161+
let collRef = collectionRef(withDocuments: ["doc1": ["foo": 1]])
3162+
let db = collRef.firestore
3163+
3164+
let pipeline = db.pipeline()
3165+
.collection(collRef.path)
3166+
.select([
3167+
CurrentTimestamp().as("timestamp"),
3168+
])
3169+
3170+
let snapshot = try await pipeline.execute()
3171+
3172+
XCTAssertEqual(snapshot.results.count, 1)
3173+
}
3174+
31603175
func testSupportsByteLength() async throws {
31613176
let db = firestore()
31623177
let randomCol = collectionRef()

0 commit comments

Comments
 (0)