Parsing and formatting days.
This commit is contained in:
		
							parent
							
								
									f42861e9da
								
							
						
					
					
						commit
						46db92cc6b
					
				
					 1 changed files with 30 additions and 1 deletions
				
			
		|  | @ -43,6 +43,8 @@ | |||
| 
 | ||||
|  (import scheme | ||||
| 	 (chicken base) | ||||
| 	 (chicken format) | ||||
| 	 (chicken string) | ||||
| 	 util-tag | ||||
| 	 cal-month | ||||
| 	 testing) | ||||
|  | @ -113,6 +115,27 @@ | |||
| 	 (cal-month-add M 1) | ||||
| 	 M))) | ||||
| 
 | ||||
|  ;; Converts day to ISO date string | ||||
|  (define (cal-day->string v) | ||||
|    (let ((y (cal-day-year v)) | ||||
| 	 (m (cal-day-month v)) | ||||
| 	 (d (cal-day-day v))) | ||||
|      (format "~A-~A-~A" | ||||
| 	     y | ||||
| 	     (if (> m 9) | ||||
| 		 m | ||||
| 		 (format "0~A" m)) | ||||
| 	     (if (> d 9) | ||||
| 		 d | ||||
| 		 (format "0~A" d))))) | ||||
| 
 | ||||
|  ;; Converts ISO date string to cal-day | ||||
|  (define (string->cal-day s) | ||||
|    (let ((l (string-split s "-"))) | ||||
|      (if (= (length l) 3) | ||||
| 	 (apply make-cal-day (map string->number l)) | ||||
| 	 #f))) | ||||
| 
 | ||||
|  ;; Module self-tests | ||||
|  (define (cal-day-tests!) | ||||
|    (run-tests | ||||
|  | @ -143,6 +166,12 @@ | |||
| 		(cal-day? (make-cal-day 2023 2 29))) | ||||
|     (test-true cal-day? | ||||
| 	       (cal-day? (make-cal-day 2024 2 29))) | ||||
|     (test-equal? cal-day->string | ||||
| 		 (cal-day->string (make-cal-day 2024 2 29)) | ||||
| 		 "2024-02-29") | ||||
|     (test-equal? string->cal-day | ||||
| 		 (string->cal-day "2023-05-11") | ||||
| 		 (make-cal-day 2023 5 11)) | ||||
|     )) | ||||
| 
 | ||||
|  ) | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue