- Main Page
- Modules
- Classes
- Files

# SystemTime.h

    00001 /*
    00002  *  Phusion Passenger - http://www.modrails.com/
    00003  *  Copyright (C) 2009  Phusion
    00004  *
    00005  *  Phusion Passenger is a trademark of Hongli Lai & Ninh Bui.
    00006  *
    00007  *  This program is free software; you can redistribute it and/or modify
    00008  *  it under the terms of the GNU General Public License as published by
    00009  *  the Free Software Foundation; version 2 of the License.
    00010  *
    00011  *  This program is distributed in the hope that it will be useful,
    00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00014  *  GNU General Public License for more details.
    00015  *
    00016  *  You should have received a copy of the GNU General Public License along
    00017  *  with this program; if not, write to the Free Software Foundation, Inc.,
    00018  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    00019  */
    00020 #ifndef _PASSENGER_SYSTEM_TIME_H_
    00021 #define _PASSENGER_SYSTEM_TIME_H_
    00022
    00023 #include <boost/thread.hpp>
    00024 #include <oxt/system_calls.hpp>
    00025 #include "Exceptions.h"
    00026
    00027 namespace Passenger {
    00028
    00029 using namespace oxt;
    00030
    00031 namespace SystemTimeData {
    00032         extern bool hasForcedValue;
    00033         extern time_t forcedValue;
    00034 }
    00035
    00036 /**
    00037  * This class allows one to obtain the system time, similar to time(). Unlike
    00038  * time(), it is possible to force a certain time to be returned, which is
    00039  * useful for testing code that depends on the system time.
    00040  */
    00041 class SystemTime {
    00042 public:
    00043         /**
    00044          * Returns the time since the Epoch, measured in seconds. Or, if a time
    00045          * was forced, then the forced time is returned instead.
    00046          *
    00047          * @throws SystemException Something went wrong while retrieving the time.
    00048          * @throws boost::thread_interrupted
    00049          */
    00050         static time_t get() {
    00051                 if (SystemTimeData::hasForcedValue) {
    00052                         return SystemTimeData::forcedValue;
    00053                 } else {
    00054                         time_t ret = syscalls::time(NULL);
    00055                         if (ret == -1) {
    00056                                 throw SystemException("Unable to retrieve the system time",
    00057                                         errno);
    00058                         }
    00059                         return ret;
    00060                 }
    00061         }
    00062
    00063         /**
    00064          * Force get() to return the given value.
    00065          */
    00066         static void force(time_t value) {
    00067                 SystemTimeData::hasForcedValue = true;
    00068                 SystemTimeData::forcedValue = value;
    00069         }
    00070
    00071         /**
    00072          * Release the previously forced value, so that get()
    00073          * returns the system time once again.
    00074          */
    00075         static void release() {
    00076                 SystemTimeData::hasForcedValue = false;
    00077         }
    00078 };
    00079
    00080 } // namespace Passenger
    00081
    00082 #endif /* _PASSENGER_SYSTEM_TIME_H_ */