MCPcopy Index your code
hub / github.com/sourcebot-dev/sourcebot / parseTemporalDate

Function parseTemporalDate

packages/web/src/features/git/dateUtils.ts:21–101  ·  view source on GitHub ↗
(dateStr: string | undefined)

Source from the content-addressed store, hash-verified

19 * parseTemporalDate(undefined) // undefined
20 */
21export function parseTemporalDate(dateStr: string | undefined): string | undefined {
22 if (!dateStr) {
23 return undefined;
24 }
25
26 // Try parsing as ISO date first
27 const isoDate = new Date(dateStr);
28 if (!isNaN(isoDate.getTime())) {
29 return isoDate.toISOString();
30 }
31
32 // Parse relative dates (Git-compatible format)
33 // Git accepts these natively, but we normalize to ISO for consistency
34 const lowerStr = dateStr.toLowerCase().trim();
35
36 // Handle "yesterday"
37 if (lowerStr === 'yesterday') {
38 const date = new Date();
39 date.setDate(date.getDate() - 1);
40 return date.toISOString();
41 }
42
43 // Handle "N <unit>s ago" format
44 const matchRelative = lowerStr.match(/^(\d+)\s+(second|minute|hour|day|week|month|year)s?\s+ago$/i);
45 if (matchRelative) {
46 const amount = parseInt(matchRelative[1]);
47 const unit = matchRelative[2].toLowerCase();
48 const date = new Date();
49
50 switch (unit) {
51 case 'second':
52 date.setSeconds(date.getSeconds() - amount);
53 break;
54 case 'minute':
55 date.setMinutes(date.getMinutes() - amount);
56 break;
57 case 'hour':
58 date.setHours(date.getHours() - amount);
59 break;
60 case 'day':
61 date.setDate(date.getDate() - amount);
62 break;
63 case 'week':
64 date.setDate(date.getDate() - (amount * 7));
65 break;
66 case 'month':
67 date.setMonth(date.getMonth() - amount);
68 break;
69 case 'year':
70 date.setFullYear(date.getFullYear() - amount);
71 break;
72 }
73
74 return date.toISOString();
75 }
76
77 // Handle "last <unit>" format
78 const matchLast = lowerStr.match(/^last\s+(week|month|year)$/i);

Callers 4

dateUtils.test.tsFile · 0.90
validateDateRangeFunction · 0.85
toDbDateFunction · 0.85
toGitDateFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected