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.
 
 

39 lines
1.0 KiB

  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. >>> normalize_datetime(datetime(2021, 6, 9, 23, 59, 59))
  18. datetime.datetime(2021, 6, 10, 0, 0)
  19. >>> normalize_datetime(datetime(2021, 12, 31, 23, 59, 59))
  20. datetime.datetime(2022, 1, 1, 0, 0)
  21. """
  22. new_date = datetime(date.year, date.month, date.day, date.hour, date.minute)
  23. if date.second >= 30:
  24. new_date += timedelta(minutes=1)
  25. return new_date