1

Trying to get the server timestamp at the CreatedAt document field before the document is added to the cloud. So it could be applied to the Time property and also saved locally, However this is what I get: Document Properties once saved on cloud.

My ChatTestMessageGroup Model

public class ChatTestMessageGroup : TimeTest
{
    public long MessageId { get; set; }
    public string Message { get; set; }
    public string Time { get; set; }
}

public class TimeTest
{
    [ServerTimestamp]
    public Timestamp CreatedAt { get; set; }
    public Timestamp StampAt { get; set; }
}

Send Message Execution code

private async Task SendTestMessageAsync()
{
    if (string.IsNullOrWhiteSpace(Message)) return;

    // under testing.
    var test = new ChatTestMessageGroup()
    {
        MessageId = new Timestamp().ToDateTime().Ticks,
        Message = Message.Trim(),
    };
    test.Time = $"{test.CreatedAt.ToDateTime():HH:mm}";
    await CloudService.CS.SendMessageAsync(test);
    Message = "";
}

Write To Cloud Code

public async Task SendMessageAsync(ChatTestMessageGroup testMessage)
{
    IDocumentReference doc = CrossCloudFirestore.Current.Instance.Collection("Testing").Document(DateTime.Now.Day.ToString());
        
    await doc.Collection("Tester").Document(${testMessage.CreatedAt.Seconds}").SetAsync(testMessage);
}

The plugin I'm using Plugin.CloudFirestore

Pascal Jk
  • 23
  • 4
  • See [Add timestamp in Firestore documents](https://stackoverflow.com/q/51846914/199364). If you want to store the value locally, you'll need to read the document back afterwards. – ToolmakerSteve May 25 '22 at 01:35

1 Answers1

1

Trying to get the server timestamp at the CreatedAt document field before the document is added to the cloud.

This is not possible. The time is taken at the server the moment the document is created. The client app can't be certain what the time is on the server because its own clock could be wrong. You can only get the timestamp that was written by reading the document back.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441