Advertisement
C0BRA

Format

Apr 23rd, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. inline std::string FormatTimeSpan(double Time)
  2. {
  3.     using namespace std;
  4.  
  5.     string names[] = {
  6.         "second",
  7.         "minute",
  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, // year, years in...
  21.         1
  22.     };
  23.  
  24.     string ret;
  25.     double units = Time;
  26.  
  27.     for(int i = 0; i < sizeof(ajustment) / sizeof(ajustment[0]) - 1; i++)
  28.     {
  29.         long long unit = (long long)fmod(units, ajustment[i+1]);
  30.         units = units / ajustment[i+1];
  31.  
  32.         string unit_name = names[i];
  33.  
  34.         if(unit != 1)
  35.             unit_name.append("s");
  36.  
  37.         string str_unit;
  38.         stringstream str;
  39.         str << unit;
  40.         str >> str_unit;
  41.        
  42.         ret = str_unit + " " + unit_name + " " + ret;
  43.  
  44.         if(units < 1)
  45.             break;
  46.     }
  47.  
  48.     return ret;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement