mptTime.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * mptTime.h
  3. * ---------
  4. * Purpose: Various time utility functions.
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #pragma once
  10. #include "openmpt/all/BuildSettings.hpp"
  11. #include <string>
  12. #include <time.h>
  13. OPENMPT_NAMESPACE_BEGIN
  14. namespace mpt
  15. {
  16. namespace Date
  17. {
  18. #if defined(MODPLUG_TRACKER)
  19. #if MPT_OS_WINDOWS
  20. namespace ANSI
  21. {
  22. // uint64 counts 100ns since 1601-01-01T00:00Z
  23. uint64 Now();
  24. mpt::ustring ToUString(uint64 time100ns); // i.e. 2015-01-15 18:32:01.718
  25. } // namespacee ANSI
  26. #endif // MPT_OS_WINDOWS
  27. #endif // MODPLUG_TRACKER
  28. class Unix
  29. {
  30. // int64 counts 1s since 1970-01-01T00:00Z
  31. private:
  32. int64 Value;
  33. public:
  34. Unix();
  35. explicit Unix(int64 unixtime);
  36. operator int64 () const;
  37. public:
  38. static mpt::Date::Unix FromUTC(tm timeUtc);
  39. tm AsUTC() const;
  40. };
  41. mpt::ustring ToShortenedISO8601(tm date); // i.e. 2015-01-15T18:32:01Z
  42. } // namespace Date
  43. } // namespace mpt
  44. #ifdef MODPLUG_TRACKER
  45. namespace Util
  46. {
  47. #if MPT_OS_WINDOWS
  48. // RAII wrapper around timeBeginPeriod/timeEndPeriod/timeGetTime (on Windows).
  49. // This clock is monotonic, even across changing its resolution.
  50. // This is needed to synchronize time in Steinberg APIs (ASIO and VST).
  51. class MultimediaClock
  52. {
  53. private:
  54. uint32 m_CurrentPeriod;
  55. private:
  56. void Init();
  57. void SetPeriod(uint32 ms);
  58. void Cleanup();
  59. public:
  60. MultimediaClock();
  61. MultimediaClock(uint32 ms);
  62. ~MultimediaClock();
  63. public:
  64. // Sets the desired resolution in milliseconds, returns the obtained resolution in milliseconds.
  65. // A parameter of 0 causes the resolution to be reset to system defaults.
  66. // A return value of 0 means the resolution is unknown, but timestamps will still be valid.
  67. uint32 SetResolution(uint32 ms);
  68. // Returns obtained resolution in milliseconds.
  69. // A return value of 0 means the resolution is unknown, but timestamps will still be valid.
  70. uint32 GetResolution() const;
  71. // Returns current instantaneous timestamp in milliseconds.
  72. // The epoch (offset) of the timestamps is undefined but constant until the next system reboot.
  73. // The resolution is the value returned from GetResolution().
  74. uint32 Now() const;
  75. // Returns current instantaneous timestamp in nanoseconds.
  76. // The epoch (offset) of the timestamps is undefined but constant until the next system reboot.
  77. // The resolution is the value returned from GetResolution() in milliseconds.
  78. uint64 NowNanoseconds() const;
  79. };
  80. #endif // MPT_OS_WINDOWS
  81. } // namespace Util
  82. #endif // MODPLUG_TRACKER
  83. OPENMPT_NAMESPACE_END