Skip to content

Commit e2b6e24

Browse files
authored
feat: Add parseTimeString() for parsing 12h/24h time formats (#15)
Add parseTimeString function to parse standalone time strings in multiple formats: - 12-hour with AM/PM: "9:07 AM", "2:30 pm", "2:30 p.m." - 24-hour: "09:00", "14:30", "9:00" - Optional seconds and fractional seconds - LLM-friendly error messages with examples Returns TimeAst compatible with Temporal.PlainTime.from(). Handles common LLM-generated formats like "09:00" alongside locale formats. Uses lexer-based parsing (no regex) consistent with library architecture. Includes 58 tests with 96%+ coverage.
1 parent af086f2 commit e2b6e24

File tree

6 files changed

+950
-1
lines changed

6 files changed

+950
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@taskade/temporal-parser": minor
3+
---
4+
5+
Add `parseTimeString` function for parsing standalone time strings in multiple formats (12h/24h, AM/PM, locale variations). Returns `TimeAst` compatible with Temporal.PlainTime. Handles common LLM-generated time formats like "09:00" alongside locale formats like "9:07 AM". Features LLM-friendly error messages with examples and suggestions.

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,39 @@ const offset = parseOffset('+08:00');
107107
// { kind: 'NumericOffset', sign: '+', hours: 8, minutes: 0, raw: '+08:00' }
108108
```
109109

110+
### Standalone Time Parser
111+
112+
```typescript
113+
import { parseTimeString } from '@taskade/temporal-parser';
114+
115+
// Parse 12-hour format with AM/PM
116+
const time1 = parseTimeString('2:30 PM');
117+
// { kind: 'Time', hour: 14, minute: 30 }
118+
119+
// Parse 24-hour format (international)
120+
const time2 = parseTimeString('14:30');
121+
// { kind: 'Time', hour: 14, minute: 30 }
122+
123+
// Parse with seconds
124+
const time3 = parseTimeString('2:30:45 PM');
125+
// { kind: 'Time', hour: 14, minute: 30, second: 45 }
126+
127+
// Parse with fractional seconds
128+
const time4 = parseTimeString('14:30:45.123');
129+
// { kind: 'Time', hour: 14, minute: 30, second: 45, fraction: '123' }
130+
131+
// Flexible AM/PM formats
132+
parseTimeString('2:30 PM'); // Standard
133+
parseTimeString('2:30PM'); // No space
134+
parseTimeString('2:30 pm'); // Lowercase
135+
parseTimeString('2:30 p.m.'); // With periods
136+
137+
// Special times
138+
parseTimeString('12:00 AM'); // Midnight (hour: 0)
139+
parseTimeString('12:00 PM'); // Noon (hour: 12)
140+
parseTimeString('11:59 PM'); // End of day (hour: 23)
141+
```
142+
110143
### Stringify AST Back to String
111144

112145
```typescript
@@ -204,6 +237,26 @@ Parses a numeric timezone offset string.
204237
- Hours: 0-14 (UTC-12:00 to UTC+14:00)
205238
- Minutes: 0-59
206239

240+
### `parseTimeString(input: string): TimeAst`
241+
242+
Parses a standalone time string in various formats.
243+
244+
**Supported formats:**
245+
- 12-hour with AM/PM: `2:30 PM`, `02:30PM`, `2:30 p.m.`
246+
- 24-hour (international): `14:30`, `02:30`, `23:59`
247+
- With seconds: `2:30:45 PM`, `14:30:45`
248+
- With fractional seconds: `2:30:45.123 PM`, `14:30:45,123` (comma or dot)
249+
250+
**Special cases:**
251+
- `12:00 AM` → midnight (hour: 0)
252+
- `12:00 PM` → noon (hour: 12)
253+
- `12:30 AM` → 00:30 (after midnight)
254+
- `12:30 PM` → 12:30 (after noon)
255+
256+
**Returns:** `TimeAst` object compatible with `Temporal.PlainTime.from()`
257+
258+
**Throws:** `ParseError` if the input is invalid
259+
207260
### `stringifyTemporal(ast: TemporalAst): string`
208261

209262
Converts a temporal AST back to its string representation.

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ export { parseTemporal } from './parser.js';
1515
// Export offset parser (useful standalone utility)
1616
export { parseOffset } from './parseOffset.js';
1717

18+
// Export time string parser (useful standalone utility)
19+
export { parseTimeString } from './parseTimeString.js';
20+
1821
// Export stringify functionality
1922
export {
2023
stringifyAnnotation,

0 commit comments

Comments
 (0)