timeUtils.h
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include <string>
7 #include <ctime>
8 
15 namespace dpyTime {
16 
22 std::string getLocalTimeStamp();
23 
28 struct tm getLocalTime();
29 
36 class SunTimes
37 {
38 public:
39  enum class ZenithPreset {
40  Official, // 90°50' = 90.833°
41  Civil, // 96°
42  Nautical, // 102°
43  Astronomical // 108°
44  };
45 
46  static double zenithDeg(ZenithPreset preset);
47 
48  SunTimes(double latitudeDeg,
49  double longitudeDeg,
50  ZenithPreset preset = ZenithPreset::Official);
51 
52  SunTimes(double latitudeDeg,
53  double longitudeDeg,
54  double zenithDeg);
55 
63  bool compute(const std::tm& localDate,
64  std::time_t& sunriseLocal,
65  std::time_t& sunsetLocal) const;
66 
71  bool compute(std::time_t currentLocal,
72  std::time_t& sunriseLocal,
73  std::time_t& sunsetLocal) const;
74 
81  bool isDay(std::time_t nowLocal) const;
82 
83 private:
84  double lat_;
85  double lon_;
86  double zenith_;
87 
88  enum class EventKind {
89  Ok,
90  PolarNight,
91  PolarDay,
92  Error
93  };
94 
95  struct EventUtcHoursResult
96  {
97  EventKind status = EventKind::Error;
98  double utcHours = 0.0;
99  };
100 
101  static EventUtcHoursResult sunEventUtcHours(const std::tm& localDate,
102  double latitudeDeg, double longitudeDeg,
103  double zenithDeg, bool rising);
104 
105  static std::time_t buildUtcTime(const std::tm& baseDate, double utcHours);
106  static std::tm shiftLocalDate(const std::tm& baseDate, int dayOffset);
107  bool findNearestEvent(const std::tm& baseDate,
108  bool rising,
109  int firstDayOffset,
110  int dayStep,
111  std::time_t& eventLocal) const;
112 };
113 
119 bool getSunriseSunsetLocal(const std::tm& localDate,
120  double latitudeDeg, double longitudeDeg,
121  SunTimes::ZenithPreset preset,
122  std::time_t& sunriseLocal,
123  std::time_t& sunsetLocal);
124 
131 bool getSunriseSunsetLocal(std::time_t nowLocal,
132  double latitudeDeg, double longitudeDeg,
133  SunTimes::ZenithPreset preset,
134  std::time_t& sunriseLocal,
135  std::time_t& sunsetLocal);
136 
141 bool isDayLocal(std::time_t nowLocal,
142  double latitudeDeg, double longitudeDeg,
143  SunTimes::ZenithPreset preset);
144 
145 } /* namespace dpyTime */
bool isDayLocal(std::time_t nowLocal, double latitudeDeg, double longitudeDeg, SunTimes::ZenithPreset preset)
Convenience wrapper: is it day at nowLocal for given location/preset?
bool compute(const std::tm &localDate, std::time_t &sunriseLocal, std::time_t &sunsetLocal) const
Compute sunrise/sunset for given local calendar date.
bool isDay(std::time_t nowLocal) const
Determine daylight state for a local instant.
struct tm getLocalTime()
Get local time.
std::string getLocalTimeStamp()
Get local time as a timestamp string.
bool getSunriseSunsetLocal(const std::tm &localDate, double latitudeDeg, double longitudeDeg, SunTimes::ZenithPreset preset, std::time_t &sunriseLocal, std::time_t &sunsetLocal)
Convenience wrapper to get sunrise/sunset for a date/location (LOCAL time_t outputs).
Sunrise/Sunset calculator (NOAA-style)
Definition: timeUtils.h:36
Time utility methods.