1 /*---------------------------------------------------------------------------*
2 Project: Horizon
3 File: fnd_TimeSpan.h
4 
5 Copyright (C) Nintendo.  All rights reserved.
6 
7 These coded instructions, statements, and computer programs contain
8 proprietary information of Nintendo of America Inc. and/or Nintendo
9 Company Ltd., and are protected by Federal copyright law.  They may
10 not be disclosed to third parties or copied or duplicated in any form,
11 in whole or in part, without the prior written consent of Nintendo.
12 
13 $Rev: 27772 $
14  *---------------------------------------------------------------------------*/
15 
16 /*! @file
17 @brief  These are API declarations for <tt>TimeSpan</tt>.
18 
19 */
20 
21 #ifndef NN_FND_TIMESPAN_H_
22 #define NN_FND_TIMESPAN_H_
23 
24 #include <nn/types.h>
25 #include <nn/config.h>
26 
27 #ifdef __cplusplus
28 
29 namespace nn { namespace fnd {
30 
31 /*!
32 @brief  Represents time.
33 
34 Within this class, time is expressed in nanoseconds using 64-bit integers.
35 This class is used to prevent unit mistakes in the values that are passed to functions that specify time within the SDK.
36 The <tt>From*</tt> functions allow you to create instances of this class from integer values of the various units.
37 To prevent ambiguity in units, no implicit conversion from integers to this data type is provided. However, <tt>0</tt> can be converted implicitly to this data type.
38 
39 */
40 class TimeSpan
41 {
42 private:
43 
44     typedef const class ZeroOnlyTag {} * ZeroOnly;
45 
46 public:
47 
48     /*!
49 @brief  Instantiates the object and initializes the time to 0.
50 
51 If the argument is omitted, it acts as the default constructor and initializes the time to 0.
52 This constructor with an argument is provided to make it possible to specify 0 in places where the time must be specified.
53 No arguments except 0 can be passed to this function.
54 
55 @param[in] zeroOnly  Either omit this argument or set it to zero.
56 */
57     TimeSpan(ZeroOnly zeroOnly = 0) : m_NanoSeconds(0) { NN_UNUSED_VAR(zeroOnly); }
58 
59     /*!
60 @brief  Generates a <tt>TimeSpan</tt> object from a nanoseconds value.
61 
62 @param[in] nanoSeconds  Specifies the length of time in nanoseconds.
63 
64 @return  Returns a <tt>TimeSpan</tt> object representing the specified length of time.
65 */
FromNanoSeconds(s64 nanoSeconds)66     static TimeSpan FromNanoSeconds(s64 nanoSeconds) { TimeSpan ret; ret.m_NanoSeconds = nanoSeconds; return ret; }
67 
68     /*!
69 @brief  Generates a <tt>TimeSpan</tt> object from a microseconds value.
70 
71 @param[in] microSeconds  Specifies the length of time in microseconds.
72 
73 @return  Returns a <tt>TimeSpan</tt> object representing the specified length of time.
74 */
FromMicroSeconds(s64 microSeconds)75     static TimeSpan FromMicroSeconds(s64 microSeconds) { return FromNanoSeconds(microSeconds * 1000); }
76 
77     /*!
78 @brief  Generates a <tt>TimeSpan</tt> object from a milliseconds value.
79 
80 @param[in] milliSeconds  Specifies the length of time in milliseconds.
81 
82 @return  Returns a <tt>TimeSpan</tt> object representing the specified length of time.
83 */
FromMilliSeconds(s64 milliSeconds)84     static TimeSpan FromMilliSeconds(s64 milliSeconds) { return FromNanoSeconds(milliSeconds * 1000 * 1000); }
85 
86     /*!
87 @brief  Generates a <tt>TimeSpan</tt> object from a seconds value.
88 
89 @param[in] seconds  Specifies the length of time in seconds.
90 
91 @return  Returns a <tt>TimeSpan</tt> object representing the specified length of time.
92 */
FromSeconds(s64 seconds)93     static TimeSpan FromSeconds(s64 seconds) { return FromNanoSeconds(seconds * 1000 * 1000 * 1000); }
94 
95     /*!
96 @brief  Generates a <tt>TimeSpan</tt> object from a minutes value.
97 
98 @param[in] minutes  Specifies the length of time in minutes.
99 
100 @return  Returns a <tt>TimeSpan</tt> object representing the specified length of time.
101 */
FromMinutes(s64 minutes)102     static TimeSpan FromMinutes(s64 minutes) { return FromNanoSeconds(minutes * 1000 * 1000 * 1000 * 60); }
103 
104     /*!
105 @brief  Generates a <tt>TimeSpan</tt> object from an hours value.
106 
107 @param[in] hours  Specifies the length of time in hours.
108 
109 @return  Returns a <tt>TimeSpan</tt> object representing the specified length of time.
110 */
FromHours(s64 hours)111     static TimeSpan FromHours(s64 hours) { return FromNanoSeconds(hours * 1000 * 1000 * 1000 * 60 * 60); }
112 
113     /*!
114 @brief  Generates a <tt>TimeSpan</tt> object from a days value.
115 
116 @param[in] days  Specifies the time in days.
117 
118 @return  Returns a <tt>TimeSpan</tt> object representing the specified length of time.
119 */
FromDays(s64 days)120     static TimeSpan FromDays(s64 days) { return FromNanoSeconds(days * 1000 * 1000 * 1000 * 60 * 60 * 24); }
121 
122     /*!
123 @brief  Gets the time in days.
124 
125 @return  Returns the length of time represented by this <tt>TimeSpan</tt> object, converted into a number of days.
126 */
GetDays()127     s64 GetDays() const { return m_NanoSeconds / (1000LL * 1000 * 1000 * 60 * 60 * 24); }
128 
129     /*!
130 @brief  Gets the time in hours.
131 
132 @return  Returns the length of time represented by this <tt>TimeSpan</tt> object, converted into a number of hours.
133 */
GetHours()134     s64 GetHours() const { return m_NanoSeconds / (1000LL * 1000 * 1000 * 60 * 60); }
135 
136     /*!
137 @brief  Gets the time in minutes.
138 
139 @return  Returns the length of time represented by this <tt>TimeSpan</tt> object, converted into a number of minutes.
140 */
GetMinutes()141     s64 GetMinutes() const { return m_NanoSeconds / (1000LL * 1000 * 1000 * 60); }
142 
143     /*!
144 @brief  Gets the time in seconds.
145 
146 @return  Returns the length of time represented by this <tt>TimeSpan</tt> object, converted into a number of seconds.
147 */
GetSeconds()148     s64 GetSeconds() const { return m_NanoSeconds / (1000 * 1000 * 1000); }
149     /*!
150 @brief  Gets the time in milliseconds.
151 
152 @return  Returns the length of time represented by this <tt>TimeSpan</tt> object, converted into a number of milliseconds.
153 */
GetMilliSeconds()154     s64 GetMilliSeconds() const { return m_NanoSeconds / (1000 * 1000); }
155     /*!
156 @brief  Gets the time in microseconds.
157 
158 @return  Returns the length of time represented by this <tt>TimeSpan</tt> object, converted into a number of microseconds.
159 */
GetMicroSeconds()160     s64 GetMicroSeconds() const { return m_NanoSeconds / 1000; }
161     /*!
162 @brief  Gets the time in nanoseconds.
163 
164 @return  Returns the length of time represented by this <tt>TimeSpan</tt> object, converted into a number of nanoseconds.
165 */
GetNanoSeconds()166     s64 GetNanoSeconds() const { return m_NanoSeconds; }
167 
168     friend bool operator==(const TimeSpan& lhs, const TimeSpan& rhs) { return lhs.m_NanoSeconds == rhs.m_NanoSeconds; }
169     friend bool operator!=(const TimeSpan& lhs, const TimeSpan& rhs) { return !(lhs == rhs); }
170     friend bool operator< (const TimeSpan& lhs, const TimeSpan& rhs) { return lhs.m_NanoSeconds <  rhs.m_NanoSeconds; }
171     friend bool operator> (const TimeSpan& lhs, const TimeSpan& rhs) { return rhs < lhs; }
172     friend bool operator<=(const TimeSpan& lhs, const TimeSpan& rhs) { return !(lhs > rhs); }
173     friend bool operator>=(const TimeSpan& lhs, const TimeSpan& rhs) { return !(lhs < rhs); }
174 
175     TimeSpan& operator+=(const TimeSpan& rhs) { this->m_NanoSeconds += rhs.m_NanoSeconds; return *this; }
176     friend TimeSpan operator+(const TimeSpan& lhs, const TimeSpan& rhs) { TimeSpan ret(lhs); return ret += rhs; }
177 
178     TimeSpan& operator-=(const TimeSpan& rhs) { this->m_NanoSeconds -= rhs.m_NanoSeconds; return *this; }
179     friend TimeSpan operator-(const TimeSpan& lhs, const TimeSpan& rhs) { TimeSpan ret(lhs); return ret -= rhs; }
180 
181 private:
182 
183     s64 m_NanoSeconds;
184 
185 };
186 
187 }}	// end of namespace nn
188 
189 #endif // __cplusplus
190 
191 #endif
192