convert.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #pragma once
  2. #include "civil.h"
  3. #include <contrib/libs/cctz/include/cctz/time_zone.h>
  4. #include <util/datetime/base.h>
  5. #include <util/draft/datetime.h>
  6. namespace NDatetime {
  7. /**
  8. * @return The mother of all time zones.
  9. * @see https://en.wikipedia.org/wiki/Coordinated_Universal_Time
  10. */
  11. TTimeZone GetUtcTimeZone();
  12. /**
  13. * @return The time zone that is curently set on your machine.
  14. */
  15. TTimeZone GetLocalTimeZone();
  16. /**
  17. * @param absoluteTime A TInstant representing a number of seconds elapsed
  18. * since The Epoch (the microsecond part is ignored).
  19. * @param tz The time zone to use for conversion.
  20. * @return The civil time corresponding to absoluteTime.
  21. * @note This conversion is always well-defined (i.e., there
  22. * is exactly one civil time which corresponds to
  23. * absoluteTime).
  24. * @see https://en.wikipedia.org/wiki/Unix_time
  25. */
  26. TSimpleTM ToCivilTime(const TInstant& absoluteTime, const TTimeZone& tz);
  27. /**
  28. * Creates civil time in place with respect of given timezone.
  29. * @param[in] tz The time zone to use for creation.
  30. * @param[in] year The year of the creation time.
  31. * @param[in] mon The month of the creation time.
  32. * @param[in] day The day of the creation time.
  33. * @param[in] h The hour of the creation time.
  34. * @param[in] m The minute of the creation time.
  35. * @param[in] s The second of the creation time.
  36. * @return a civil time
  37. */
  38. TSimpleTM CreateCivilTime(const TTimeZone& tz, ui32 year, ui32 mon, ui32 day, ui32 h = 0, ui32 m = 0, ui32 s = 0);
  39. /**
  40. * @param civilTime A human-readable date and time (the following fields
  41. * are used by this function: {Year,Mon,MDay,Hour,Min,Sec}).
  42. * @param tz The time zone to use for conversion.
  43. * @return Some absolute time corresponding to civilTime.
  44. * @note If multiple absolute times match civilTime, the earliest
  45. * if returned.
  46. * If civilTime doesn't exist due to discontinuity in time
  47. * (e.g., DST happened) we pretend the discontinuity isn't
  48. * there (i.e., if we skipped from 1:59AM to 3:00AM then
  49. * ToAbsoluteTime(2:30AM) == ToAbsoluteTime(3:30AM)).
  50. * @see https://en.wikipedia.org/wiki/Daylight_saving_time
  51. */
  52. TInstant ToAbsoluteTime(const TSimpleTM& civilTime, const TTimeZone& tz);
  53. }