根据Unix时间戳计算时间

前端之家收集整理的这篇文章主要介绍了根据Unix时间戳计算时间前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

根据Unix时间戳计算时间,不分大小月,每月30天,每年按360天计算。开始时间1970/01/01 00:00:00,输入秒数,显示时间

输入:10
返回:1970/01/01 00:00:10

输入:12345678
返回:1970/05/23 21:21:18

  1. string CalculationDate(long long second)
  2. {
  3. int year = 1970,month = 1,day = 1;
  4. int hour = 0,minute = 0;
  5. while(second > 60)
  6. {
  7. second -= 60;
  8. minute += 1;
  9. if (minute == 60)
  10. {
  11. minute = 0;
  12. hour += 1;
  13. if (hour == 24)
  14. {
  15. hour = 0;
  16. day += 1;
  17. if (day == 31)
  18. {
  19. day = 1;
  20. month += 1;
  21. if (month == 13)
  22. {
  23. month = 1;
  24. year += 1;
  25. }
  26. }
  27. }
  28. }
  29. }
  30. char buff[] = "1970/01/01 00:00:10";
  31. sprintf(buff,"%d/%02d/%02d %02d:%02d:%02d",year,month,day,hour,minute,second);
  32. return buff;
  33. }
  34.  
  35. void Test()
  36. {
  37. cout<<CalculationDate(10)<<endl;
  38. cout<<CalculationDate(12345678)<<endl;
  39. }
  40.  
  41. int main()
  42. {
  43. Test();
  44. system("pause");
  45. return 0;
  46. }

猜你在找的Bash相关文章