资源简介
2019月4月30日 MFC 控制台 获取网络时间,两个服务器,两种方法,编译可用,亲测有效

代码片段和文件信息
// GetNetTime.cpp : 定义控制台应用程序的入口点。
//
#include “stdafx.h“
#include “GetNetTime.h“
#include
#include
#include
#pragma comment(lib “Wininet.lib“)
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 唯一的应用程序对象
CWinApp theApp;
using namespace std;
int IsLeap(unsigned short year)
{
return ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0));
}
CString MillSecond2LocalTime(long long time long timezone)
{
const int monthLengths[2][13] = {
{ 0 31 59 90 120 151 181 212 243 273 304 334 365 }
{ 0 31 60 91 121 152 182 213 244 274 305 335 366 }
};
const int yearLengths[2] = { 365 366 };
int year(0) month(0) minMonth(0) maxMonth(0) days(0) clock(0) isLeap(0) day(0) hour(0) minute(0) second(0);
time /= 1000;
time += timezone * 60 * 60;
days = time / 86400;//天数
clock = time % 86400;//小时数
if (clock < 0)
{
clock += 86400;
days -= 1;
}
if (days >= 0)
{
year = days / 366;
days -= year * 365 + (year + 1) / 4 - (year + 69) / 100 + (year + 369) / 400;
for (year = year + 1970;; year++)
{
isLeap = IsLeap(year);
if (days < yearLengths[isLeap])
{
break;
}
days -= yearLengths[isLeap];
}
}
else
{
year = days / 366;
days -= year * 365 + (year - 2) / 4 - (year - 30) / 100 + (year - 30) / 400;
for (year = year + 1970 - 1;; year--)
{
isLeap = false;
days += yearLengths[isLeap];
if (days >= 0)
{
break;
}
}
}
minMonth = 0;
maxMonth = 12;
for (month = 5; month<12 && month>0; month = (minMonth + maxMonth) / 2)
{
if (days < monthLengths[isLeap][month])
{
maxMonth = month;
}
else if (days >= monthLengths[isLeap][month + 1])
{
minMonth = month;
}
else
{
break;
}
}
days -= monthLengths[isLeap][month];
month++;
day = days + 1;
hour = clock / 3600;
clock = clock % 3600;
minute = clock / 60;
second = clock % 60;
//printf(“%d-%02d-%02d %02d:%02d:%02d“ year month day hour minute second);
CString tm;
tm.Format(“%d-%02d-%02d %02d:%02d:%02d“ year month day hour minute second);
return tm;
}
//淘宝服务器
CString GetTBTime(CHAR* url)
{
HINTERNET hSession = InternetOpen(_T(“UrlTest“) INTERNET_OPEN_TYPE_PRECONFIG NULL NULL 0);
if (hSession != NULL)
{
HINTERNET hHttp = InternetOpenUrl(hSession url NULL 0 INTERNET_FLAG_DONT_CACHE 0);
if (hHttp != NULL)
{
char buffer[1024];
DWORD bytes_read;
BOOL b;
do
{
b = InternetReadFile(hHttp buffer 1024 - 1 &bytes_read);
} while (bytes_read != 0);
//完整字符串
CString tm;
tm.Format(“%s“ buffer);
//查找截取
int n = tm.ReverseFind(‘}‘);
CString tm2 = tm.Mid(n - 15 13);
//时间戳转换
USES_CONVERSION;
char *str = T2A(tm2.GetBuffer(0));
long long time;
sscanf(str “%I64d“ &time);
CString Ctm = MillSecond2LocalTime(time 8);
return Ctm;
InternetCloseHandle(hHttp);
hHttp = NULL;
}
InternetCloseHandle(hSession);
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2019-04-30 13:07 GetNetTime\
文件 4764 2019-04-30 13:07 GetNetTime\GetNetTime.cpp
文件 39 2019-04-30 09:26 GetNetTime\GetNetTime.h
文件 2642 2019-04-30 09:26 GetNetTime\GetNetTime.rc
文件 4795 2019-04-30 11:31 GetNetTime\GetNetTime.vcxproj
文件 1638 2019-04-30 09:26 GetNetTime\GetNetTime.vcxproj.filters
文件 1997 2019-04-30 09:26 GetNetTime\ReadMe.txt
文件 393 2019-04-30 09:26 GetNetTime\Resource.h
文件 216 2019-04-30 09:26 GetNetTime\stdafx.cpp
文件 899 2019-04-30 09:26 GetNetTime\stdafx.h
文件 236 2019-04-30 09:26 GetNetTime\targetver.h
- 上一篇:四位七脚数码管C语言
- 下一篇:c++教师排课程序
相关资源
- QtWebApp
- 基于STM32F407的W5500 tcpserver(官网例程
- 高性能服务器代码(50_06th_server_thre
- C++ sql2008 WebServer通讯.docx
- VC操作SQLSERVER数据库
- 一个简单而强大的基于MFC的web server源
- 基于C++和数据库SQL server开发的商品销
- 基于C++和SQL Server开发的商品销售管理
- 宾馆管理系统(C++MFC)数据库课程设
- c++和SqlServer做的图书管理系统
- Windows下基于ModbusTcp的Server端开发C语言
- DDE server VC++
- 网络编程HttpServer c++实现
- SQL server compact 3.5
- 基于C语言和SQL SERVER数据库实现的图书
- 进销存管理系统MFC SqlServer编程
- 服务端和客户端(MFC CSocket)
- IEC104规约Server及Client实现
- mfc配合listbox操作数据库
- C/C++使用ODBC操作SQL server数据库
- C/C++使用ODBC连接SQL server数据库完整流
- MFC ADO方法链接SQlServer数据库vc6实现
- 数据库课设SQL Server 2000 + Visual C++客户
- 简单易懂C++ WebServer接口开发源代码
- 最好用的OPC Server开发包OPCServer开发工
- MFC与SQL Server
- 数据库课程设计(教室管理信息系统
- FTP服务器MFC源码
- vs+mssql实现医药管理信息系统实验报告
- MFC websocket server | MFC websocket服务器
评论
共有 条评论