time_stamp.cpp

Go to the documentation of this file.
00001 #ifndef TIME_STAMP_IMPLEMENTATION_FILE
00002 #define TIME_STAMP_IMPLEMENTATION_FILE
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : time_stamp                                                        *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 1995-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include "time_stamp.h"
00019 
00020 #include <basis/function.h>
00021 #include <basis/istring.h>
00022 #include <basis/log_base.h>
00023 #include <basis/portable.h>
00024 
00025 time_stamp::time_stamp() : _stamp(0) { fill_in_time(); }
00026 
00027 time_stamp::time_stamp(time_representation offset)
00028 : _stamp(0) { reset(offset); }
00029 
00030 void time_stamp::reset() { fill_in_time(); }
00031 
00032 istring time_stamp::text_form(stamp_display_style style) const
00033 {
00034   time_representation stump = _stamp;
00035   bool past = false;
00036   if (style == STAMP_RELATIVE) {
00037     // adjust the returned time by subtracting the current time.
00038     stump -= get_time_now();
00039     if (negative(stump)) {
00040       // if we're negative, just note that the stamp is in the past.
00041       past = true;
00042       stump = absolute_value(stump);
00043     }
00044   }
00045   time_representation divisor = 3600 * SECOND_ms;
00046   u_int hours = u_int(stump / divisor);
00047   stump -= divisor * time_representation(hours);
00048   divisor /= 60;
00049   u_int minutes = u_int(stump / divisor);
00050   stump -= divisor * time_representation(minutes);
00051   divisor /= 60;
00052   u_int seconds = u_int(stump / divisor);
00053   stump -= divisor * time_representation(seconds);
00054   u_int milliseconds = u_int(stump);
00055 
00056   istring to_return;
00057   bool did_hours = false;
00058   if (hours) {
00059     to_return += istring(istring::SPRINTF, "%uh:", hours);
00060     did_hours = true;
00061   }
00062   if (minutes || did_hours)
00063     to_return += istring(istring::SPRINTF, "%02um:", minutes);
00064   to_return += istring(istring::SPRINTF, "%02us.%03u", seconds, milliseconds);
00065   if (style == STAMP_RELATIVE) {
00066     if (past) to_return += " ago";
00067     else to_return += " from now";
00068   }
00069   return to_return;
00070 }
00071 
00072 void time_stamp::fill_in_time()
00073 {
00074   time_representation current = get_time_now();
00075   _stamp = current;  // reset our own time now.
00076 }
00077 
00078 void time_stamp::reset(time_representation offset)
00079 {
00080   fill_in_time();
00081   _stamp += offset;
00082 }
00083 
00084 time_stamp::time_representation time_stamp::get_time_now()
00085 { return portable::rolling_uptime(); }
00086 
00087 
00088 #endif //TIME_STAMP_IMPLEMENTATION_FILE
00089 

Generated on Fri Nov 21 04:29:50 2008 for HOOPLE Libraries by  doxygen 1.5.1