Time added to Firestore's Timestamp upon adding a document - datetime

I have Firestore documents that use Timestamp. I've checked every part of my code and I'm 100% sure the Instants I use to create timestamp always contain only dates like 2020-05-01T00:00:00Z. However, when I look a the https://console.firebase.google.com I see strange things. Each timestamp has suddenly additional time in it. It's either 1:00:00 AM UTC+1 or 2:00:00 AM UTC+2.
In order to turn Instant to Timestamp I use this extension:
fun Instant.toTimestamp(): Timestamp {
return Timestamp.ofTimeSecondsAndNanos(this.epochSecond, this.nano)
}
How is it possible that correct data on the client side becomes incorrect when saved? What am I missing here?
I use batch updates and I also checked the mutations it contains and everything is fine there too. I converted all its timestamps back to Instants and none of them contained time. Why do they appear in the console?

Timestamps don't encode a timezone - they are always UTC. However, the Firebase web console displays timestamps using the timezone configuration of the local machine running the browser (for local readability). If you query for the document and get the timestamp back out, it will be the same as the time you put in (again, internally represented in UTC).

Related

Firebase cloud function timestamp

In my current application, I store a value in Firestore for each user something along the lines of this:
User1Doc - hasUsedFeatureToday = true
User2Doc - hasUsedFeatureToday = false
...
At the end of the day, I run a scheduled function that resets all of these to false. This was fine while my application was relatively small, but does not scale very effectively as I'm sure you can imagine.
Each user can only use this aspect of my app once per day, so the only time this field is read is when they try to use it.
I would like to change this system to store a timestamp in the user's document when they use the feature and then check if this timestamp is the same day (Europe/London time) if someone tries to use it again.
Does Firebase offer a way to get a "timezoned" timestamp like this and store/check it with the Firestore?
You can just store a timestamp (UTC). Whenever users logs in to your app, just check the timestamp and update the same. You can always use libraries like Luxon to get local time from the UTC time.
If you want to allow the user to update this timestamp only once, you can use security rules to restrict the same. However, user may try to prevent the timestamp from being updated at first place.
You can instead use Cloud Function to serve the data only when the user requests it. This will be better than updating documents of all users even if they won't be using the feature every day.

Timestamp field in cosmos db

I am having a hard time using ts field in cosmos/document db with c#. Is it just better to create another date time field and use that instead of ts? How to decide when to use ts vs a custom date time field?
_ts represents a Unix Epoch and marks the time of the document's last modification. When a document is created, it has the creation time, but if later on the document is updated, then _ts is updated with that time.
Effectively it means the last modified time of the document.
It depends on your use case if you can use it or you need a custom date field. For example, if your intent it to track when a document was created, then you can't use it, as the value will change if the document is modified. If your intent is to track the last modified time, it can work (depends on the precision you need and if a Unix Epoch fills that requirement, otherwise you would need your own custom field).
Here is the official reference of system properties: https://learn.microsoft.com/azure/cosmos-db/account-databases-containers-items#properties-of-an-item

How to get the local time correctly in Functions after the change in Firebase 5.4.1?

After the change in the new version of Firebase 5.4.1. for web, when I send a timestamp like
const timestamp = {seconds: 1534183200, nanoseconds: 0}
to the server in my Functions, I can't convert it to local time that the client is in as timestamp has 0 timeZoneOffset. Do I need to keep track of timezones per client now or is there a way that I am missing?
Timestamps don't have timezones. They just measure on offset from unix epoch time (since Jan 1, 1970).
If you want a timezone, you should get that from the user's device, or ask them for their preference. Then you can use that to format the timestamp to their locale. This is typically done on the client app, not on the backend.

How does Firestore save dates?

I've recently discovered firestore.Timestamp and so I tried to assign firestore.FieldValue.serverTimestamp(); to one and it didn't work. instead it says Type 'FieldValue' is not assignable to type 'Timestamp' which I would say is strange. Shouldn't serverTimestamp() be a Timestamp and not a FieldValue? What would be the purpose of firestore.Timestamp if not to save timestamps to Firestore? Is there a way to get a server timestamp that is compatible with firestore.Timestamp or should I be avoiding timestamps altogether, and just stick to firestore.FieldValue for all my dates?
serverTimestamp() just returns sentinel value that tells the Firestore server that it should use the current time on the server as the value for the field you're trying to set. It doesn't return an actual Timestamp object itself.
The reason why you'd use this is to make sure that dates are being set consistently on the server instead of depending on the clock being correct on the user's device.
If you want to know the current time on the device, just use the native date/time objects provided by the language or operating system.

Solr does not retrieve the date correctly

I have some documents indexed in Solr that contain some timestamps, like that:
2017-10-21T11:53:33Z.
When I perform queries form Solr Admin UI, I get the correct information (the exact timestamps).
However, when I perform queries from SolrJ, I get the following timestamps:
Sat Oct 21 14:53:33 EEST 2017
The field that stores this timestamp has the following type: org.apache.solr.schema.TrieDateField
So the format and the timezone is changed. I read this post and this one and I understand that SolrJ takes into consideration the local timezone when it retrieves dates, but why is this happening?
Also, the format is changed and I don't want that! I want the exact same format that I input when I index dates to Solr.
How can I change this date format when I retrieve dates from Solr using SolrJ and how can I retrieve the exact values (the exact timezone) that I can see when I use Solr Admin UI?
SolrJ parses the response from Solr, converts string "2017-10-21T11:53:33Z" to a Date object and returns you it.
By default, the timezone is printed in your local timezone (EEST), so it just a representation, but the date/time is the same. You can convert it and display in any timezone.
For example see this answer: https://stackoverflow.com/a/22364501/937970
Another option is to set a global timezone for your JVM, but this might affect the whole application:
java -Duser.timezone=UTC ...
When you make a query using Solr Admin UI it just returns you raw values, that's why you see the UTC value.

Resources