bh_time.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <stdio.h>
  6. #include "bh_time.h"
  7. #include <time.h>
  8. #include <sys/timeb.h>
  9. /* Since GetTickCount64 is not supported on Windows XP, use a temporary method
  10. * to solve the issue. However, GetTickCount return a DWORD value and overflow
  11. * may happen (http://msdn.microsoft.com/en-us/library/ms724408(v=vs.85).aspx).
  12. *
  13. * TODO: Implement GetTickCount64 on Windows XP by self or check overflow issues.
  14. */
  15. #if (WINVER >= 0x0600)
  16. extern uint64 __stdcall GetTickCount64(void);
  17. #else
  18. extern uint32 __stdcall GetTickCount(void);
  19. #endif
  20. /*
  21. * This function returns milliseconds per tick.
  22. * @return milliseconds per tick.
  23. */
  24. uint64 _bh_time_get_tick_millisecond()
  25. {
  26. return 5;
  27. }
  28. /*
  29. * This function returns milliseconds after boot.
  30. * @return milliseconds after boot.
  31. */
  32. uint64 _bh_time_get_boot_millisecond()
  33. {
  34. /* Since GetTickCount64 is not supported on Windows XP, use a temporary method
  35. * to solve the issue. However, GetTickCount return a DWORD value and overflow
  36. * may happen (http://msdn.microsoft.com/en-us/library/ms724408(v=vs.85).aspx).
  37. *
  38. * TODO: Implement GetTickCount64 on Windows XP by self or check overflow issues.
  39. */
  40. #if (WINVER >= 0x0600)
  41. return GetTickCount64();
  42. #else
  43. return GetTickCount();
  44. #endif
  45. }
  46. /*
  47. * This function returns GMT time milliseconds since from 1970.1.1, AKA UNIX time.
  48. * @return milliseconds since from 1970.1.1.
  49. */
  50. uint64 _bh_time_get_millisecond_from_1970()
  51. {
  52. struct timeb tp;
  53. ftime(&tp);
  54. return ((uint64) tp.time) * 1000 + tp.millitm
  55. - (tp.dstflag == 0 ? 0 : 60 * 60 * 1000) + tp.timezone * 60 * 1000;
  56. }
  57. size_t bh_time_strftime(char *s, size_t max, const char *format, int64 time)
  58. {
  59. time_t time_sec = time / 1000;
  60. struct timeb tp;
  61. struct tm local_time;
  62. if (NULL == s)
  63. return 0;
  64. ftime(&tp);
  65. time_sec -= tp.timezone * 60;
  66. if (localtime_s(&local_time, &time_sec) != 0)
  67. return 0;
  68. return strftime(s, max, format, &local_time);
  69. }
  70. int bh_time_get(uint8 *timeoff_info, int16 timeoff_info_len, uint32* time)
  71. {
  72. return BH_UNIMPLEMENTED;
  73. }
  74. int bh_time_set(uint32 ntp, uint8 *timeoff_info)
  75. {
  76. return BH_UNIMPLEMENTED;
  77. }