/*---------------------------------------------------------------------------* Project: Horizon File: fnd_TimeSpan.h Copyright (C)2009 Nintendo Co., Ltd. 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: 23609 $ *---------------------------------------------------------------------------*/ /*! @file @brief TimeSpan に関するAPI の宣言 */ #ifndef NN_FND_TIMESPAN_H_ #define NN_FND_TIMESPAN_H_ #include #include #include #ifdef __cplusplus namespace nn { namespace fnd { /*! @brief 時間を表すクラスです。 このクラスの内部では、64bit 整数としてナノ秒単位で時間を表しています。 SDK 内の時間を指定する関数に渡す値は、単位を間違えないためにもこのクラスを使うようになっています。 このクラスのインスタンスは From* 関数を使うことで、各単位で表された整数値から作成することができます。 単位があいまいになるのを防ぐため、整数からこの型への暗黙的な変換は用意されていませんが、 0 はこの型への暗黙的な変換をすることができます。 */ class TimeSpan { private: static const s64 TICKS_PER_SECOND = NN_HW_TICKS_PER_SECOND; typedef const class ZeroOnlyTag {} * ZeroOnly; public: /*! @brief 時間を 0 で初期化するコンストラクタです。 引数を省略した場合、デフォルトコンストラクタとなり、時間 0 で初期化されます。 引数つきのこのコンストラクタは、時間を指定するべき場所で 0 を指定できるようにするために用意されています。 0 以外の引数を与えることはできません。 @param[in] zeroOnly 引数を省略するか、0 を指定してください。 */ TimeSpan(ZeroOnly zeroOnly = 0) : m_NanoSeconds(0) { NN_UNUSED_VAR(zeroOnly); } /*! @brief ナノ秒数から TimeSpan オブジェクトを生成します。 @param[in] nanoSeconds 時間の長さをナノ秒数で指定します。 @return TimeSpan 指定された時間の長さを表す TimeSpan オブジェクトを返します。 */ static TimeSpan FromNanoSeconds(s64 nanoSeconds) { TimeSpan ret; ret.m_NanoSeconds = nanoSeconds; return ret; } /*! @brief マイクロ秒数から TimeSpan オブジェクトを生成します。 @param[in] microSeconds 時間の長さをマイクロ秒数で指定します。 @return TimeSpan 指定された時間の長さを表す TimeSpan オブジェクトを返します。 */ static TimeSpan FromMicroSeconds(s64 microSeconds) { return FromNanoSeconds(microSeconds * 1000); } /*! @brief ミリ秒数から TimeSpan オブジェクトを生成します。 @param[in] milliSeconds 時間の長さをミリ秒数で指定します。 @return TimeSpan 指定された時間の長さを表す TimeSpan オブジェクトを返します。 */ static TimeSpan FromMilliSeconds(s64 milliSeconds) { return FromNanoSeconds(milliSeconds * 1000 * 1000); } /*! @brief 秒数から TimeSpan オブジェクトを生成します。 @param[in] seconds 時間の長さを秒数で指定します。 @return TimeSpan 指定された時間の長さを表す TimeSpan オブジェクトを返します。 */ static TimeSpan FromSeconds(s64 seconds) { return FromNanoSeconds(seconds * 1000 * 1000 * 1000); } /*! @brief 分か数ら TimeSpan オブジェクトを生成します。 @param[in] minutes 時間の長さを分数で指定します。 @return TimeSpan 指定された時間の長さを表す TimeSpan オブジェクトを返します。 */ static TimeSpan FromMinutes(s64 minutes) { return FromNanoSeconds(minutes * 1000 * 1000 * 1000 * 60); } /*! @brief 時間数から TimeSpan オブジェクトを生成します。 @param[in] hours 時間の長さを時数で指定します。 @return TimeSpan 指定された時間の長さを表す TimeSpan オブジェクトを返します。 */ static TimeSpan FromHours(s64 hours) { return FromNanoSeconds(hours * 1000 * 1000 * 1000 * 60 * 60); } /*! @brief 日数から TimeSpan オブジェクトを生成します。 @param[in] days 時間(日数)を指定します。 @return TimeSpan 指定された時間の長さを表す TimeSpan オブジェクトを返します。 */ static TimeSpan FromDays(s64 days) { return FromNanoSeconds(days * 1000 * 1000 * 1000 * 60 * 60 * 24); } /*! @brief 時間を日数で取得します。 @return s64 この TimeSpan オブジェクトが表す時間の長さを日数に換算した値を返します。 */ s64 GetDays() const { return m_NanoSeconds / (1000LL * 1000 * 1000 * 60 * 60 * 24); } /*! @brief 時間を時数で取得します。 @return s64 この TimeSpan オブジェクトが表す時間の長さを時数に換算した値を返します。 */ s64 GetHours() const { return m_NanoSeconds / (1000LL * 1000 * 1000 * 60 * 60); } /*! @brief 時間を分数で取得します。 @return s64 この TimeSpan オブジェクトが表す時間の長さを分数に換算した値を返します。 */ s64 GetMinutes() const { return m_NanoSeconds / (1000LL * 1000 * 1000 * 60); } /*! @brief 時間を秒数で取得します。 @return s64 この TimeSpan オブジェクトが表す時間の長さを秒数に換算した値を返します。 */ s64 GetSeconds() const { return m_NanoSeconds / (1000 * 1000 * 1000); } /*! @brief 時間をミリ秒数で取得します。 @return TimeSpan この TimeSpan オブジェクトが表す時間の長さをミリ秒数に換算した値を返します。 */ s64 GetMilliSeconds() const { return m_NanoSeconds / (1000 * 1000); } /*! @brief 時間をマイクロ秒数で取得します。 @return TimeSpan この TimeSpan オブジェクトが表す時間の長さをマイクロ秒数に換算した値を返します。 */ s64 GetMicroSeconds() const { return m_NanoSeconds / 1000; } /*! @brief 時間をナノ秒数で取得します。 @return TimeSpan この TimeSpan オブジェクトが表す時間の長さをナノ秒数に換算した値を返します。 */ 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