"SfR Fresh" - the SfR Freeware/Shareware Archive

Member "procinfo-ng-2.0.217/timeRoutines.cpp" of archive procinfo-ng-2.0.217.tar.gz:


As a special service "SfR Fresh" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. That can be also achieved for any archive member file by clicking within an archive contents listing on the first character of the file(path) respectively on the according byte size field.
    1 /*
    2 	This file is part of procinfo-NG
    3 
    4 	procinfo-NG/routines.cpp is free software; you can redistribute it
    5 	and/or modify it under the terms of the GNU Lesser General Public
    6 	License as published by	the Free Software Foundation; version 2.1.
    7 
    8 	procinfo-NG is distributed in the hope that it will be useful,
    9 	but WITHOUT ANY WARRANTY; without even the implied warranty of
   10 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   11 	GNU General Public License for more details.
   12 
   13 	You should have received a copy of the GNU Lesser General Public License
   14 	along with procinfo-NG; if not, write to the Free Software
   15 	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   16  */
   17 
   18 // Procinfo-NG is Copyright tabris@tabris.net 2007, 2008
   19 
   20 #ifndef TIMEROUTINES_CPP
   21 #define TIMEROUTINES_CPP
   22 
   23 #include <time.h>
   24 #include <sys/time.h>
   25 
   26 struct timeWDHMS {
   27 	uint32_t weeks, days, hours, minutes;
   28 	double seconds;
   29 };
   30 
   31 #define secPerMin 60
   32 #define minPerHour 60
   33 #define hourPerDay 24
   34 #define dayPerWeek 7
   35 #define secPerHour secPerMin*minPerHour
   36 #define secPerDay secPerMin*minPerHour*hourPerDay
   37 #define monthPerYear 12
   38 
   39 static double getCurTime() {
   40 	struct timeval timeNow;
   41 	gettimeofday(&timeNow, NULL);
   42 	return (double(timeNow.tv_sec) + double(timeNow.tv_usec) / double(1e6));
   43 }
   44 
   45 template <typename T> const static inline struct timeWDHMS splitTime(T difference) {
   46 	struct timeWDHMS time;
   47 	time.seconds = (double)(difference % secPerMin);
   48 	difference = (difference - (T)time.seconds) / secPerMin;
   49 	time.minutes = (uint32_t)(difference % minPerHour);
   50 	difference = (difference - time.minutes) / minPerHour;
   51 	time.hours = (uint32_t)(difference % hourPerDay);
   52 	difference = (difference - time.hours) / hourPerDay;
   53 	time.days = (uint32_t)(difference % hourPerDay);
   54 	time.weeks = (uint32_t)((difference - time.days) / dayPerWeek);
   55 
   56 	return time;
   57 }
   58 const static inline struct timeWDHMS splitTime(const double &difference) {
   59 	struct timeWDHMS time;
   60 
   61 	uint64_t difference2 = (uint64_t)(difference / secPerMin);
   62 
   63 	time.seconds = (difference - (difference2 * secPerMin));
   64 	time.minutes = (uint32_t)(difference2 % minPerHour);
   65 	difference2 = (uint64_t)(difference2 - time.minutes) / minPerHour;
   66 	time.hours = (uint32_t)(difference2 % hourPerDay);
   67 	difference2 = (difference2 - time.hours) / hourPerDay;
   68 	time.days = (uint32_t)(difference2 % dayPerWeek);
   69 	time.weeks = (uint32_t)((difference2 - time.days) / dayPerWeek);
   70 
   71 	return time;
   72 }
   73 
   74 /* Nonzero if YEAR is a leap year (every 4 years,
   75    except every 100th isn't, and every 400th is).  */
   76 // In case the macro isn't available in time.h
   77 #ifndef __isleap
   78 #define __isleap(year) \
   79   !!((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
   80 #endif
   81 const static inline int get_monthdays(const int month, const int year) {
   82 	switch(month) {
   83 		case 1:
   84 		case 3:
   85 		case 5:
   86 	        case 7:
   87 		case 8:
   88 		case 10:
   89 		case 12:
   90 			return 31;
   91 		case 4:
   92 		case 6:
   93 		case 9:
   94 		case 11:
   95 			return 30;
   96 		case 2:
   97 			if( __isleap(year) ) { // macro from time.h
   98 				return 29;
   99 		        }
  100 			else
  101 				return 28;
  102 		default:
  103 	        	return 0;
  104 	}
  105 }
  106 
  107 struct timeDiff
  108 { // shamelessly stolen from time.h's struct tm and modified
  109   double tm_sec;		/* Seconds.       [0-60] (1 leap second) */
  110   int tm_min;			/* Minutes.       [0-59] */
  111   int tm_hour;			/* Hours.         [0-23] */
  112   int tm_wday;			/* Day of week.   [0-6] */
  113   int tm_week;			/* Week of month. [0-6] */
  114   int tm_mon;			/* Month.         [0-11] */
  115   int tm_year;			/* Year - 1900.  */
  116 };
  117 
  118 /*
  119    This is for cases over 4 weeks, when we need years, months, weeks, and days
  120    WARNING. This code is a straight port from some known-good Perl code.
  121    However, it has not been tested (yet) in this version.
  122 */
  123 const static inline struct timeDiff __time_rel_long(const struct tm &lesser_time, const struct tm &greater_time) {
  124 	struct timeDiff result;
  125 
  126 	result.tm_sec = greater_time.tm_sec - lesser_time.tm_sec;
  127 	result.tm_min = greater_time.tm_min - lesser_time.tm_min;
  128 	if(result.tm_sec < 0) {
  129 		result.tm_sec += secPerMin; result.tm_min--;
  130 	}
  131 	result.tm_hour = greater_time.tm_hour - lesser_time.tm_hour;
  132 	if(result.tm_min < 0) {
  133 		result.tm_min += minPerHour; result.tm_hour--;
  134 	}
  135 	result.tm_wday = greater_time.tm_mday - lesser_time.tm_mday;
  136 	if(result.tm_hour < 0) {
  137 		result.tm_hour += hourPerDay; result.tm_wday--;
  138 	}
  139 	result.tm_mon = greater_time.tm_mon - lesser_time.tm_mon;
  140 	if(result.tm_wday < 0) {
  141 		result.tm_wday += get_monthdays(
  142 			(greater_time.tm_mon == 0 ? monthPerYear - 1 : greater_time.tm_mon - 1),
  143 			(greater_time.tm_mon == 0 ? greater_time.tm_year - 1 : greater_time.tm_year));
  144 		result.tm_mon--;
  145 	}
  146 	result.tm_week = result.tm_wday / dayPerWeek;
  147 	result.tm_wday %= dayPerWeek;
  148 	result.tm_year = greater_time.tm_year - lesser_time.tm_year;
  149 	if(result.tm_mon < 0) {
  150 		result.tm_mon += monthPerYear; result.tm_year--;
  151 	}
  152 	return result;
  153 }
  154 
  155 const static inline struct timeDiff __time_rel_long(const time_t lesser_time, const time_t greater_time) {
  156 	struct tm __greater_time; gmtime_r(&greater_time, &__greater_time);
  157 	struct tm __lesser_time; gmtime_r(&lesser_time, &__lesser_time);
  158 	struct timeDiff result = __time_rel_long(__lesser_time, __greater_time);
  159 	return result;
  160 }
  161 
  162 const static inline string time_rel_abbrev(const time_t lesser_time, const time_t greater_time) {
  163 	struct timeDiff result = __time_rel_long(lesser_time, greater_time);
  164 	char output[40]; bzero(output, 40);
  165 	if(result.tm_year) {
  166 		snprintf(output, 39, "%dy", result.tm_year);
  167 	}
  168 	if(result.tm_mon) {
  169 		snprintf(output, 39, "%s%s%dm", output, (strlen(output) ? " " : ""), result.tm_mon);
  170 	}
  171 	if(result.tm_wday) {
  172 		snprintf(output, 39, "%s%s%dm", output, (strlen(output) ? " " : ""), result.tm_wday);
  173 	}
  174 	snprintf(output, 39, "%s%s%02d:%02d:%02d.%02d", output, (strlen(output) ? " " : ""),
  175 		result.tm_hour, result.tm_min, (uint32_t)result.tm_sec,
  176 		(uint32_t)((result.tm_sec - (uint32_t)result.tm_sec)*100));
  177 	return output;
  178 }
  179 
  180 const static inline string time_rel_abbrev(const time_t lesser_time) {
  181 	time_t greater_time = time(NULL);
  182 	return time_rel_abbrev(lesser_time, greater_time);
  183 }
  184 
  185 #endif