/*---------------------------------------------------------------------------* Project: Horizon File: fnd_TimeSpan.h Copyright (C) 2009-2011 Nintendo. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Rev: 27772 $ *---------------------------------------------------------------------------*/ /*! @file @brief These are API declarations for TimeSpan. */ #ifndef NN_FND_TIMESPAN_H_ #define NN_FND_TIMESPAN_H_ #include #include #ifdef __cplusplus namespace nn { namespace fnd { /*! @brief Represents time. Within this class, time is expressed in nanoseconds using 64-bit integers. This class is used to prevent unit mistakes in the values that are passed to functions that specify time within the SDK. The From* functions allow you to create instances of this class from integer values of the various units. To prevent ambiguity in units, no implicit conversion from integers to this data type is provided. However, 0 can be converted implicitly to this data type. */ class TimeSpan { private: typedef const class ZeroOnlyTag {} * ZeroOnly; public: /*! @brief Instantiates the object and initializes the time to 0. If the argument is omitted, it acts as the default constructor and initializes the time to 0. This constructor with an argument is provided to make it possible to specify 0 in places where the time must be specified. No arguments except 0 can be passed to this function. @param[in] zeroOnly Either omit this argument or set it to zero. */ TimeSpan(ZeroOnly zeroOnly = 0) : m_NanoSeconds(0) { NN_UNUSED_VAR(zeroOnly); } /*! @brief Generates a TimeSpan object from a nanoseconds value. @param[in] nanoSeconds Specifies the length of time in nanoseconds. @return Returns a TimeSpan object representing the specified length of time. */ static TimeSpan FromNanoSeconds(s64 nanoSeconds) { TimeSpan ret; ret.m_NanoSeconds = nanoSeconds; return ret; } /*! @brief Generates a TimeSpan object from a microseconds value. @param[in] microSeconds Specifies the length of time in microseconds. @return Returns a TimeSpan object representing the specified length of time. */ static TimeSpan FromMicroSeconds(s64 microSeconds) { return FromNanoSeconds(microSeconds * 1000); } /*! @brief Generates a TimeSpan object from a milliseconds value. @param[in] milliSeconds Specifies the length of time in milliseconds. @return Returns a TimeSpan object representing the specified length of time. */ static TimeSpan FromMilliSeconds(s64 milliSeconds) { return FromNanoSeconds(milliSeconds * 1000 * 1000); } /*! @brief Generates a TimeSpan object from a seconds value. @param[in] seconds Specifies the length of time in seconds. @return Returns a TimeSpan object representing the specified length of time. */ static TimeSpan FromSeconds(s64 seconds) { return FromNanoSeconds(seconds * 1000 * 1000 * 1000); } /*! @brief Generates a TimeSpan object from a minutes value. @param[in] minutes Specifies the length of time in minutes. @return Returns a TimeSpan object representing the specified length of time. */ static TimeSpan FromMinutes(s64 minutes) { return FromNanoSeconds(minutes * 1000 * 1000 * 1000 * 60); } /*! @brief Generates a TimeSpan object from an hours value. @param[in] hours Specifies the length of time in hours. @return Returns a TimeSpan object representing the specified length of time. */ static TimeSpan FromHours(s64 hours) { return FromNanoSeconds(hours * 1000 * 1000 * 1000 * 60 * 60); } /*! @brief Generates a TimeSpan object from a days value. @param[in] days Specifies the time in days. @return Returns a TimeSpan object representing the specified length of time. */ static TimeSpan FromDays(s64 days) { return FromNanoSeconds(days * 1000 * 1000 * 1000 * 60 * 60 * 24); } /*! @brief Gets the time in days. @return Returns the length of time represented by this TimeSpan object, converted into a number of days. */ s64 GetDays() const { return m_NanoSeconds / (1000LL * 1000 * 1000 * 60 * 60 * 24); } /*! @brief Gets the time in hours. @return Returns the length of time represented by this TimeSpan object, converted into a number of hours. */ s64 GetHours() const { return m_NanoSeconds / (1000LL * 1000 * 1000 * 60 * 60); } /*! @brief Gets the time in minutes. @return Returns the length of time represented by this TimeSpan object, converted into a number of minutes. */ s64 GetMinutes() const { return m_NanoSeconds / (1000LL * 1000 * 1000 * 60); } /*! @brief Gets the time in seconds. @return Returns the length of time represented by this TimeSpan object, converted into a number of seconds. */ s64 GetSeconds() const { return m_NanoSeconds / (1000 * 1000 * 1000); } /*! @brief Gets the time in milliseconds. @return Returns the length of time represented by this TimeSpan object, converted into a number of milliseconds. */ s64 GetMilliSeconds() const { return m_NanoSeconds / (1000 * 1000); } /*! @brief Gets the time in microseconds. @return Returns the length of time represented by this TimeSpan object, converted into a number of microseconds. */ s64 GetMicroSeconds() const { return m_NanoSeconds / 1000; } /*! @brief Gets the time in nanoseconds. @return Returns the length of time represented by this TimeSpan object, converted into a number of nanoseconds. */ s64 GetNanoSeconds() const { return m_NanoSeconds; } friend bool operator==(const TimeSpan& lhs, const TimeSpan& rhs) { return lhs.m_NanoSeconds == rhs.m_NanoSeconds; } friend bool operator!=(const TimeSpan& lhs, const TimeSpan& rhs) { return !(lhs == rhs); } friend bool operator< (const TimeSpan& lhs, const TimeSpan& rhs) { return lhs.m_NanoSeconds < rhs.m_NanoSeconds; } friend bool operator> (const TimeSpan& lhs, const TimeSpan& rhs) { return rhs < lhs; } friend bool operator<=(const TimeSpan& lhs, const TimeSpan& rhs) { return !(lhs > rhs); } friend bool operator>=(const TimeSpan& lhs, const TimeSpan& rhs) { return !(lhs < rhs); } TimeSpan& operator+=(const TimeSpan& rhs) { this->m_NanoSeconds += rhs.m_NanoSeconds; return *this; } friend TimeSpan operator+(const TimeSpan& lhs, const TimeSpan& rhs) { TimeSpan ret(lhs); return ret += rhs; } TimeSpan& operator-=(const TimeSpan& rhs) { this->m_NanoSeconds -= rhs.m_NanoSeconds; return *this; } friend TimeSpan operator-(const TimeSpan& lhs, const TimeSpan& rhs) { TimeSpan ret(lhs); return ret -= rhs; } private: s64 m_NanoSeconds; }; }} // end of namespace nn #endif // __cplusplus #endif