怎么获取系统时间?
大家好,我想获取系统时间,格式为:年-月-日-时-分-秒-毫秒 问题点数:20、回复次数:23Top
1 楼sycnick(李小虾)回复于 2006-04-29 11:33:57 得分 1
COleDateTime::GetCurrentTime
CTime::GetCurrentTimeTop
2 楼sunmz_wjxy(孙大圣)回复于 2006-04-29 11:37:42 得分 2
MSDN的代码:
#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <string.h>
void main()
{
char tmpbuf[128], ampm[] = "AM";
time_t ltime;
struct _timeb tstruct;
struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 };
/* Set time zone from TZ environment variable. If TZ is not set,
* the operating system is queried to obtain the default value
* for the variable.
*/
_tzset();
/* Display operating system-style date and time. */
_strtime( tmpbuf );
printf( "OS time:\t\t\t\t%s\n", tmpbuf );
_strdate( tmpbuf );
printf( "OS date:\t\t\t\t%s\n", tmpbuf );
/* Get UNIX-style time and display as number and string. */
time( <ime );
printf( "Time in seconds since UTC 1/1/70:\t%ld\n", ltime );
printf( "UNIX time and date:\t\t\t%s", ctime( <ime ) );
/* Display UTC. */
gmt = gmtime( <ime );
printf( "Coordinated universal time:\t\t%s", asctime( gmt ) );
/* Convert to time structure and adjust for PM if necessary. */
today = localtime( <ime );
if( today->tm_hour > 12 )
{
strcpy( ampm, "PM" );
today->tm_hour -= 12;
}
if( today->tm_hour == 0 ) /* Adjust if midnight hour. */
today->tm_hour = 12;
/* Note how pointer addition is used to skip the first 11
* characters and printf is used to trim off terminating
* characters.
*/
printf( "12-hour time:\t\t\t\t%.8s %s\n",
asctime( today ) + 11, ampm );
/* Print additional time information. */
_ftime( &tstruct );
printf( "Plus milliseconds:\t\t\t%u\n", tstruct.millitm );
printf( "Zone difference in seconds from UTC:\t%u\n",
tstruct.timezone );
printf( "Time zone name:\t\t\t\t%s\n", _tzname[0] );
printf( "Daylight savings:\t\t\t%s\n",
tstruct.dstflag ? "YES" : "NO" );
/* Make time for noon on Christmas, 1993. */
if( mktime( &xmas ) != (time_t)-1 )
printf( "Christmas\t\t\t\t%s\n", asctime( &xmas ) );
/* Use time structure to build a customized time string. */
today = localtime( <ime );
/* Use strftime to build a customized time string. */
strftime( tmpbuf, 128,
"Today is %A, day %d of %B in the year %Y.\n", today );
printf( tmpbuf );
}
Output
OS time: 21:51:03
OS date: 05/03/94
Time in seconds since UTC 1/1/70: 768027063
UNIX time and date: Tue May 03 21:51:03 1994
Coordinated universal time: Wed May 04 04:51:03 1994
12-hour time: 09:51:03 PM
Plus milliseconds: 279
Zone difference in seconds from UTC: 480
Time zone name:
Daylight savings: YES
Christmas Sat Dec 25 12:00:00 1993
Today is Tuesday, day 03 of May in the year 1994
Top
3 楼Qiushen(忘魂)回复于 2006-04-29 11:53:32 得分 1
CString strTime = CTime::GetCurrentTime().Format("%Y-%m-%d %H:%M:%S");Top
4 楼Pipi0714(老顽童)回复于 2006-04-29 11:58:57 得分 1
GetLocalTimeTop
5 楼fwall(防火墙)回复于 2006-05-08 11:32:42 得分 0
谢谢各位,我刚刚接触VC,很多东西都的开始学,请大家多提供点帮助,我的分很多的!Top
6 楼fwall(防火墙)回复于 2006-05-15 17:00:34 得分 0
pDbcom->GetRealData(10,(LPCTSTR)strdbM,transdata);
dbsql.Format("INSERT INTO Exp(ID, Np, Pft, Pfpi, Pfpo, EPfp, Tft, Tfpi, Qfao, Te, He, Fl) VALUES(%ld,%6.1f,%5.3f,%5.3f,%5.3f,%5.3f,%4.1f,%4.1f,%7.3f,%4.1f,%4.1f,%4.1f)",dbcount,transdata[0],transdata[1],transdata[2],transdata[3],(transdata[3]-transdata[2]),transdata[4],transdata[5],transdata[6],transdata[7],transdata[8],transdata[9]);
我想在F1变量后面增加系统的时间,格式位:时-分-秒-毫秒
各位大狭,怎么修改上边的代码?
Top
7 楼lfchen(一条晚起的虫--床上用品[家纺]专卖)回复于 2006-05-15 17:03:28 得分 2
数据库中的时间字段是什么格式?是什么数据库?
Top
8 楼fwall(防火墙)回复于 2006-05-15 17:06:16 得分 0
我在MSDN里找的,可我才开始看VC++,不知道怎么使用,请大家帮忙,谢谢!
typedef struct _SYSTEMTIME { // st
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME;
Top
9 楼lfchen(一条晚起的虫--床上用品[家纺]专卖)回复于 2006-05-15 17:09:17 得分 1
这个是个结构
SYSTEMTIME time;
GetLocalTime(&time); //获取当前系统时间。
time.wYear; //年
...
time.wMilliseconds; //msTop
10 楼rubor5168(Alan)回复于 2006-05-15 17:12:16 得分 1
多看MSDN吧.上面也有实例的Top
11 楼fwall(防火墙)回复于 2006-05-16 13:35:35 得分 0
VC真的好难啊!
那为帮我作个例子,创建一个数据库表格,里面有系统时间字段
如表A,字段为:ID,数据1,数据2,年:月:日:时:分:秒:毫秒
采用VC语言的SQL语句创建表格,如:
dbsql.Format("INSERT INTO Exp(ID, Np, Pft, Pfpi)VALUES(%ld,%6.1f,%5.3f,%5.3f)",dbcount,transdata[0],transdata[1],transdata[2],transdata[3],transdata[4]);
Top
12 楼fwall(防火墙)回复于 2006-05-18 10:54:02 得分 0
问题看来还的自己解决!
你们说的都不适合,我已经找到了,分享给大家吧!
#include <stdio.h>
#include <sys/timeb.h>
#include <time.h>
void main( void )
{
struct _timeb timebuffer;
char *timeline;
_ftime( &timebuffer );
timeline = ctime( & ( timebuffer.time ) );
printf( "The time is %.19s.%hu %s", timeline, timebuffer.millitm, &timeline[20] );
}
Output
The time is Tue Mar 21 15:26:41.341 1995
Top
13 楼huanazhang()回复于 2006-05-18 11:14:15 得分 1
CString sTime,sYear,sMonth,sDay,sHour,sMinute,sSecond,sMilliseconds;
SYSTEMTIME CurTime;
GetLocalTime(&CurTime);
sYear.Format("%d",CurTime.wYear);
sMonth.Format("%d",CurTime.wMonth);
sDay.Format("%d",CurTime.wDay);
sHour.Format("%d",CurTime.wHour);
sMinute.Format("%d",CurTime.wMinute);
sSecond.Format("%d",CurTime.wSecond);
sMilliseconds.Format("%d",CurTime.wMilliseconds);
sTime = sYear+ sMonth + sDay + sHour + sMinute + sSecond + sMilliseconds;
return sTime;Top
14 楼fwall(防火墙)回复于 2006-05-19 17:05:04 得分 0
msdn中说使用SYSTEMTIME 结构体,得不到sMilliseconds,不过我还是感谢大家参与,我要结帐了。
另外谁能帮我解决我这个问题,我另外开个帖子,分都给他!
VC中创建数据库插入数据:
dbsql.Format("INSERT INTO Exp(ID, Np, Pft, Pfpi)VALUES(%ld,%6.1f,%5.3f,%5.3f)",dbcount,transdata[0],transdata[1],transdata[2],transdata[3],transdata[4]);
我要在表的后面插入H:M:S.MS 就是15:26:41.341 (小时:分:秒.毫秒)怎么插入,主要是VALUES()中的参数怎么添加,谢谢!Top
15 楼lfchen(一条晚起的虫--床上用品[家纺]专卖)回复于 2006-05-19 17:08:34 得分 0
dbsql.Format("INSERT INTO Exp(ID, Np, Pft, Pfpi,mytime)VALUES(%ld,%6.1f,%5.3f,%5.3f,'%s')",dbcount,transdata[0],transdata[1],transdata[2],transdata[3],transdata[4],"15:26:41.341");Top
16 楼fwall(防火墙)回复于 2006-05-19 17:11:44 得分 0
http://community.csdn.net/Expert/topic/4764/4764683.xml?temp=.3682215
新开的帖子,请踊跃参加!Top
17 楼lid0770(卡卡)回复于 2006-05-19 17:14:20 得分 1
BOOL CXXX::OnInitDialog()
{
...
SetTimer(1,1000,0); //设定一秒更新一次
}
void CXXX::OnTimer(UINT_PTR nIDEvent)
{
if (nIDEvent = 1)
{
CTime tm;
CString strTime;
tm = CTime::GetCurrentTime();
strTime = tm.Format("%H:%M:%S");
// 在EDIT控件上显示出来
((CEdit*)GetDlgItem(IDC_EDIT1))->SetWindowText(strTime);
}
}
Top
18 楼kocs2002(拒绝签名)回复于 2006-05-19 17:16:43 得分 1
CTime t=CTime::GetCurrentTime();Top
19 楼breakind(冰舞,把练街舞的精神拿来编程,必有所成.)回复于 2006-05-19 21:17:59 得分 1
GetLocalTimeTop
20 楼happyna()回复于 2006-05-20 09:40:35 得分 1
//获取系统时间
CString m_time;
CTime t = CTime::GetCurrentTime();
CString strTime = t.Format("%Y-%m-%d %H:%M:%S");
m_time.Empty();
m_time+=strTime;Top
21 楼boyplayee()回复于 2006-05-20 11:06:13 得分 1
CTime tm;
CString strTime;
tm = CTime::GetCurrentTime();
strTime = tm.Format("%H:%M:%S");
up this easy way.Top
22 楼fwall(防火墙)回复于 2006-09-07 12:43:45 得分 0
谢谢大家,最近太忙忘记结帐了,马上结!Top
23 楼kite079(风筝)回复于 2006-09-07 12:51:42 得分 5
我有例子,发到邮箱了Top




