Add missing code
This commit is contained in:
		
							parent
							
								
									4bde0d8524
								
							
						
					
					
						commit
						a46661c1b3
					
				
							
								
								
									
										39
									
								
								agenda/birthday.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								agenda/birthday.py
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,39 @@
 | 
				
			||||||
 | 
					"""Birthdays."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from datetime import date
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import yaml
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from .types import Event
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def next_birthday(from_date: date, birth_date: date) -> tuple[date, int]:
 | 
				
			||||||
 | 
					    """Calculate the date of the next birthday based on a given birth date."""
 | 
				
			||||||
 | 
					    next_birthday_date = birth_date.replace(year=from_date.year)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if from_date > next_birthday_date:
 | 
				
			||||||
 | 
					        next_birthday_date = birth_date.replace(year=from_date.year + 1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    age_at_next_birthday = next_birthday_date.year - birth_date.year
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return next_birthday_date, age_at_next_birthday
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def get_birthdays(from_date: date, filepath: str) -> list[Event]:
 | 
				
			||||||
 | 
					    """Get birthdays from config."""
 | 
				
			||||||
 | 
					    events = []
 | 
				
			||||||
 | 
					    with open(filepath) as f:
 | 
				
			||||||
 | 
					        entities = yaml.safe_load(f)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for entity in entities:
 | 
				
			||||||
 | 
					        birthday = date(**entity["birthday"])
 | 
				
			||||||
 | 
					        bday, age = next_birthday(from_date, birthday)
 | 
				
			||||||
 | 
					        events.append(
 | 
				
			||||||
 | 
					            Event(
 | 
				
			||||||
 | 
					                date=bday,
 | 
				
			||||||
 | 
					                name="birthday",
 | 
				
			||||||
 | 
					                title=f'{entity["label"]} (aged {age})',
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return events
 | 
				
			||||||
		Loading…
	
		Reference in a new issue