2018-11-08 21:39:26 +08:00
// Copyright (c) 2016-2018 Easy2D - Nomango
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
# pragma once
2019-03-12 00:27:54 +08:00
# include "../macros.h"
2018-11-08 21:39:26 +08:00
namespace easy2d
{
namespace time
{
// ʱ<> <CAB1> <EFBFBD> <EFBFBD>
//
2019-01-22 10:23:12 +08:00
// ʱ<> <CAB1> <EFBFBD> α <EFBFBD> ʾ <EFBFBD> <CABE> :
// 5 <20> <> : time::Second * 5
// 1.5 Сʱ: time::Hour * 1.5
// 3 Сʱ 45 <20> <> 15 <20> <> : time::Hour * 3 + time::Minute * 45 + time::Second * 15
// <20> <> VS2015 <20> <> <EFBFBD> <EFBFBD> <EFBFBD> ߰汾<DFB0> <E6B1BE> <EFBFBD> <EFBFBD> ʹ <EFBFBD> <CAB9> time literals:
// 5 <20> <> : 5_s
// 1.5 Сʱ: 1.5_h
// 3 Сʱ 45 <20> <> 15 <20> <> : 3_h + 45_m + 15_s
2018-11-11 16:32:12 +08:00
//
2019-02-09 00:03:24 +08:00
struct E2D_API Duration
2018-11-08 21:39:26 +08:00
{
Duration ( ) ;
2019-01-14 20:38:49 +08:00
Duration (
2019-01-08 19:30:02 +08:00
long milliseconds
2018-11-08 21:39:26 +08:00
) ;
// ת<> <D7AA> Ϊ<EFBFBD> <CEAA> <EFBFBD> <EFBFBD>
2019-01-08 19:30:02 +08:00
inline long Milliseconds ( ) const { return milliseconds_ ; }
2018-11-08 21:39:26 +08:00
// ת<> <D7AA> Ϊ<EFBFBD> <CEAA>
float Seconds ( ) const ;
// ת<> <D7AA> Ϊ<EFBFBD> <CEAA> <EFBFBD> <EFBFBD>
float Minutes ( ) const ;
// ת<> <D7AA> ΪС ʱ
float Hours ( ) const ;
2018-11-14 16:39:24 +08:00
// ʱ<> <CAB1> <EFBFBD> Ƿ<EFBFBD> <C7B7> <EFBFBD> <EFBFBD> <EFBFBD>
inline bool IsZero ( ) const { return milliseconds_ = = 0LL ; }
2018-11-11 16:32:12 +08:00
// תΪ<D7AA> ַ<EFBFBD> <D6B7> <EFBFBD>
std : : wstring ToString ( ) const ;
2019-01-26 17:44:37 +08:00
inline operator bool ( ) const { return ! IsZero ( ) ; }
2018-11-08 21:39:26 +08:00
bool operator = = ( const Duration & ) const ;
bool operator ! = ( const Duration & ) const ;
bool operator > ( const Duration & ) const ;
bool operator > = ( const Duration & ) const ;
bool operator < ( const Duration & ) const ;
bool operator < = ( const Duration & ) const ;
2018-11-14 16:39:24 +08:00
float operator / ( const Duration & ) const ;
2018-11-12 22:56:47 +08:00
const Duration operator + ( const Duration & ) const ;
const Duration operator - ( const Duration & ) const ;
const Duration operator - ( ) const ;
const Duration operator * ( int ) const ;
const Duration operator * ( unsigned long long ) const ;
const Duration operator * ( float ) const ;
const Duration operator * ( double ) const ;
const Duration operator * ( long double ) const ;
const Duration operator / ( int ) const ;
const Duration operator / ( float ) const ;
const Duration operator / ( double ) const ;
2018-11-08 21:39:26 +08:00
Duration & operator + = ( const Duration & ) ;
Duration & operator - = ( const Duration & ) ;
Duration & operator * = ( int ) ;
Duration & operator * = ( float ) ;
Duration & operator * = ( double ) ;
Duration & operator / = ( int ) ;
Duration & operator / = ( float ) ;
Duration & operator / = ( double ) ;
2018-11-12 22:56:47 +08:00
friend const Duration operator * ( int , const Duration & ) ;
friend const Duration operator * ( float , const Duration & ) ;
friend const Duration operator * ( double , const Duration & ) ;
friend const Duration operator * ( long double , const Duration & ) ;
friend const Duration operator / ( int , const Duration & ) ;
friend const Duration operator / ( float , const Duration & ) ;
friend const Duration operator / ( double , const Duration & ) ;
2018-11-11 16:32:12 +08:00
friend std : : wostream & operator < < ( std : : wostream & , const Duration & ) ;
friend std : : wistream & operator > > ( std : : wistream & , Duration & ) ;
2018-11-08 21:39:26 +08:00
private :
2019-01-08 19:30:02 +08:00
long milliseconds_ ;
2018-11-08 21:39:26 +08:00
} ;
2019-01-14 20:38:49 +08:00
/* Ԥ<> <D4A4> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ʱ<EFBFBD> <CAB1> <EFBFBD> <EFBFBD> */
2019-02-09 00:03:24 +08:00
E2D_API extern const Duration Millisecond ; // <20> <> <EFBFBD> <EFBFBD>
E2D_API extern const Duration Second ; // <20> <>
E2D_API extern const Duration Minute ; // <20> <> <EFBFBD> <EFBFBD>
E2D_API extern const Duration Hour ; // Сʱ
2018-11-08 21:39:26 +08:00
// ʱ<> <CAB1>
//
2019-01-22 10:23:12 +08:00
// <20> <> ȡ<EFBFBD> <C8A1> ǰʱ<C7B0> <CAB1> : TimePoint now = time::Now();
// <20> <> ʱ<EFBFBD> <CAB1> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> , <20> õ<EFBFBD> һ <EFBFBD> <D2BB> Duration <20> <> <EFBFBD> <EFBFBD> , <20> <> <EFBFBD> <EFBFBD> :
// TimePoint t1, t2;
// int ms = (t2 - t1).Milliseconds(); // <20> <> ȡ<EFBFBD> <C8A1> ʱ<EFBFBD> <CAB1> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ĺ<EFBFBD> <C4BA> <EFBFBD> <EFBFBD> <EFBFBD>
2018-11-08 21:39:26 +08:00
//
2019-02-09 00:03:24 +08:00
struct E2D_API TimePoint
2018-11-08 21:39:26 +08:00
{
TimePoint ( ) ;
2019-01-24 15:14:55 +08:00
TimePoint ( long ) ;
2018-11-08 21:39:26 +08:00
// <20> Ƿ<EFBFBD> <C7B7> <EFBFBD> <EFBFBD> <EFBFBD> ʱ
2019-02-03 00:16:53 +08:00
inline bool IsZero ( ) const { return dur_ = = 0 ; }
2018-11-08 21:39:26 +08:00
2018-11-12 22:56:47 +08:00
const TimePoint operator + ( const Duration & ) const ;
const TimePoint operator - ( const Duration & ) const ;
2018-11-08 21:39:26 +08:00
TimePoint & operator + = ( const Duration & ) ;
TimePoint & operator - = ( const Duration & ) ;
2018-11-12 22:56:47 +08:00
const Duration operator - ( const TimePoint & ) const ;
2018-11-08 21:39:26 +08:00
private :
2019-02-03 00:16:53 +08:00
long dur_ ;
2018-11-08 21:39:26 +08:00
} ;
// <20> <> ȡ<EFBFBD> <C8A1> ǰʱ<C7B0> <CAB1>
2019-01-22 10:23:12 +08:00
//
// <20> <> <EFBFBD> ڸ<EFBFBD> ʱ<EFBFBD> <CAB1> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ϵͳ<CFB5> <CDB3> <EFBFBD> <EFBFBD> ʱ<EFBFBD> 俪ʼ <E4BFAA> <CABC> <EFBFBD> <EFBFBD> , <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <DEB7> <EFBFBD> ʽ <EFBFBD> <CABD> <EFBFBD> <EFBFBD> ʱ<EFBFBD> <CAB1> ,
// Ҳ<> <EFBFBD> <DEB7> <EFBFBD> <EFBFBD> ø<EFBFBD> ʱ<EFBFBD> <CAB1> <EFBFBD> <EFBFBD> Unix ʱ<> <CAB1> <EFBFBD> <EFBFBD>
2019-02-09 00:03:24 +08:00
E2D_API TimePoint Now ( ) E2D_NOEXCEPT ;
2018-11-08 21:39:26 +08:00
// ʱ<> <CAB1> <EFBFBD> θ<EFBFBD> ʽ <EFBFBD> <CABD>
2019-01-22 10:23:12 +08:00
//
2018-11-08 21:39:26 +08:00
// ʱ<> <CAB1> <EFBFBD> <EFBFBD> <EFBFBD> ַ<EFBFBD> <D6B7> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> з<EFBFBD> <D0B7> ŵĸ<C5B5> <C4B8> <EFBFBD> <EFBFBD> <EFBFBD> , <20> <> <EFBFBD> Ҵ<EFBFBD> <D2B4> <EFBFBD> ʱ<EFBFBD> 䵥λ<E4B5A5> <CEBB>
// <20> <> <EFBFBD> <EFBFBD> : "300ms", "-1.5h", "2h45m"
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ʱ<EFBFBD> 䵥λ<E4B5A5> <CEBB> "ms", "s", "m", "h"
2019-02-09 00:03:24 +08:00
E2D_API Duration ParseDuration ( const std : : wstring & parse_str ) ;
2018-11-08 21:39:26 +08:00
}
2018-11-11 16:32:12 +08:00
}
2018-11-14 01:34:41 +08:00
namespace easy2d
{
using namespace time ;
}
2018-11-11 16:32:12 +08:00
# if VS_VER >= VS_2015
namespace easy2d
{
inline namespace literals
{
inline const easy2d : : time : : Duration operator " " _ms ( long double val )
{
return easy2d : : time : : Millisecond * val ;
}
inline const easy2d : : time : : Duration operator " " _s ( long double val )
{
return easy2d : : time : : Second * val ;
}
inline const easy2d : : time : : Duration operator " " _m ( long double val )
{
return easy2d : : time : : Minute * val ;
}
inline const easy2d : : time : : Duration operator " " _h ( long double val )
{
return easy2d : : time : : Hour * val ;
}
inline const easy2d : : time : : Duration operator " " _ms ( unsigned long long val )
{
return easy2d : : time : : Millisecond * val ;
}
inline const easy2d : : time : : Duration operator " " _s ( unsigned long long val )
{
return easy2d : : time : : Second * val ;
}
inline const easy2d : : time : : Duration operator " " _m ( unsigned long long val )
{
return easy2d : : time : : Minute * val ;
}
inline const easy2d : : time : : Duration operator " " _h ( unsigned long long val )
{
return easy2d : : time : : Hour * val ;
}
}
namespace time
{
using namespace easy2d : : literals ;
}
}
# endif