Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid End Time when using new Date().toISOString() #363

Open
ceebows opened this issue Jun 19, 2024 · 1 comment
Open

Invalid End Time when using new Date().toISOString() #363

ceebows opened this issue Jun 19, 2024 · 1 comment

Comments

@ceebows
Copy link

ceebows commented Jun 19, 2024

We follow the options requirements for sampling data.

There are times were for unknown reasons we receive these error reports in our Firebase Crashyltics despite using the correct type for endDate, startDate, etc.

Options object used:

const endDate = new Date().toISOString();
...
const options = {
                    unit: 'kg', // required; default 'kg'
                    startDate: startDate, // always an ISO String we receive as func parameter
                    endDate: endDate, // required
                    bucketUnit: BucketUnit.DAY, // optional - default "DAY". Valid values: "NANOSECOND" | "MICROSECOND" | "MILLISECOND" | "SECOND" | "MINUTE" | "HOUR" | "DAY"
                    bucketInterval: 1, // optional - default 1.
                    ascending: false // optional; default false
                };

It seems like we end up getting endTime in milliseconds since EPOCH based on crash log.
The default start date is a time very close to epoch, around Thu, 15 Jan 1970 06:32:06 GMT

Body History snippet:

public ReadableArray getHistory(long startTime, long endTime, int bucketInterval, String bucketUnit) {
        // for height we need to take time, since GoogleFit foundation - https://stackoverflow.com/questions/28482176/read-the-height-in-googlefit-in-android
        startTime = this.dataType == DataType.TYPE_WEIGHT ? startTime : 1401926400;
        DataReadRequest.Builder readRequestBuilder = new DataReadRequest.Builder()
                .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS);

        if (this.dataType == DataType.TYPE_WEIGHT) {

Crash:
Fatal Exception: java.lang.IllegalStateException: Invalid end time: 1718648023783
at com.google.android.gms.common.internal.Preconditions.checkState(com.google.android.gms:play-services-basement@@18.3.0:3)
at com.google.android.gms.fitness.request.DataReadRequest$Builder.build(com.google.android.gms:play-services-fitness@@21.2.0:8)
at com.reactnative.googlefit.BodyHistory.getHistory(BodyHistory.java:99)
at com.reactnative.googlefit.GoogleFitModule.getWeightSamples(GoogleFitModule.java:214)
at java.lang.reflect.Method.invoke(Method.java)
at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372)
at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:149)
at com.facebook.jni.NativeRunnable.run(NativeRunnable.java)
at android.os.Handler.handleCallback(Handler.java:959)
at android.os.Handler.dispatchMessage(Handler.java:100)
at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:29)
at android.os.Looper.loopOnce(Looper.java:232)
at android.os.Looper.loop(Looper.java:317)
at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:234)
at java.lang.Thread.run(Thread.java:1012)

@shmkane
Copy link

shmkane commented Jul 11, 2024

We've been using date.toString() instead of toISOString.

  const options = {
    startDate: startDate.toString(),
    endDate: endDate.toString(),
    ...dataTypeParams.additionalOptions,
  };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants