Skip to content

Commit

Permalink
Added the inLocalTimeZone parameter to the prepareResponse function. …
Browse files Browse the repository at this point in the history
…This defaults to false (existing behaviour) so is backwards compatible.

When inLocalTimeZone = false, dates are returned in the format: "endDate":"2022-12-22T16:51:30.000Z"
When inLocalTimeZone = true, dates are returned in the format: "endDate":"2022-12-22T11:51:30.000-0500"

This allows performance improvements, because you don't have to perform further date processing on the values to convert them back into the local time zone.

This PR adds the flag as a parameter to getSleepSamples. It can easily be added to other functions as required.
  • Loading branch information
gearnshaw committed Aug 10, 2023
1 parent db8fca9 commit 14bad8a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
4 changes: 3 additions & 1 deletion index.android.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,11 @@ declare module 'react-native-google-fit' {
/**
* Get the sleep sessions over a specified date range.
* @param {Object} options getSleepData accepts an options object containing required startDate: ISO8601Timestamp and endDate: ISO8601Timestamp.
* @param inLocalTimeZone return start and end dates in local time zone rather than converting to UTC.
*/
getSleepSamples: (
options: Partial<StartAndEndDate>
options: Partial<StartAndEndDate>,
inLocalTimeZone: boolean
) => Promise<SleepSampleResponse[]>

saveSleep: (
Expand Down
5 changes: 3 additions & 2 deletions index.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,17 +689,18 @@ class RNGoogleFit {
/**
* Get the sleep sessions over a specified date range.
* @param {Object} options getSleepData accepts an options object containing required startDate: ISO8601Timestamp and endDate: ISO8601Timestamp.
* @param inLocalTimeZone return start and end dates in local time zone rather than converting to UTC
*/

getSleepSamples = async (options) => {
getSleepSamples = async (options, inLocalTimeZone = false) => {
const { startDate, endDate } = prepareInput(options);

const result = await googleFit.getSleepSamples(
startDate,
endDate
);

return prepareResponse(result, "addedBy");
return prepareResponse(result, "addedBy", inLocalTimeZone);
}

saveSleep = async (options) => {
Expand Down
11 changes: 8 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function prepareInput(options) {
return { startDate, endDate, bucketInterval, bucketUnit };
}

export function prepareResponse(response, byKey = 'value') {
export function prepareResponse(response, byKey = 'value', inLocalTimeZone = false) {
return response
.map(el => {
if (!isNil(el[byKey])) {
Expand All @@ -57,8 +57,13 @@ export function prepareResponse(response, byKey = 'value') {
// the Chrome V8 debugger, but not on device. Using momentJS here to get around this issue.
// el.startDate = new Date(el.startDate).toISOString()
// el.endDate = new Date(el.endDate).toISOString()
el.startDate = moment(el.startDate).toISOString()
el.endDate = moment(el.endDate).toISOString()
if (inLocalTimeZone) {
el.startDate = moment.parseZone(el.startDate).toISOString(true)
el.endDate = moment.parseZone(el.endDate).toISOString(true)
} else {
el.startDate = moment(el.startDate).toISOString()
el.endDate = moment(el.endDate).toISOString()
}
return el
}
})
Expand Down

0 comments on commit 14bad8a

Please sign in to comment.