A library that computes the ephemerides.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

33 lines
862 B

  1. #!/usr/bin/env python3
  2. from datetime import datetime, timezone, timedelta
  3. def translate_to_timezone(date: datetime, to_tz: int, from_tz: int = None):
  4. if from_tz is not None:
  5. source_tz = timezone(timedelta(hours=from_tz))
  6. else:
  7. source_tz = timezone.utc
  8. return date.replace(tzinfo=source_tz).astimezone(
  9. tz=timezone(timedelta(hours=to_tz))
  10. )
  11. def normalize_datetime(date: datetime) -> datetime:
  12. """Round the seconds in the given datetime
  13. >>> normalize_datetime(datetime(2021, 6, 9, 2, 30, 29))
  14. datetime.datetime(2021, 6, 9, 2, 30)
  15. >>> normalize_datetime(datetime(2021, 6, 9, 2, 30, 30))
  16. datetime.datetime(2021, 6, 9, 2, 31)
  17. """
  18. return datetime(
  19. date.year,
  20. date.month,
  21. date.day,
  22. date.hour,
  23. date.minute if date.second < 30 else date.minute + 1,
  24. )