1:在对话框拖一个edit控件,用类向导设置为CString型变量s
2:用类向导为XXXXDlg类创建三个Message函数,如下:
int CDuihuaDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
SetTimer(1,1000,NULL);
return 0;
}
void CDuihuaDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
KillTimer(1);
CDialog::OnClose();
}
void CDuihuaDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CTime time;
time=CTime::GetCurrentTime();
CString s1=time.Format("%Y-%m-%d\");
CString s2=time.Format("%H:%m:%S");
s=s1+s2;
UpdateData(FALSE);
CDialog::OnTimer(nIDEvent);
}
CDuihuaDlg是我的创建的对话框。
CTime 使用总结
1.初始化 m_begintime=CTime(2004,1,1,0,0,0,-1);//参数依次为year,month,day,hour,minite,second m_endtime =CTime::GetCurrentTime();//当前时间2.日期比较 CTimeSpan span;span=time1-time2;得到两时间的间隔.可以取得span.GetHours().等3.access数据库查询使用DateDiff()函数,具体参照access帮助CString timesql; timesql.Format(" Where DateDiff('d',%s,'%s')<=0日期",m_begintime.Format("%Y-%m-%d"));4读取日期字段(odbc) CDBVariant var; recset.GetFieldValue(i,var); s.Format("%d-%d-%d",(var.m_pdate)->year,(var.m_pdate)->month, (var.m_pdate)->day);
我知道库函数 localtime() 可以把 time_t 转换成结构 struct tm, 而 ctime() 可以把 time_t 转换成为可打印的字符串。怎样才能进行反向操作, 把 struct tm 或一个字符串转换成 time_t?
ANSI C 提供了库函数 mktime(), 它把 struct tm 转换成 time_t。
把一个字符串转换成 time_t 比较难些, 这是由于可能遇到各种各样的日期和时间格式。某些系统提供函数 strptime(), 基本上是 strftime() 的反向函数。其它常用的函数有 partime() (与 RCS 包一起被广泛的发布) 和 getdate() (还有少数其它函数, 发布在 C 的新闻组)。
我们在编程的过程中,可能会求两个时间之间的间隔。比如你在数据库里保存了一条记录保存的时间,过一段时间你想检查那条记录到现在为止是否超期了。
获得当前日期的方法有几种比如用SYSTEMTIME结构,或是用CTime和CTimeSpan类。
要获得当前的日期可以照以下的方法作:
SYSTEMTIME systemtime;
::GetSystemTime(&systime);
SYSTEMTIME结构中,m_wyear就是当前的年份,m_wdate就是当前的日期,m_wmonth就是当前的月份。
还有一种方法就是:
CTime t1=CTime::GetCurrentTime();
t1中保存了当前的年月日时分秒。
CTimeSpan类主要是保存两个时间之间的间隔。例如:
假设另一个CTime类的对象是t2;
CTimeSpan t3=t1-t2;
这样t1和t2之间的间隔就保存到了t3中。 如果想得到天数的间隔:
int days=t3.GetDays();
要得到别的间隔可以参考MSDN中CTimeSpan;
VC中CTime时间
获得当前日期和时间
CTime tm=CTime::GetCurrentTime();
CString str=tm.Format("%Y-%m-%d");
在VC中,我们可以借助CTime时间类,获取系统当前日期,具体使用方法如下:
CTime t = CTime::GetCurrentTime(); //获取系统日期
int d=t.GetDay(); //获得几号
int y=t.GetYear(); //获取年份
int m=t.GetMonth(); //获取当前月份
int h=t.GetHour(); //获取当前为几时
int mm=t.GetMinute(); //获取分钟
int s=t.GetSecond(); //获取秒
int w=t.GetDayOfWeek(); //获取星期几,注意1为星期天,7为星期六
如果想计算两段时间的差值,可以使用CTimeSpan类,具体使用方法如下:
CTime t1( 1999, 3, 19, 22, 15, 0 );
CTime t = CTime::GetCurrentTime();
CTimeSpan span=t-t1; //计算当前系统时间与时间t1的间隔
int iDay=span.GetDays(); //获取这段时间间隔共有多少天
int iHour=span.GetTotalHours(); //获取总共有多少小时
int iMin=span.GetTotalMinutes();//获取总共有多少分钟
int iSec=span.GetTotalSeconds();//获取总共有多少秒
设置计时器
定义TIMER ID
#define TIMERID_JISUANFANGSHI 2
在适当的地方设置时钟,需要开始其作用的地方;
SetTimer(TIMERID_JISUANFANGSHI,200,NULL);
在不需要的时候销毁掉时钟
KillTimer(TIMERID_JISUANFANGSHI);
消息映射
void CJisuan::OnTimer(UINT nIDEvent)
{}
ctime_format() 函数
不知道各位是否已经早已解决这些问题了,也不知道LPMUD OS里是否直接支持 format 日期的方法,今天在改一个游戏时,突然发现03年我写的 ctime_new() 还是有许多不如意之处,所以干脆重新来过,写一个更好用、更清晰的 ctime_format() 。
不要告诉我你还在用着 OS 里自带的英文风格的 ctime() !
// naihe 05-8-16 13:18
// 自从 2003-04-29 使用 ctime_new() 函数以来,一直觉得有些不爽
// 今天改成新的方式,兼容旧的,新的更好!
#include "localtime.h"
#define LT_MON_ADD 1 // 有些OS存在这个BUG,没有把 LT_MON 加一,可以修正一下
#define DEFAULT_FORMAT_STR "YY.M.D(w) h:m:s" // 默认标准表述格式
string help()
{
string msg;
msg = "\\n"
+ "varargs string ctime_format( mixed arg1, mixed arg2 ) 帮助:\\n"
+ "----------------------------------------------------------------------\\n"
+ "函数说明:根据自定义的格式,把一个时间数字所代表的时间信息表述出来。\\n"
+ " (返回 string 信息)\\n"
+ "传入参数:1、(string) 格式规范(sFormat)\\n"
+ " 2、(int) 欲查询的时间(iTime)\\n"
+ "兼容: 自由传入参数,使代码编写更方便。\\n"
+ " 规则:1、省略参数时,得到当前时间的默认格式表述;\\n"
+ " 2、只传入 (int) iTime 时,使用默认格式表述;\\n"
+ " 3、只传入 (string) sFormat 时,使用当前时间表述;\\n"
+ " 4、两个参数都传入时,根据你的设置表述。\\n";
msg += ""
+ "格式规范:["+DEFAULT_FORMAT_STR+"] 是标准的格式,其中:\\n"
+ " YY 将被替换成为年份的数字如:2005\\n"
+ " Y 优先替换 YY 之后,Y 将被替换为短年份格式如:05\\n"
+ " M 将被替换成为月份的数字如:02, 11\\n"
+ " D 将被替换成为日期的数字如:08, 31\\n"
+ " w 将被替换成为星期的汉字如:三,日,六\\n"
+ " h 将被替换成为小时的数字如:01, 24\\n"
+ " m 将被替换成为分钟的数字如:01, 58\\n"
+ " s 将被替换成为秒钟的数字如:08, 59\\n";
msg += ""
+ " 根据以上规律,你可以自定需要的格式,例如:\\n"
+ " ctime_format( \\"Y-M-D h:m:s 星期w\\",11241748 ) 即得出:\\n"
+ " 05-08-16 14:48:09 星期二 这种非规范的格式。\\n"
+ "----------------------------------------------------------------------\\n";
return msg;
}
string i_to_s( int num )
{
if( num == 0 )
return "00";
if( num < 10 )
return sprintf( "0%d", num );
return sprintf( "%d", num );
}
// 这是真正的检测函数。喜欢的话,也可以直接调它;但是还是经过检测的安全些吧。
string ctime_format_real( int iTime, string sFormat )
{
mixed *lt;
string wday;
if( !iTime || !sFormat )
return 0;
lt = localtime( iTime );
if( !lt || !sizeof(lt) )
return 0;
// 获得星期几的中文
wday = chinese_number( lt[ LT_WDAY ] );
if( lt[ LT_WDAY ] == 0 )
wday = "日";
sFormat = replace_string( sFormat, "YY", i_to_s(lt[LT_YEAR]) ); // 注:这句要在 Y 前
sFormat = replace_string( sFormat, "Y", (lt[LT_YEAR]+"")[2..<1] ); // 注:这句要在 YY 后
sFormat = replace_string( sFormat, "M", i_to_s(lt[LT_MON]+LT_MON_ADD) );
sFormat = replace_string( sFormat, "D", i_to_s(lt[LT_MDAY]) );
sFormat = replace_string( sFormat, "w", wday );
sFormat = replace_string( sFormat, "h", i_to_s(lt[LT_HOUR]) );
sFormat = replace_string( sFormat, "m", i_to_s(lt[LT_MIN]) );
sFormat = replace_string( sFormat, "s", i_to_s(lt[LT_SEC]) );
return sFormat;
}
varargs string ctime_format( mixed arg1, mixed arg2 )
{
string sFormat;
int iTime;
// 忽略2个参数时,即以默认格式获得当前时间的表述
if( !arg1 && !arg2 )
{
return ctime_format_real( efun::time(), DEFAULT_FORMAT_STR );
}
// 忽略第二个参数时
if( arg1 && !arg2 )
{
if( stringp(arg1) )
{
sFormat = arg1;
iTime = time();
}
else if( intp(arg1) && arg1 > 0 )
{
iTime = arg1;
sFormat = DEFAULT_FORMAT_STR;
}
else
return 0;
}
// 忽略第一个参数时(例如传入 0, 1)
if( !arg1 && arg2 )
{
if( stringp(arg2) )
{
sFormat = arg2;
iTime = time();
}
else if( intp(arg2) && arg2 > 0 )
{
iTime = arg2;
sFormat = DEFAULT_FORMAT_STR;
}
else
return 0;
}
// 两个参数都有时
if( arg1 && arg2 )
{
if( intp(arg1) && arg1 > 0 && stringp(arg2) )
{
iTime = arg1;
sFormat = arg2;
}
else if( stringp(arg1) && intp(arg2) && arg2 > 0 )
{
iTime = arg2;
sFormat = arg1;
}
else
return 0;
}
return ctime_format_real( iTime, sFormat );
}