Advertisement
C0BRA

Time span format?

Apr 22nd, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. inline std::string FormatTimeSpan(double Time)
  2. {
  3.     using namespace std;
  4.  
  5.     string names[] = {
  6.         "second",
  7.         "miniute",
  8.         "hour",
  9.         "day",
  10.         "week",
  11.         "year"
  12.     };
  13.  
  14.     double ajustment[] = {
  15.         60, // seconds
  16.         60, // minutes, minuets in
  17.         60, // hour, hours in
  18.         24, // day, days in
  19.         7,  // week, weeks in,
  20.         52
  21.     };
  22.  
  23.     string ret;
  24.     double units = Time;
  25.  
  26.     for(int i = 0; i < sizeof(names); i++)
  27.     {
  28.         long long unit = fmod(units, ajustment[i+1]);
  29.         units = units / ajustment[i+1];
  30.  
  31.         string unit_name = names[i];
  32.  
  33.         if(unit != 1)
  34.             unit_name.append("s");
  35.  
  36.         string str_unit;
  37.         stringstream str;
  38.         str << unit;
  39.         str >> str_unit;
  40.        
  41.         ret = str_unit + " " + unit_name + " " + ret;
  42.  
  43.         if(units < 1)
  44.             break;
  45.     }
  46.  
  47.     return ret;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement