vc ++ 常用CTime相关

CTime 值基于协调世界时(UTC),相当于协调世界时(格林尼治标准时间,GMT)

CTime:: CTime	以多种方式构造 CTime 的对象。

CTime() throw();
CTime(__time64_t time) throw();
CTime(int nYear, int nMonth, int nDay,
      int nHour, int nMin, int nSec, int nDST = -1);
CTime(WORD wDosDate, WORD wDosTime, int nDST = -1);
CTime(const SYSTEMTIME& st, int nDST = - 1) throw();
CTime(const FILETIME& ft, int nDST = - 1);
CTime(const DBTIMESTAMP& dbts, int nDST = -1) throw();

CString 转 CTime


char *chLineData =  "2020-03-24 21:39:40";
sscanf_s(chLineData,"%4d-%2d-%2d %2d:%2d:%2d",&nYear, &nMonth, &nDate, &nHour, &nMin, &nSec);
// 判断是否是有效
nYear > 1970 && nYear < 3000 ? nYear : nYear = 2018;
nMonth > 0 && nMonth <= 12   ? nMonth : nMonth = 1;
nDate > 0 && nDate <= 31 ? nDate : nDate = 1;
nHour >= 0 && nHour < 24 ? nHour : nHour = 0;
nMin >= 0 && nMin < 60 ? nMin : nMin = 0;
nSec >= 0 && nSec < 60 ? nSec : nSec = 0;

CTime cTime = CTime(nYear, nMonth, nDate, nHour, nMin, nSec);



CString strTime = CTime::GetCurrentTime().Format(_T("%4d-%2d-%2d %2d:%2d:%2d"));

time_t 构造 CTime

time_t osBinaryTime;  // C run-time time (defined in <time.h>)
time(&osBinaryTime) ;  // Get the current time from the 
                         // operating system.
CTime time1; // Empty CTime. (0 is illegal time value.)
CTime time2 = time1; // Copy constructor.
CTime time3(osBinaryTime);  // CTime from C run-time time


char   str[128];
tm	   time;
time_t date = (time_t)lTime;
localtime_s(&time, &date);
strftime(str, sizeof(str), "%F %T", &time);
CString stTime= str;


公共方法
名称	说明
CTime:: Format	根据本地时区,将 CTime 对象转换为带格式的字符串。
CTime:: FormatGmt	将 CTime 对象转换为基于 UTC 的格式化字符串。
CTime:: GetAsDBTIMESTAMP	将 CTime 对象中存储的时间信息转换为与 Win32 兼容的 DBTIMESTAMP 结构。
CTime:: GetAsSystemTime	将 CTime 对象中存储的时间信息转换为与 Win32 兼容的SYSTEMTIME结构。
CTime:: GetCurrentTime	创建一个表示当前时间的 CTime 对象(静态成员函数)。
CTime:: GetDay	返回 CTime 对象表示的日期。
CTime:: GetDayOfWeek	返回 CTime 对象所表示的周中的某一天。
CTime:: GetGmtTm	基于 UTC 将 CTime 对象分解为组件。
CTime:: GetHour	返回 CTime 对象所表示的小时。
CTime:: GetLocalTm	根据本地时区,将 CTime 对象分解为个组件。
CTime:: GetMinute	返回 CTime 对象表示的分钟数。
CTime:: GetMonth	返回 CTime 对象所表示的月份。
CTime:: GetSecond	返回 CTime 对象表示的第二个。
CTime:: GetTime	返回给定 CTime 对象的 __time64_t值。
CTime:: GetYear	返回 CTime 对象所表示的年份。
CTime:: Serialize64	在存档中序列化数据。

运算符
operator +-	这些运算符添加并减去 CTimeSpan 和 CTime 对象。
operator + =,-=	这些运算符在此 CTime 对象中增加和减少一个 CTimeSpan 对象。
operator =	赋值运算符。
operator = =、< 等。	比较运算符。

CTime::Format

CTime Format

%a  —— 星期(缩写英文),如Fri; 
%A  —— 星期(全写英文),如Friday 
%b  —— 月份(缩写英文),如Oct 
%B  —— 月份(全写英文),如 October 
%c  —— 月/日/年 时:分:秒,如 10/13/06 19:17:17 
%d  —— 日期(1 ~ 31) 
%H  —— 时(24小时制)(0 ~ 23) 
%I  —— 时(12小时制)(0 ~ 12) 
%j  —— 一年当中的第几天,(1 ~ 366) 
%m  —— 月份(数字 1 ~ 12) 
%M  —— 分(0 ~ 59) 
%p  —— 12小时中的A M/PM指示,或者AM,或者PM 
%S  —— 秒(0 ~ 59) 
%U  —— 一年中的第几周,星期日作为每周的第一天(0 ~ 53) 
%w  —— 星期(数字表示,0 ~ 6,0代表星期日) 
%W  —— 一年中的第几周,星期一作为每周的第一天(0 ~ 53) 
%x  —— 月/日/年,%c的前半段

CTime:: GetAsSystemTime

typedef struct _SYSTEMTIME {
  WORD wYear;
  WORD wMonth;
  WORD wDayOfWeek;        //星期几
  WORD wDay;
  WORD wHour;
  WORD wMinute;
  WORD wSecond;
  WORD wMilliseconds;     // 毫秒
} SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;



#include <windows.h>
#include <stdio.h>

void main()
{
    SYSTEMTIME st, lt;
    
    GetSystemTime(&st);
    GetLocalTime(&lt);
    
    printf("The system time is: %02d:%02d\n", st.wHour, st.wMinute);
    printf(" The local time is: %02d:%02d\n", lt.wHour, lt.wMinute);
}

CTime:: GetGmtTm

struct tm* GetGmtTm(struct tm* ptm) const;
struct tm* GetLocalTm(struct tm* ptm) const;


struct tm *gmtime( const time_t *sourceTime );
struct tm *_gmtime32( const __time32_t *sourceTime );
struct tm *_gmtime64( const __time64_t *sourceTime );

CTime::GetTime

CTime t(2005, 10, 20, 23, 50, 0); // 11:50 PM October 20, 2005
time_t osBinaryTime = t.GetTime();  // time_t defined in <time.h>

伴生类CTimeSpan表示时间间隔。

CDateTimeCtrl 控件类型

CloseMonthCal	关闭当前日期和时间选取器控件。
Create	        创建日期和时间选取器控件,并将其附加到 CDateTimeCtrl 对象上。
GetDateTimePickerInfo	检索有关当前日期和时间选择器控件的信息。
GetIdealSize	返回显示当前日期或时间所需的日期和时间选取器控件的理想大小。
GetMonthCalColor	检索日期和时间选取器控件内月历的给定部分的颜色。
GetMonthCalCtrl	检索与日期和时间选取器控件关联的 CMonthCalCtrl 对象。
GetMonthCalFont	检索日期和时间选择器控件的子月历控件当前使用的字体。
GetMonthCalStyle	获取当前日期和时间选择器控件的样式。
GetRange	检索日期和时间选择器控件的当前最小和最大允许系统时间。

GetTime	从日期和时间选取器控件中检索当前选定的时间,并将其放入指定的 SYSTEMTIME 结构。

SetFormat	根据给定的格式字符串,设置日期和时间选择器控件的显示。
SetMonthCalColor	设置日期和时间选取器控件内月历的给定部分的颜色。
SetMonthCalFont	设置日期和时间选择器控件的子月历控件将使用的字体。
SetMonthCalStyle	设置当前日期和时间选取器控件的样式。
SetRange	设置日期和时间选取器控件所允许的最小和最大系统时间。

SetTime	设置日期和时间选取器控件中的时间。


CTime  starttime1;
m_starttime1.GetTime(starttime1);

m_starttime1.SetTime(&starttime1);


C  中的Time


#include <time.h>
#include <stdio.h>

int main( void )
{
   struct tm  when;
   __time64_t now, result;
   int        days;
   char       buff[80];

   time( &now );
   _localtime64_s( &when, &now );
   asctime_s( buff, sizeof(buff), &when );
   printf( "Current time is %s\n", buff );
   days = 20;
   when.tm_mday = when.tm_mday + days;
   if( (result = mktime( &when )) != (time_t)-1 ) {
      asctime_s( buff, sizeof(buff), &when );
      printf( "In %d days the time will be %s\n", days, buff );
   } else
      perror( "mktime failed" );
}


out put :
Current time is Fri Apr 25 13:34:07 2003

In 20 days the time will be Thu May 15 13:34:07 2003

  

    long long lt = (long)time(nullptr);
    CTime ct(lt);


    tm llt;
    ct.GetGmtTm(&llt);
    time_t to_time_t = mktime(&llt);
    long long llt2 = (long)time(&to_time_t);
    CTime ct2(llt2);

    //csMsg = ct2.Format(_T("%y-%m-%d %H:%M:%S"));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值