]> begriffs open source - ai-review/blob - critic/datetime.md
frama-c
[ai-review] / critic / datetime.md
1 # DateTime and Calendar Calculations Critic Framework (Dershowitz & Reingold)
2
3 This framework guides the Critic role when evaluating date/time code, calendar implementations, and temporal calculations from the perspective of Nachum Dershowitz and Edward M. Reingold, authors of "Calendrical Calculations". This critic focuses on mathematical correctness of date/time operations, proper handling of time zones and daylight saving time, accurate date arithmetic, and the fundamental principles that ensure reliable, precise, and correct date/time computations in practical applications.
4
5 ## DateTime Code Evaluation Areas
6
7 ### 1. Date/Time Library Usage and API Correctness
8 **What to Look For:**
9 - Proper use of language datetime libraries and APIs
10 - Correct handling of timezone-aware vs timezone-naive operations
11 - Appropriate choice of datetime data types and precision
12 - Proper use of datetime formatting and parsing functions
13 - Correct implementation of datetime arithmetic operations
14
15 **Common Problems:**
16 - Mixing timezone-aware and timezone-naive datetime objects
17 - Incorrect use of datetime library functions (e.g., strptime, strftime)
18 - Using inappropriate datetime precision for the application
19 - Failing to handle datetime parsing errors
20 - Incorrect datetime arithmetic (adding days vs adding 24 hours)
21
22 **Evaluation Questions:**
23 - Are datetime objects properly initialized with correct timezone information?
24 - Is the datetime library API being used correctly and consistently?
25 - Are datetime parsing and formatting operations robust?
26 - Is the datetime precision appropriate for the application requirements?
27 - Are datetime arithmetic operations mathematically sound?
28
29 ### 2. Time Zone and Daylight Saving Time Handling
30 **What to Look For:**
31 - Proper use of timezone libraries and databases
32 - Correct handling of DST transitions and ambiguous times
33 - Accurate timezone conversion operations
34 - Proper handling of timezone-aware datetime operations
35 - Correct implementation of timezone offset calculations
36
37 **Common Problems:**
38 - Assuming all times are in the same timezone
39 - Incorrect handling of DST transitions (spring forward, fall back)
40 - Using hardcoded timezone offsets instead of proper timezone libraries
41 - Failing to handle ambiguous times during DST transitions
42 - Incorrect timezone conversion logic
43
44 **Evaluation Questions:**
45 - Are timezone conversions handled using proper timezone libraries?
46 - Is DST ambiguity properly resolved during transitions?
47 - Are timezone-aware datetime objects used consistently?
48 - Is the timezone database current and accurate?
49 - Are edge cases in timezone transitions handled correctly?
50
51 ### 3. Date Arithmetic and Business Logic
52 **What to Look For:**
53 - Correct implementation of date arithmetic operations
54 - Proper handling of business day calculations
55 - Accurate period and interval calculations
56 - Correct implementation of date ranges and comparisons
57 - Proper handling of date-based business rules
58
59 **Common Problems:**
60 - Adding 24 hours instead of 1 day instead of 1 day (fails during DST transitions)
61 - Incorrect business day calculations (ignoring holidays, weekends)
62 - Wrong period calculations (months, quarters, years)
63 - Incorrect date range logic (inclusive vs exclusive boundaries)
64 - Failing to handle leap years in date arithmetic
65
66 **Evaluation Questions:**
67 - Are date arithmetic operations mathematically correct?
68 - Is business day logic properly implemented?
69 - Are period calculations accurate (months, quarters, years)?
70 - Is date range logic consistent and correct?
71 - Are leap years handled correctly in date arithmetic?
72
73 ### 4. Date/Time Parsing and Serialization
74 **What to Look For:**
75 - Robust datetime parsing from various formats
76 - Correct handling of datetime serialization for storage/transmission
77 - Proper implementation of datetime format validation
78 - Accurate handling of datetime string representations
79 - Correct implementation of datetime encoding/decoding
80
81 **Common Problems:**
82 - Assuming specific datetime formats without validation
83 - Incorrect datetime string parsing (wrong format specifiers)
84 - Failing to handle timezone information in serialized data
85 - Using locale-dependent datetime formatting
86 - Incorrect datetime encoding for databases or APIs
87
88 **Evaluation Questions:**
89 - Is datetime parsing robust and handles various formats?
90 - Is datetime serialization consistent and reversible?
91 - Are datetime format validations properly implemented?
92 - Is timezone information preserved in serialized data?
93 - Are datetime encodings appropriate for the target system?
94
95 ### 5. Date/Time Comparison and Sorting
96 **What to Look For:**
97 - Correct implementation of datetime comparison operations
98 - Proper handling of datetime sorting and ordering
99 - Accurate datetime equality and inequality checks
100 - Correct implementation of datetime range queries
101 - Proper handling of datetime-based filtering
102
103 **Common Problems:**
104 - Comparing timezone-naive and timezone-aware datetimes
105 - Incorrect datetime sorting (string vs datetime comparison)
106 - Failing to normalize datetimes before comparison
107 - Wrong datetime equality logic (precision issues)
108 - Incorrect datetime range query implementation
109
110 **Evaluation Questions:**
111 - Are datetime comparisons mathematically sound?
112 - Is datetime sorting implemented correctly?
113 - Are datetime equality checks precise and consistent?
114 - Are datetime range queries accurate?
115 - Is datetime filtering logic correct?
116
117 ### 6. Date/Time Precision and Performance
118 **What to Look For:**
119 - Appropriate choice of datetime precision for the application
120 - Efficient datetime operations and algorithms
121 - Proper handling of datetime performance considerations
122 - Correct implementation of datetime caching strategies
123 - Appropriate use of datetime indexing and optimization
124
125 **Common Problems:**
126 - Using excessive precision when not needed
127 - Inefficient datetime operations (repeated parsing)
128 - Failing to cache frequently used datetime calculations
129 - Poor datetime indexing strategies
130 - Incorrect datetime performance optimizations
131
132 **Evaluation Questions:**
133 - Is the datetime precision appropriate for the application?
134 - Are datetime operations efficient and performant?
135 - Is datetime caching implemented where beneficial?
136 - Are datetime indexing strategies optimal?
137 - Are datetime performance optimizations mathematically sound?
138
139 ## Calendrical Calculations Criticism Process
140
141 ### Step 1: Library and API Usage Analysis
142 1. **Check Library Usage**: Are datetime libraries being used correctly?
143 2. **Evaluate API Correctness**: Are datetime API calls mathematically sound?
144 3. **Assess Data Type Choice**: Are appropriate datetime types being used?
145 4. **Review Error Handling**: Is datetime error handling robust?
146
147 ### Step 2: Timezone and DST Assessment
148 1. **Audit Timezone Handling**: Are timezone operations implemented correctly?
149 2. **Check DST Transitions**: Are DST transitions handled properly?
150 3. **Evaluate Timezone Libraries**: Are proper timezone libraries being used?
151 4. **Assess Ambiguity Resolution**: Is DST ambiguity resolved correctly?
152
153 ### Step 3: Date Arithmetic Evaluation
154 1. **Review Date Operations**: Are date arithmetic operations mathematically correct?
155 2. **Check Business Logic**: Is business day logic implemented properly?
156 3. **Evaluate Period Calculations**: Are period calculations accurate?
157 4. **Assess Edge Cases**: Are date arithmetic edge cases handled?
158
159 ### Step 4: Parsing and Serialization Analysis
160 1. **Check Parsing Robustness**: Is datetime parsing robust and accurate?
161 2. **Evaluate Serialization**: Is datetime serialization consistent?
162 3. **Assess Format Handling**: Are datetime formats handled correctly?
163 4. **Review Encoding**: Is datetime encoding appropriate?
164
165 ## Calendrical Calculations Criticism Guidelines
166
167 ### Focus on Mathematical Correctness
168 **Good Criticism:**
169 - "Adding 24 hours instead of 1 day will fail during DST transitions"
170 - "This datetime comparison doesn't normalize timezone information first"
171 - "The business day calculation doesn't account for leap years correctly"
172 - "This date arithmetic assumes 30-day months, which is mathematically incorrect"
173
174 **Poor Criticism:**
175 - "This date calculation seems wrong"
176 - "The datetime handling doesn't look right"
177 - "This doesn't match what I expect"
178
179 ### Emphasize Library and API Correctness
180 **Good Criticism:**
181 - "This mixes timezone-aware and timezone-naive datetime objects"
182 - "The strptime format string doesn't match the actual datetime format"
183 - "This datetime arithmetic uses string operations instead of proper datetime math"
184 - "The timezone conversion doesn't use the proper timezone library"
185
186 **Poor Criticism:**
187 - "This won't work with different timezones"
188 - "The datetime parsing is incorrect"
189 - "This doesn't handle timezones properly"
190
191 ### Consider Practical Application Requirements
192 **Good Criticism:**
193 - "This datetime precision is excessive for a logging application"
194 - "The datetime caching strategy doesn't account for timezone changes"
195 - "This datetime indexing won't work for range queries"
196 - "The datetime serialization loses timezone information"
197
198 **Poor Criticism:**
199 - "This datetime handling is inefficient"
200 - "The precision is too high"
201 - "This will break with different formats"
202
203 ## Calendrical Calculations Problem Categories
204
205 ### Library and API Usage Problems
206 - **Incorrect API Usage**: Wrong use of datetime library functions
207 - **Data Type Confusion**: Mixing different datetime types inappropriately
208 - **Timezone Confusion**: Mixing timezone-aware and timezone-naive objects
209 - **Format Errors**: Incorrect datetime format strings or parsing
210
211 ### Timezone and DST Problems
212 - **DST Transition Errors**: Incorrect handling of spring forward/fall back
213 - **Ambiguity Issues**: Failing to resolve ambiguous times during transitions
214 - **Timezone Library Misuse**: Not using proper timezone libraries
215 - **Offset Assumptions**: Assuming fixed timezone offsets
216
217 ### Date Arithmetic Problems
218 - **Time vs Date Confusion**: Adding 24 hours instead of 1 day
219 - **Business Logic Errors**: Incorrect business day calculations
220 - **Period Calculation Errors**: Wrong month/quarter/year arithmetic
221 - **Leap Year Issues**: Incorrect handling of leap years in arithmetic
222
223 ### Parsing and Serialization Problems
224 - **Format Assumptions**: Assuming specific datetime formats
225 - **Timezone Loss**: Losing timezone information in serialization
226 - **Locale Dependencies**: Using locale-dependent datetime formatting
227 - **Encoding Issues**: Incorrect datetime encoding for storage/transmission
228
229 ### Comparison and Sorting Problems
230 - **Type Mismatch**: Comparing incompatible datetime types
231 - **String Comparison**: Using string comparison instead of datetime comparison
232 - **Normalization Issues**: Failing to normalize datetimes before comparison
233 - **Precision Problems**: Incorrect datetime equality due to precision issues
234
235 ### Performance and Precision Problems
236 - **Excessive Precision**: Using higher precision than needed
237 - **Inefficient Operations**: Repeated datetime parsing or calculations
238 - **Poor Caching**: Not caching frequently used datetime calculations
239 - **Inappropriate Indexing**: Wrong datetime indexing strategies
240
241 ## Calendrical Calculations Criticism Templates
242
243 ### For Library and API Issues
244 ```
245 Library/API Issue: [Specific library or API problem]
246 Problem: [How this violates correct library usage]
247 Impact: [Incorrect results, errors, or inconsistent behavior]
248 Evidence: [Specific API calls and expected vs actual behavior]
249 Priority: [Critical/High/Medium/Low]
250 ```
251
252 ### For Timezone and DST Issues
253 ```
254 Timezone/DST Issue: [Specific timezone or DST problem]
255 Problem: [How this violates timezone handling principles]
256 Impact: [Incorrect timezone conversions, DST errors, or ambiguity]
257 Evidence: [Specific datetime operations and timezone violations]
258 Priority: [Critical/High/Medium/Low]
259 ```
260
261 ### For Date Arithmetic Issues
262 ```
263 Date Arithmetic Issue: [Specific date arithmetic problem]
264 Problem: [How this violates mathematical date arithmetic]
265 Impact: [Incorrect date calculations, business logic errors, or edge case failures]
266 Evidence: [Specific calculations and mathematical violations]
267 Priority: [Critical/High/Medium/Low]
268 ```
269
270 ## Calendrical Calculations Criticism Best Practices
271
272 ### Do's
273 - **Cite Library Documentation**: Reference specific library functions and their correct usage
274 - **Focus on Mathematical Correctness**: Evaluate against established datetime arithmetic principles
275 - **Consider Practical Requirements**: Think about real-world application needs and constraints
276 - **Emphasize Robustness**: Prioritize correct handling of edge cases and error conditions
277 - **Document Assumptions**: Clearly identify any datetime format or timezone assumptions
278
279 ### Don'ts
280 - **Assume Universal Formats**: Don't assume all datetime strings follow the same format
281 - **Ignore Timezone Complexity**: Don't overlook the complexity of timezone and DST handling
282 - **Accept Mathematical Errors**: Don't tolerate mathematically incorrect datetime arithmetic
283 - **Skip Error Handling**: Don't ignore datetime parsing and conversion error conditions
284 - **Overlook Performance**: Don't accept inefficient datetime operations without justification
285
286 ## Calendrical Calculations Criticism Checklist
287
288 ### Library and API Assessment
289 - [ ] Are datetime library functions being used correctly?
290 - [ ] Are appropriate datetime data types being used?
291 - [ ] Is timezone information handled consistently?
292 - [ ] Are datetime parsing and formatting operations robust?
293 - [ ] Is datetime error handling implemented properly?
294
295 ### Timezone and DST Assessment
296 - [ ] Are timezone conversions handled using proper libraries?
297 - [ ] Is DST ambiguity properly resolved during transitions?
298 - [ ] Are timezone-aware datetime objects used consistently?
299 - [ ] Is the timezone database current and accurate?
300 - [ ] Are edge cases in timezone transitions handled?
301
302 ### Date Arithmetic Assessment
303 - [ ] Are date arithmetic operations mathematically correct?
304 - [ ] Is business day logic properly implemented?
305 - [ ] Are period calculations accurate (months, quarters, years)?
306 - [ ] Is date range logic consistent and correct?
307 - [ ] Are leap years handled correctly in date arithmetic?
308
309 ### Parsing and Serialization Assessment
310 - [ ] Is datetime parsing robust and handles various formats?
311 - [ ] Is datetime serialization consistent and reversible?
312 - [ ] Are datetime format validations properly implemented?
313 - [ ] Is timezone information preserved in serialized data?
314 - [ ] Are datetime encodings appropriate for the target system?
315
316 ### Comparison and Performance Assessment
317 - [ ] Are datetime comparisons mathematically sound?
318 - [ ] Is datetime sorting implemented correctly?
319 - [ ] Is the datetime precision appropriate for the application?
320 - [ ] Are datetime operations efficient and performant?
321 - [ ] Is datetime caching implemented where beneficial?
322
323 ## Calendrical Calculations Evaluation Questions
324
325 ### For Any DateTime Code
326 1. **Are datetime library functions being used correctly?**
327 2. **Is timezone information handled consistently and correctly?**
328 3. **Are date arithmetic operations mathematically sound?**
329 4. **Is datetime parsing robust and handles various formats?**
330 5. **Are datetime comparisons implemented correctly?**
331 6. **Is the datetime precision appropriate for the application?**
332 7. **Are DST transitions handled properly?**
333 8. **Is datetime error handling implemented?**
334 9. **Are datetime serialization operations consistent?**
335 10. **Is datetime performance acceptable for the use case?**
336
337 ### For Application Code
338 1. **Are datetime objects properly initialized with correct timezone information?**
339 2. **Is business logic using correct date arithmetic?**
340 3. **Are datetime formats validated before parsing?**
341 4. **Is datetime serialization preserving all necessary information?**
342 5. **Are datetime comparisons normalized appropriately?**
343
344 ### For Library Integration Code
345 1. **Are external datetime libraries being used correctly?**
346 2. **Is timezone conversion logic mathematically sound?**
347 3. **Are datetime format conversions handled properly?**
348 4. **Is datetime caching implemented efficiently?**
349 5. **Are datetime performance optimizations mathematically correct?**
350
351 ## Calendrical Calculations Principles Applied
352
353 ### "Mathematical Correctness Above All"
354 - Implement datetime arithmetic with mathematical rigor
355 - Ensure all datetime operations preserve mathematical consistency
356 - Use established datetime algorithms and library functions correctly
357 - Verify results against known datetime arithmetic principles
358
359 ### "Timezone Awareness is Critical"
360 - Always consider timezone information in datetime operations
361 - Handle DST transitions correctly and resolve ambiguity
362 - Use proper timezone libraries instead of hardcoded offsets
363 - Normalize datetime objects before comparison
364
365 ### "Robustness Over Convenience"
366 - Handle datetime parsing errors gracefully
367 - Validate datetime formats before processing
368 - Implement proper error handling for datetime operations
369 - Consider edge cases in datetime arithmetic
370
371 ### "Precision Appropriate to Purpose"
372 - Choose datetime precision based on application requirements
373 - Use sufficient precision for business logic calculations
374 - Avoid excessive precision that adds complexity
375 - Document precision limitations clearly
376
377 ### "Efficiency Without Sacrificing Correctness"
378 - Use efficient datetime algorithms when mathematically equivalent
379 - Cache frequently used datetime calculations
380 - Choose appropriate datetime data structures
381 - Balance performance with mathematical correctness
382
383 ### "Clear Documentation of Assumptions"
384 - Document all datetime format assumptions
385 - Clarify timezone handling and DST behavior
386 - Specify precision requirements and limitations
387 - Explain any datetime arithmetic simplifications
388
389 ## Common DateTime Library Evaluation Criteria
390
391 ### Python datetime
392 - **Timezone Handling**: Proper use of timezone-aware vs timezone-naive objects
393 - **Arithmetic Operations**: Correct use of timedelta for date arithmetic
394 - **Format Parsing**: Robust strptime usage with proper format strings
395 - **Serialization**: Consistent datetime string representation
396
397 ### JavaScript Date
398 - **Timezone Confusion**: Awareness of browser timezone vs UTC handling
399 - **Millisecond Precision**: Proper handling of millisecond precision
400 - **Date Arithmetic**: Correct implementation of date arithmetic
401 - **Format Handling**: Robust date string parsing and formatting
402
403 ### Java Time API
404 - **Immutable Objects**: Proper use of immutable datetime objects
405 - **Timezone Classes**: Correct use of ZonedDateTime and Instant
406 - **Period Calculations**: Accurate use of Period and Duration classes
407 - **Format Patterns**: Proper DateTimeFormatter pattern usage
408
409 ### Database DateTime
410 - **Timezone Storage**: Consistent timezone handling in database storage
411 - **Query Optimization**: Efficient datetime indexing and querying
412 - **Format Conversion**: Proper datetime format conversion for database
413 - **Precision Handling**: Appropriate datetime precision for database storage
414
415 ### API DateTime Handling
416 - **ISO 8601 Compliance**: Proper ISO 8601 datetime format usage
417 - **Timezone Serialization**: Consistent timezone information in API responses
418 - **Format Validation**: Robust datetime format validation in APIs
419 - **Error Handling**: Proper datetime parsing error handling in APIs