Skip to content

Commit 3f9be54

Browse files
Merge pull request #43 from developmentseed/feature/40-dates
Update date handling
2 parents a4b4881 + 7bf5366 commit 3f9be54

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

src/hooks/useStacSearch.test.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,10 @@ describe('useStacSearch — API supports POST', () => {
188188
await waitFor(() => expect(result.current.results).toEqual({ data: '12345' }));
189189
// Validate POST payload
190190
const postPayload = parseRequestPayload(fetch.mock.calls[1][1]);
191-
expect(postPayload).toEqual({ datetime: '2022-01-17/2022-05-17', limit: 25 });
191+
expect(postPayload).toEqual({
192+
datetime: '2022-01-17T00:00:00Z/2022-05-17T23:59:59Z',
193+
limit: 25,
194+
});
192195
});
193196

194197
it('includes open date range in search (no to-date)', async () => {
@@ -215,7 +218,7 @@ describe('useStacSearch — API supports POST', () => {
215218
await waitFor(() => expect(result.current.results).toEqual({ data: '12345' }));
216219
// Validate POST payload
217220
const postPayload = parseRequestPayload(fetch.mock.calls[1][1]);
218-
expect(postPayload).toEqual({ datetime: '2022-01-17/..', limit: 25 });
221+
expect(postPayload).toEqual({ datetime: '2022-01-17T00:00:00Z/..', limit: 25 });
219222
});
220223

221224
it('includes open date range in search (no from-date)', async () => {
@@ -242,7 +245,7 @@ describe('useStacSearch — API supports POST', () => {
242245
await waitFor(() => expect(result.current.results).toEqual({ data: '12345' }));
243246
// Validate POST payload
244247
const postPayload = parseRequestPayload(fetch.mock.calls[1][1]);
245-
expect(postPayload).toEqual({ datetime: '../2022-05-17', limit: 25 });
248+
expect(postPayload).toEqual({ datetime: '../2022-05-17T23:59:59Z', limit: 25 });
246249
});
247250

248251
it('handles error with JSON response', async () => {
@@ -799,7 +802,7 @@ describe('useStacSearch — API supports GET', () => {
799802
await waitFor(() => expect(fetch).toHaveBeenCalledTimes(2));
800803
// Assert fetch URL and results
801804
expect(fetch.mock.calls[1][0]).toEqual(
802-
'https://fake-stac-api.net/search?limit=25&datetime=2022-01-17%2F2022-05-17'
805+
'https://fake-stac-api.net/search?limit=25&datetime=2022-01-17T00%3A00%3A00Z%2F2022-05-17T23%3A59%3A59Z'
803806
);
804807
expect(result.current.results).toEqual({ data: '12345' });
805808
});
@@ -825,7 +828,7 @@ describe('useStacSearch — API supports GET', () => {
825828
await waitFor(() => expect(fetch).toHaveBeenCalledTimes(2));
826829
// Assert fetch URL and results
827830
expect(fetch.mock.calls[1][0]).toEqual(
828-
'https://fake-stac-api.net/search?limit=25&datetime=2022-01-17%2F..'
831+
'https://fake-stac-api.net/search?limit=25&datetime=2022-01-17T00%3A00%3A00Z%2F..'
829832
);
830833
expect(result.current.results).toEqual({ data: '12345' });
831834
});
@@ -851,7 +854,7 @@ describe('useStacSearch — API supports GET', () => {
851854
await waitFor(() => expect(fetch).toHaveBeenCalledTimes(2));
852855
// Assert fetch URL and results
853856
expect(fetch.mock.calls[1][0]).toEqual(
854-
'https://fake-stac-api.net/search?limit=25&datetime=..%2F2022-05-17'
857+
'https://fake-stac-api.net/search?limit=25&datetime=..%2F2022-05-17T23%3A59%3A59Z'
855858
);
856859
expect(result.current.results).toEqual({ data: '12345' });
857860
});

src/stac-api/StacApi.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,32 @@ describe('StacApi', () => {
2424
expect(result).toBeUndefined();
2525
});
2626

27-
it('should format date range with from and to', () => {
27+
it('should format date range with from and to appending time parts', () => {
2828
const dateRange = { from: '2025-12-01', to: '2025-12-31' };
2929
const result = stacApi.makeDatetimePayload(dateRange);
3030
// Simple date format for STAC API compatibility
31-
expect(result).toBe('2025-12-01/2025-12-31');
31+
expect(result).toBe('2025-12-01T00:00:00Z/2025-12-31T23:59:59Z');
3232
});
3333

3434
it('should format date range with only from', () => {
3535
const dateRange = { from: '2025-12-01' };
3636
const result = stacApi.makeDatetimePayload(dateRange);
37-
expect(result).toBe('2025-12-01/..');
37+
expect(result).toBe('2025-12-01T00:00:00Z/..');
3838
});
3939

4040
it('should format date range with only to', () => {
4141
const dateRange = { to: '2025-12-31' };
4242
const result = stacApi.makeDatetimePayload(dateRange);
43-
expect(result).toBe('../2025-12-31');
43+
expect(result).toBe('../2025-12-31T23:59:59Z');
4444
});
4545

4646
it('should handle full datetime strings', () => {
4747
const dateRange = {
48-
from: '2025-12-01T00:00:00Z',
49-
to: '2025-12-31T23:59:59Z',
48+
from: '2025-12-01T11:11:11Z',
49+
to: '2025-12-31T11:11:11Z',
5050
};
5151
const result = stacApi.makeDatetimePayload(dateRange);
52-
expect(result).toBe('2025-12-01T00:00:00Z/2025-12-31T23:59:59Z');
52+
expect(result).toBe('2025-12-01T11:11:11Z/2025-12-31T11:11:11Z');
5353
});
5454
});
5555

@@ -80,7 +80,7 @@ describe('StacApi', () => {
8080
}),
8181
body: JSON.stringify({
8282
collections: ['sentinel-2-l2a'],
83-
datetime: '2025-12-01/2025-12-31',
83+
datetime: '2025-12-01T00:00:00Z/2025-12-31T23:59:59Z',
8484
ids: undefined,
8585
bbox: undefined,
8686
}),
@@ -98,7 +98,7 @@ describe('StacApi', () => {
9898
await getStacApi.search(searchPayload);
9999

100100
expect(mockFetch).toHaveBeenCalledWith(
101-
'https://api.example.com/search?collections=sentinel-2-l2a&datetime=2025-12-01%2F2025-12-31',
101+
'https://api.example.com/search?collections=sentinel-2-l2a&datetime=2025-12-01T00%3A00%3A00Z%2F2025-12-31T23%3A59%3A59Z',
102102
expect.objectContaining({
103103
method: 'GET',
104104
headers: expect.objectContaining({

src/stac-api/index.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,18 @@ class StacApi {
5151
}
5252

5353
makeDatetimePayload(dateRange?: DateRange): string | undefined {
54-
if (!dateRange) {
54+
if (!dateRange?.from && !dateRange?.to) {
5555
return undefined;
5656
}
5757

58-
const { from, to } = dateRange;
58+
const formatDate = (date: string | undefined, end?: boolean) => {
59+
if (!date) return '..';
5960

60-
if (from || to) {
61-
return `${from || '..'}/${to || '..'}`;
62-
} else {
63-
return undefined;
64-
}
61+
const timePart = end ? 'T23:59:59Z' : 'T00:00:00Z';
62+
return date.includes('T') ? date : `${date}${timePart}`;
63+
};
64+
65+
return `${formatDate(dateRange?.from)}/${formatDate(dateRange?.to, true)}`;
6566
}
6667

6768
payloadToQuery({ sortby, ...payload }: SearchPayload): string {

0 commit comments

Comments
 (0)