fastdo  0.6.16
time.hpp
浏览该文件的文档.
1 #ifndef __TIME_HPP__
2 #define __TIME_HPP__
3 //
4 // time 提供日期与时间相关的功能
5 //
6 
7 namespace winux
8 {
10 inline static ulong GetSecondsFromWeeks( int weeks ) { return weeks * 7UL * 86400UL; }
12 inline static ulong GetSecondsFromDays( int days ) { return days * 86400UL; }
14 inline static ulong GetSecondsFromHours( int hours ) { return hours * 3600UL; }
16 inline static ulong GetSecondsFromMinutes( int minutes ) { return minutes * 60UL; }
17 
20 {
21 public:
23  static DateTimeL Current();
25  static DateTimeL FromSecond( time_t second );
27  static DateTimeL FromMilliSec( uint64 millisec );
29  static DateTimeL FromTm( struct tm const * t );
30 
31  struct Second
32  {
33  time_t value;
34  explicit Second( time_t second ) : value(second)
35  {
36  }
37  };
38  struct MilliSec
39  {
41  explicit MilliSec( uint64 millisec ) : value(millisec)
42  {
43  }
44  };
45 
47  DateTimeL();
49  DateTimeL( short year, short month, short day, short hour, short minute, short second, short millisec = 0 );
51  DateTimeL( Second const & utcSecond );
53  DateTimeL( MilliSec const & utcMillisec );
55  explicit DateTimeL( String const & dateTimeStr );
56 
58  short getYear() const { return _year; }
60  short getMonth() const { return _month; }
62  short getDay() const { return _day; }
64  short getHour() const { return _hour; }
66  short getMinute() const { return _minute; }
68  short getSecond() const { return _second; }
70  short getMillisec() const { return _millisec; }
72  short getDayOfWeek() const { return _wday; }
74  short getDayOfYear() const { return _yday; }
75 
77  void setYear( short year ) { _year = year; }
79  void setMonth( short month ) { _month = month; }
81  void setDay( short day ) { _day = day; }
83  void setHour( short hour ) { _hour = hour; }
85  void setMinute( short minute ) { _minute = minute; }
87  void setSecond( short second ) { _second = second; }
89  void setMillisec( short millisec ) { _millisec = millisec; }
90 
92  time_t toUtcTime() const;
94  uint64 toUtcTimeMs() const;
95 
96  template < typename _ChTy >
97  XString<_ChTy> format( XString<_ChTy> const & fmt ) const;
98 
102  #if defined(_UNICODE) || defined(UNICODE)
103  XString<wchar> format( XString<wchar> const & fmt ) const;
104  #else
105  XString<char> format( XString<char> const & fmt ) const;
106  #endif
107 
109  template <
110  #if defined(_UNICODE) || defined(UNICODE)
111  typename _ChTy = wchar
112  #else
113  typename _ChTy = char
114  #endif
115  >
116  XString<_ChTy> toString() const;
117 
119  template <
120  #if defined(_UNICODE) || defined(UNICODE)
121  typename _ChTy = wchar
122  #else
123  typename _ChTy = char
124  #endif
125  >
126  XString<_ChTy> toGmtString() const;
127 
129  DateTimeL & current();
131  DateTimeL & fromTm( struct tm const * t );
132 
133 private:
134  short _millisec;
135  short _second;
136  short _minute;
137  short _hour;
138  short _day;
139  short _month;
140  short _year;
141  short _wday;
142  short _yday;
143 };
144 
145 template <>
147 template <>
149 
150 #if defined(_UNICODE) || defined(UNICODE)
151 inline XString<wchar> DateTimeL::format( XString<wchar> const & fmt ) const { return this->format<wchar>(fmt); }
152 #else
153 inline XString<char> DateTimeL::format( XString<char> const & fmt ) const { return this->format<char>(fmt); }
154 #endif
155 
156 template <>
158 template <>
160 
161 template <>
163 template <>
165 
166 WINUX_FUNC_DECL(std::ostream &) operator << ( std::ostream & o, DateTimeL const & dt );
167 WINUX_FUNC_DECL(std::wostream &) operator << ( std::wostream & o, DateTimeL const & dt );
168 
174 WINUX_FUNC_DECL(time_t) GetUtcTime( void );
175 
176 
177 
178 } // namespace winux
179 
180 #endif // __TIME_HPP__
本地日期时间。L意思&#39;local&#39;,不包含时区信息
Definition: time.hpp:19
void setHour(short hour)
设置时[0~23]
Definition: time.hpp:83
static ulong GetSecondsFromMinutes(int minutes)
获取几分钟是多少秒
Definition: time.hpp:16
#define WINUX_DLL
Definition: utilities.hpp:60
wchar_t wchar
Definition: utilities.hpp:229
void setMonth(short month)
设置月[1~12]
Definition: time.hpp:79
XString< tchar > String
Definition: utilities.hpp:261
MilliSec(uint64 millisec)
Definition: time.hpp:41
short getDayOfYear() const
年第几日[1~366, 1=01-01]
Definition: time.hpp:74
void setDay(short day)
设置日[1~31]
Definition: time.hpp:81
Second(time_t second)
Definition: time.hpp:34
std::ostream & operator<<(std::ostream &out, ConsoleAttrT< _VarType > const &tr)
Definition: console.hpp:131
XString< _ChTy > toString() const
转为时间串
static ulong GetSecondsFromWeeks(int weeks)
获取几周是多少秒
Definition: time.hpp:10
std::basic_string< _ChTy, std::char_traits< _ChTy >, std::allocator< _ChTy > > XString
Definition: utilities.hpp:255
time_t GetUtcTime(void)
获取UTC时间秒数,或者调用CRT的time(NULL)
short getHour() const
时[0~23]
Definition: time.hpp:64
void setSecond(short second)
设置秒[0~59]
Definition: time.hpp:87
XString< _ChTy > format(XString< _ChTy > const &fmt) const
#define WINUX_FUNC_DECL(ret)
Definition: utilities.hpp:64
short getSecond() const
秒[0~59]
Definition: time.hpp:68
uint64 GetUtcTimeMs(void)
获取UTC时间毫秒数,UTC秒数可以直接除以1000,或者调用CRT的time(NULL)
short getMonth() const
月[1~12]
Definition: time.hpp:60
short getYear() const
年[1~2038]
Definition: time.hpp:58
uint64 GetUtcTimeUs(void)
获取UTC时间微秒数,UTC秒数可以直接除以1000000,或者调用CRT的time(NULL)
short getMillisec() const
毫秒[0~999]
Definition: time.hpp:70
static ulong GetSecondsFromHours(int hours)
获取几小时是多少秒
Definition: time.hpp:14
static ulong GetSecondsFromDays(int days)
获取几天是多少秒
Definition: time.hpp:12
short getDayOfWeek() const
星期几[0~6, 0=Sunday]
Definition: time.hpp:72
void setMillisec(short millisec)
设置毫秒[0~999]
Definition: time.hpp:89
unsigned long ulong
Definition: utilities.hpp:216
void setMinute(short minute)
设置分[0~59]
Definition: time.hpp:85
unsigned __int64 uint64
Definition: utilities.hpp:230
short getMinute() const
分[0~59]
Definition: time.hpp:66
short getDay() const
日[1~31]
Definition: time.hpp:62
跨平台基础功能库
Definition: archives.hpp:7
XString< _ChTy > toGmtString() const
转为GMT时间串,可用在HTTP头里
void setYear(short year)
设置年[1~2038]
Definition: time.hpp:77