💿🐜 Antkeeper source code https://antkeeper.com
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.

77 lines
2.7 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_PHYSICS_TIME_GREGORIAN_HPP
  20. #define ANTKEEPER_PHYSICS_TIME_GREGORIAN_HPP
  21. #include "physics/time/jd.hpp"
  22. namespace physics {
  23. namespace time {
  24. /// Gregorian calender time.
  25. namespace gregorian {
  26. /**
  27. * Calculates the JD time from a Gregorian date and time. Valid for all dates after November 23, 4713.
  28. *
  29. * @param year Astronomical year numbering. 1 BC is `0`, 2 BC is `-1`.
  30. * @param month Month number on `[1, 12]`.
  31. * @param day Day number on `[1, 31]`.
  32. * @param hour Hour number on `[0, 23]`.
  33. * @param minute Minute number on `[0, 59]`.
  34. * @param second Fractional second on `[0.0, 60.0)`.
  35. * @param utc UTC offset.
  36. *
  37. * @return JD time.
  38. *
  39. * @see L. E. Doggett, Ch. 12, "Calendars", p. 606, in Seidelmann 1992
  40. */
  41. template <class T>
  42. T to_jd(int year, int month, int day, int hour, int minute, T second, T utc)
  43. {
  44. T jdn = static_cast<T>((1461 * (year + 4800 + (month - 14) / 12)) / 4 + (367 * (month - 2 - 12 * ((month - 14) / 12))) / 12 - (3 * ((year + 4900 + (month - 14) / 12) / 100)) / 4 + day - 32075);
  45. return jdn + static_cast<T>(hour - 12) / T(24) + static_cast<T>(minute) / T(1440) + static_cast<T>(second) / T(86400) + utc / T(24);
  46. }
  47. /**
  48. * Calculates the UT1 time from a Gregorian date and time. Valid for all dates after November 23, 4713.
  49. *
  50. * @param year Astronomical year numbering. 1 BC is `0`, 2 BC is `-1`.
  51. * @param month Month number on `[1, 12]`.
  52. * @param day Day number on `[1, 31]`.
  53. * @param hour Hour number on `[0, 23]`.
  54. * @param minute Minute number on `[0, 59]`.
  55. * @param second Fractional second on `[0.0, 60.0)`.
  56. * @param utc UTC offset.
  57. *
  58. * @return UT1 time.
  59. */
  60. template <class T>
  61. T to_ut1(int year, int month, int day, int hour, int minute, T second, T utc)
  62. {
  63. return physics::time::jd::to_ut1<T>(to_jd<T>(year, month, day, hour, minute, second, utc));
  64. }
  65. } // namespace gregorian
  66. } // namespace time
  67. } // namespace physics
  68. #endif // ANTKEEPER_PHYSICS_TIME_GREGORIAN_HPP