通用功能模块(时间获取,时间转换,文件控制,本地主机信息等)

#pragma once
#include <Windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <CString>
#include <time.h>
#include <boost/filesystem.hpp>

#include <iphlpapi.h>  
#pragma comment(lib, "IPHLPAPI.lib")  

u_char g_ucLocalMac[6];  
DWORD g_dwGatewayIP;  
DWORD g_dwLocalIP;  
DWORD g_dwMask;  

class DHAboutComFunction
{
public:

	class DHTimeContorl
	{
	public:
		//************************************
		// Method:    GetNowTime
		// FullName:  DHAboutComFunction::GetNowTime
		// Access:    public static 
		// Returns:   CString
		// Qualifier:获取当前时间
		//************************************
		static CString GetNowTime()
		{
			CString cstrTime;
			time_t local_time;
			time(&local_time);
			auto time_now = *localtime(&local_time);
			cstrTime.Format(_T("%d-%02d-%02d %02d:%02d:%02d"), time_now.tm_year + 1900, time_now.tm_mon + 1, time_now.tm_mday,
				time_now.tm_hour, time_now.tm_min, time_now.tm_sec);
			return cstrTime;
		}

		//************************************
		// Method:    GetNowHour
		// FullName:  DHAboutComFunction::GetNowHour
		// Access:    public static 
		// Returns:   CString
		// Qualifier:获取当前小时
		//************************************
		static CString GetNowHour()
		{
			CString cstrTime;
			time_t local_time;
			time(&local_time);
			auto time_now = *localtime(&local_time);
			cstrTime.Format(_T("%02d"),time_now.tm_hour);
			return cstrTime;
		}

		//************************************
		// Method:    GetNowMinute
		// FullName:  DHAboutComFunction::GetNowMinute
		// Access:    public static 
		// Returns:   CString
		// Qualifier:获取当前分钟数
		//************************************
		static CString GetNowMinute()
		{
			CString cstrTime;
			time_t local_time;
			time(&local_time);
			auto time_now = *localtime(&local_time);
			cstrTime.Format(_T("%02d"),time_now.tm_min);
			return cstrTime;
		}

		//************************************
		// Method:    GetNowSec
		// FullName:  DHAboutComFunction::GetNowSec
		// Access:    public static 
		// Returns:   CString
		// Qualifier:获取当前秒数
		//************************************
		static CString GetNowSec()
		{
			CString cstrTime;
			time_t local_time;
			time(&local_time);
			auto time_now = *localtime(&local_time);
			cstrTime.Format(_T("%02d"),time_now.tm_sec);
			return cstrTime;
		}

		//************************************
		// Method:    GetNowDate
		// FullName:  DHAboutComFunction::GetNowDate
		// Access:    public static 
		// Returns:   CString
		// Qualifier:获取当前日期
		//************************************
		static CString GetNowDate()
		{
			CString cstrTime;
			time_t local_time;
			time(&local_time);
			auto time_now = *localtime(&local_time);
			cstrTime.Format(_T("%d-%02d-%02d"), time_now.tm_year + 1900, time_now.tm_mon + 1, time_now.tm_mday);
			return cstrTime;
		}

		//************************************
		// Method:    GetWeekDay
		// FullName:  DHAboutComFunction::GetWeekDay
		// Access:    public static 
		// Returns:   CString
		// Qualifier:获取今天的星期
		//************************************
		static CString GetWeekDay()
		{
			CString cstrTime;
			time_t local_time;
			time(&local_time);
			auto time_now = *localtime(&local_time);
			if (time_now.tm_wday == 0)
			{
				cstrTime.Format(_T("7"));
			}
			else
			{
				cstrTime.Format(_T("%d"),time_now.tm_wday);
			}
			return cstrTime;
		}
	};


	//文件控制相关(需要配置boost库)
	class DHFileContorl
	{
	public:
		static BOOL BoostIsFileExist(LPCSTR strFilePath)
		{
			boost::filesystem::path ph(strFilePath);
			if (boost::filesystem::exists(ph))
			{
				return TRUE;
			}
			else
			{
				return FALSE;
			}
		}

		static BOOL BoostCreateDir(LPCSTR strFilePath)
		{
			boost::filesystem::path ph(strFilePath);
			if (boost::filesystem::exists(ph))
			{
				return TRUE;
			}
			else
			{
				if (boost::filesystem::create_directory(ph))
				{
					return TRUE;
				}
				else
				{
					return FALSE;
				}
			}
		}

		static BOOL BoostIsDir(LPCSTR strFilePath)
		{
			boost::filesystem::path ph(strFilePath);
			if (boost::filesystem::is_directory(ph))
			{
				return TRUE;
			}
			else
			{
				return FALSE;
			}
		}

		static BOOL BoostDeleteFile(LPCSTR strFilePath)
		{
			boost::filesystem::path ph(strFilePath);
			if (boost::filesystem::remove(ph))
			{
				return TRUE;
			}
			else
			{
				return FALSE;
			}
		}
	};

	class DHCover
	{
	public:
		static int StringToTime(const std::string &strDateStr,time_t &timeData)
		{
			char *pBeginPos = (char*) strDateStr.c_str();
			char *pPos = strstr(pBeginPos,"-");
			if(pPos == NULL)
			{
				return -1;
			}
			int iYear = atoi(pBeginPos);
			int iMonth = atoi(pPos + 1);

			pPos = strstr(pPos + 1,"-");
			if(pPos == NULL)
			{
				return -1;
			}

			int iDay = atoi(pPos + 1);
			int iHour = 0,iMin = 0, iSsec = 0;
			pPos = strstr(pBeginPos,":");
			if (pPos == NULL)
			{

			}
			iHour = atoi(pPos - 2);
			iMin  = atoi(pPos + 1);
			iSsec = atoi(pPos +4);

			struct tm sourcedate;
			//bzero((void*)&sourcedate,sizeof(sourcedate));
			memset(&sourcedate,0,sizeof(sourcedate));
			sourcedate.tm_mday = iDay;
			sourcedate.tm_mon = iMonth - 1; 
			sourcedate.tm_year = iYear - 1900;
			sourcedate.tm_hour = iHour;
			sourcedate.tm_min = iMin;
			sourcedate.tm_sec = iSsec;
			timeData = mktime(&sourcedate);  

			return 0;
		}

		static int TimeToString( std::string &strDateStr,time_t &timeData)
		{
			char chTmp[15];
			memset(chTmp,0,sizeof(chTmp)); 
			struct tm *p = new tm;
			localtime_s(p,&timeData);

			p->tm_year = p->tm_year + 1900;

			p->tm_mon = p->tm_mon + 1;

			char *Temp = new char[50];
			memset(Temp,0,49);
			int zz=sizeof(Temp);
			char strHour[3]={0};
			char strMin[3]={0};
			char strSec[3]={0};
			if (p->tm_hour < 10 || p->tm_min <10 || p->tm_sec < 10)
			{
				sprintf_s(Temp,50,"%04d-%d-%d ",p->tm_year, p->tm_mon, p->tm_mday);
				if (p->tm_hour < 10)	
				{
					strHour[0] = '0';
					strHour[1] = p->tm_hour + 48;
				}
				else
				{
					_itoa_s(p->tm_hour,strHour,10);
				}
				if (p->tm_min < 10)	
				{
					strMin[0] = '0';
					strMin[1] = p->tm_min + 48;
				}
				else
				{
					_itoa_s(p->tm_min,strMin,10);
				}
				if (p->tm_sec < 10)	
				{
					strSec[0] = '0';
					strSec[1] = p->tm_sec + 48;
				}
				else
				{
					_itoa_s(p->tm_sec,strSec,10);
				}
				strcat(Temp,strHour);strcat(Temp,":");strcat(Temp,strMin);strcat(Temp,":");strcat(Temp,strSec);
			}
			else
			{
				sprintf_s(Temp,50,"%04d-%d-%d %02d:%02d:%02d",p->tm_year, p->tm_mon, p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec);
			}

			strDateStr.assign(Temp);
			delete []Temp;
			delete p;
			return 0;
		} 
	};

	class DHGetHostInfo
	{
	public:

		//************************************
		// Method:    GetHostName
		// FullName:  DHAboutComFunction::DHGetHostInfo::GetHostName
		// Access:    public static 
		// Returns:   CString
		// Qualifier:获取本机名字
		//************************************
		static CString GetHostName()
		{
			char    HostName[100];
			gethostname(HostName, sizeof(HostName));// 获得本机主机名.
			return HostName;
		}


		//************************************
		// Method:    GetHostIp
		// FullName:  DHAboutComFunction::DHGetHostInfo::GetHostIp
		// Access:    public static 
		// Returns:   CString
		// Qualifier:获取本机ip
		//************************************
		static CString GetHostIp()
		{
			CString strIPAddr;
			char    HostName[100];
			gethostname(HostName, sizeof(HostName));// 获得本机主机名.

			hostent* hn;
			hn = gethostbyname(HostName);//根据本机主机名得到本机ip

			strIPAddr=inet_ntoa(*(struct in_addr *)hn->h_addr_list[0]);//把ip换成字符串形式
			return strIPAddr;
		}


		//************************************
		// Method:    GetHostMac
		// FullName:  DHAboutComFunction::DHGetHostInfo::GetHostMac
		// Access:    public static 
		// Returns:   CString
		// Qualifier: 获取本机mac地址
		//************************************
		static CString GetHostMac()  
		{  
			PIP_ADAPTER_INFO pAdapterInfo = NULL;  
			ULONG ulSize = 0;  
			//为结构申请内存  
			::GetAdaptersInfo(pAdapterInfo, &ulSize);  
			pAdapterInfo = (PIP_ADAPTER_INFO)::GlobalAlloc(GPTR, ulSize);  
			if ( ERROR_SUCCESS  == ::GetAdaptersInfo(pAdapterInfo, &ulSize))  
			{  
				if (pAdapterInfo != NULL)  
				{  
					memcpy(g_ucLocalMac, pAdapterInfo->Address, 6);  
					g_dwGatewayIP = ::inet_addr(pAdapterInfo->GatewayList.IpAddress.String);  
					g_dwLocalIP = ::inet_addr(pAdapterInfo->IpAddressList.IpAddress.String);  
					g_dwMask = ::inet_addr(pAdapterInfo->IpAddressList.IpMask.String);  
				}  
			}  
			//in_addr表示IP的结构  

			CString strMac;
			u_char* p = g_ucLocalMac;  

			strMac.Format("%02X-%02X-%02X-%02X-%02X-%02X", p[0], p[1], p[2], p[3], p[4], p[5]);  
			::GlobalFree(pAdapterInfo);  
			pAdapterInfo = NULL;  
			return strMac;  
		}
	};
};

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值