// DateUtility.cpp: CDateUtility クラスのインプリメンテーション
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "DateUtility.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// 構築/消滅
//////////////////////////////////////////////////////////////////////

CDateUtility::CDateUtility()
{

}

CDateUtility::~CDateUtility()
{

}

//////////////////////////////////////////////////////////////////////
// GMT取得
//////////////////////////////////////////////////////////////////////
CString CDateUtility::GetGMTString(CTime time)
{
	return(time.FormatGmt("%a, %d %b %Y %H:%M:%S GMT"));
}

//////////////////////////////////////////////////////////////////////
// 文字列から変換
//////////////////////////////////////////////////////////////////////
CTime CDateUtility::StringToCTime(CString time)
{
	time.TrimLeft();
	time.TrimRight();
	time.MakeLower();

	int	index = time.Find(",");
	int	day,year,month,hour,minute,second;

	if(index == -1)
	{
		// Sun Nov  6 08:49:37 1994       ; ANSI C の asctime() フォーマット


		//	DayOfWeek
		time = time.Mid(4);

		//	Month
		month = StrToMonth(time.Left(3));
		time = time.Mid(4);

		//	Day
		day = atol(time.Left(2));
		time = time.Mid(3);

		//	Hour
		hour = atol(time.Left(2));
		time = time.Mid(3);

		//	Minute
		minute = atol(time.Left(2));
		time = time.Mid(3);
		
		//	Second
		second = atol(time.Left(2));
		time = time.Mid(3);
	
		//	Year
		year = atol(time.Left(4));

		//	Localへ
		TIME_ZONE_INFORMATION	zoneInfo;
		GetTimeZoneInformation(&zoneInfo);
		CTime ret = CTime(year,month,day,hour,minute,second);
		ret -= CTimeSpan(zoneInfo.Bias * 60);

		return(ret);
	}
	else if(index > 3)
	{
		// Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, RFC 1036 により時代遅れ

		//	DayOfWeek
		time = time.Mid(index + 2);

		//	Day
		day = atol(time.Left(2));
		time = time.Mid(3);

		//	Month
		month = StrToMonth(time.Left(3));
		time = time.Mid(4);

		//	Year
		year = atol(time.Left(2)) + 2000;
		time = time.Mid(3);

		//	Hour
		hour = atol(time.Left(2));
		time = time.Mid(3);

		//	Minute
		minute = atol(time.Left(2));
		time = time.Mid(3);
		
		//	Second
		second = atol(time.Left(2));
		time = time.Mid(3);

		//	Localへ
		TIME_ZONE_INFORMATION	zoneInfo;
		GetTimeZoneInformation(&zoneInfo);
		CTime ret = CTime(year,month,day,hour,minute,second);
		ret -= CTimeSpan(zoneInfo.Bias * 60);

		return(ret);
	}
	else if(index == 3)
	{
		// Sun, 06 Nov 1994 08:49:37 GMT  ; RFC 822, RFC 1123 で改定された

		//	DayOfWeek
		time = time.Mid(index + 2);

		//	Day
		day = atol(time.Left(2));
		time = time.Mid(3);

		//	Month
		month = StrToMonth(time.Left(3));
		time = time.Mid(4);

		//	Year
		year = atol(time.Left(4));
		time = time.Mid(5);

		//	Hour
		hour = atol(time.Left(2));
		time = time.Mid(3);

		//	Minute
		minute = atol(time.Left(2));
		time = time.Mid(3);
		
		//	Second
		second = atol(time.Left(2));
		time = time.Mid(3);

		//	安全のため
		if(year == 1970)
			return(CTime(1980,1,1,1,1,1));

		//	Localへ
		TIME_ZONE_INFORMATION	zoneInfo;
		GetTimeZoneInformation(&zoneInfo);
		CTime ret = CTime(year,month,day,hour,minute,second);
		ret -= CTimeSpan(zoneInfo.Bias * 60);

		return(ret);
	}
	return(0);
}


//	月の取得
int CDateUtility::StrToMonth(CString month)
{
	static char emonth[12][10]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
	int		i;

	for(i=0;i<12;i++)
		if(month.CompareNoCase(emonth[i]) == 0)
			return(i+1);
	return(1);
}
