timefmt.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include <precomp.h>
  2. #include <bfc/wasabi_std.h>
  3. #include <time.h>
  4. #include "timefmt.h"
  5. void TimeFmt::printMinSec(int sec, wchar_t *buf, int buflen)
  6. {
  7. int minutes, seconds;
  8. int negative = sec < 0;
  9. if (buf == NULL) return;
  10. if (sec == -1)
  11. {
  12. *buf = 0;
  13. return;
  14. }
  15. seconds = sec % 60;
  16. sec /= 60;
  17. minutes = sec;
  18. StringPrintfW sp(L"%s%d:%02d", (minutes == 0 && negative) ? L"-" : L"", minutes, ABS(seconds));
  19. WCSCPYN(buf, sp, buflen);
  20. }
  21. void TimeFmt::printHourMinSec(int sec, wchar_t *buf, int buflen, int hoursonlyifneeded)
  22. {
  23. int hours, minutes, seconds;
  24. int negative = sec < 0;
  25. sec = ABS(sec);
  26. if (buf == NULL) return;
  27. if (sec == -1) {
  28. *buf = 0;
  29. return;
  30. }
  31. hours = sec / 3600;
  32. sec -= hours * 3600;
  33. seconds = sec % 60;
  34. sec /= 60;
  35. minutes = sec;
  36. StringW sp;
  37. if (hoursonlyifneeded && hours == 0)
  38. sp = StringPrintfW(L"%s%d:%02d", (minutes == 0 && negative) ? L"-" : L"", minutes, seconds);
  39. else
  40. sp = StringPrintfW(L"%s%d:%02d:%02d", (hours == 0 && negative) ? L"-" : L"", hours, minutes, seconds);
  41. WCSCPYN(buf, sp, buflen);
  42. }
  43. void TimeFmt::printTimeStamp(wchar_t *buf, int bufsize, int ts)
  44. {
  45. if (ts == 0)
  46. {
  47. WCSCPYN(buf, L"Never", bufsize); // FUCKO: load from lang pack
  48. return;
  49. }
  50. struct tm *tm_now;
  51. tm_now = localtime((const time_t *)&ts);
  52. if (tm_now == NULL)
  53. {
  54. *buf = 0;
  55. return;
  56. }
  57. wcsftime(buf, bufsize, L"%a %b %Y %d %I:%M:%S %p", tm_now);
  58. }