react-native-google-fit icon indicating copy to clipboard operation
react-native-google-fit copied to clipboard

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

Open ceebows opened this issue 1 year ago • 1 comments

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)

ceebows avatar Jun 19 '24 12:06 ceebows

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

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

shmkane avatar Jul 11 '24 16:07 shmkane