bh_time.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdio.h>
  17. #include "bh_time.h"
  18. #include <time.h>
  19. #include <sys/timeb.h>
  20. /* Since GetTickCount64 is not supported on Windows XP, use a temporary method
  21. * to solve the issue. However, GetTickCount return a DWORD value and overflow
  22. * may happen (http://msdn.microsoft.com/en-us/library/ms724408(v=vs.85).aspx).
  23. *
  24. * TODO: Implement GetTickCount64 on Windows XP by self or check overflow issues.
  25. */
  26. #if (WINVER >= 0x0600)
  27. extern uint64 __stdcall GetTickCount64(void);
  28. #else
  29. extern uint32 __stdcall GetTickCount(void);
  30. #endif
  31. /*
  32. * This function returns milliseconds per tick.
  33. * @return milliseconds per tick.
  34. */
  35. uint64 _bh_time_get_tick_millisecond()
  36. {
  37. return 5;
  38. }
  39. /*
  40. * This function returns milliseconds after boot.
  41. * @return milliseconds after boot.
  42. */
  43. uint64 _bh_time_get_boot_millisecond()
  44. {
  45. /* Since GetTickCount64 is not supported on Windows XP, use a temporary method
  46. * to solve the issue. However, GetTickCount return a DWORD value and overflow
  47. * may happen (http://msdn.microsoft.com/en-us/library/ms724408(v=vs.85).aspx).
  48. *
  49. * TODO: Implement GetTickCount64 on Windows XP by self or check overflow issues.
  50. */
  51. #if (WINVER >= 0x0600)
  52. return GetTickCount64();
  53. #else
  54. return GetTickCount();
  55. #endif
  56. }
  57. /*
  58. * This function returns GMT time milliseconds since from 1970.1.1, AKA UNIX time.
  59. * @return milliseconds since from 1970.1.1.
  60. */
  61. uint64 _bh_time_get_millisecond_from_1970()
  62. {
  63. struct timeb tp;
  64. ftime(&tp);
  65. return ((uint64) tp.time) * 1000 + tp.millitm
  66. - (tp.dstflag == 0 ? 0 : 60 * 60 * 1000) + tp.timezone * 60 * 1000;
  67. }
  68. size_t bh_time_strftime(char *s, size_t max, const char *format, int64 time)
  69. {
  70. time_t time_sec = time / 1000;
  71. struct timeb tp;
  72. struct tm local_time;
  73. if (NULL == s)
  74. return 0;
  75. ftime(&tp);
  76. time_sec -= tp.timezone * 60;
  77. if (localtime_s(&local_time, &time_sec) != 0)
  78. return 0;
  79. return strftime(s, max, format, &local_time);
  80. }
  81. int bh_time_get(uint8 *timeoff_info, int16 timeoff_info_len, uint32* time)
  82. {
  83. return BH_UNIMPLEMENTED;
  84. }
  85. int bh_time_set(uint32 ntp, uint8 *timeoff_info)
  86. {
  87. return BH_UNIMPLEMENTED;
  88. }