⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timeex.cpp

📁 一个完整的桌面日历程序
💻 CPP
字号:
/// ------------------------------------------------------------------------------------------------------------------------------------------------------
// -																																					-
// - File:			timeex.cpp.																															-
// -																																					-
// - Contents: 		Implementation of CTimeEx.																											-
// -																																					-
// - Purpose:  		-																																	-
// -																																					-
// - Remarks:    	-																																	-
// -																																					-
// - Originator: 	Michael Mogensen, MM-IT Consult 2003.																								-
// -																																					-
// - Compiler:		MS Visual C++ ver6.0.																											    -
// -																																					-
// - Period:		00.00.00 - 00.00.00.																											    -
// -																																					-
// - Version:		1.00. 																																-
// -																																					-
// ------------------------------------------------------------------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------------------------------------------------------------------
// - Miscellaneous.																																		-
// ------------------------------------------------------------------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------------------------------------------------------------------
// - Header(s).																																			-
// ------------------------------------------------------------------------------------------------------------------------------------------------------
#pragma once
#include "stdafx.h"
#include "timeex.hpp" // Self.

// ------------------------------------------------------------------------------------------------------------------------------------------------------
// - Miscellaneous.																																		-
// ------------------------------------------------------------------------------------------------------------------------------------------------------
#define new DEBUG_NEW

// ------------------------------------------------------------------------------------------------------------------------------------------------------
// - CTimeEx.																																			-
// ------------------------------------------------------------------------------------------------------------------------------------------------------

// 4. Slaves. (alphabetical).

// ******************************
// * Second related.			*
// ******************************
// ******************************
// * Minute related.			*
// ******************************
// ******************************
// * Hour related.				*
// ******************************
// ******************************
// * Day related.				*
// ******************************
const CString CTimeEx::GetDay_Desc(const bool bShortFrom)
// Return desc. of day in week.
{ return GetDay_Desc(GetDayOfWeek(), bShortFrom); }

const CString CTimeEx::GetDay_Desc(const int iDay, const bool bShortFrom)
// Return desc. of day in week.
{
	const CString cstrDaysOfWeek[7][2] =
	{
		_T("sunday"),		_T("sun"), 
		_T("monday"),		_T("mon"), 
		_T("tuesday"),		_T("tue"), 
		_T("wednesday"),	_T("wen"), 
		_T("thursday"),		_T("thu"), 
		_T("friday"),		_T("fri"), 
		_T("saturday"),		_T("sat"),
	};
	return cstrDaysOfWeek[iDay - 1][(bShortFrom ? 1 : 0)];
}

const CTimeEx CTimeEx::GetDay_FirstOfMonth()
// Return time obj. of first of this month.
{ return CTimeEx(GetYear(), GetMonth(), 1); }

const int CTimeEx::GetDay_FirstOfWeek() // Is static.
// Return the day that begins a week in this country (in denmark this is always a monday).
{
	// Using local settings found in control panel.
	TCHAR szLocalInfo[2];
	BOOL bResult = ::GetLocaleInfo(
		LOCALE_SYSTEM_DEFAULT, 
		LOCALE_IFIRSTDAYOFWEEK, 
		szLocalInfo, 
		2);
	int iFirstOfWeek = _ttoi(szLocalInfo);
	SetCyclic(iFirstOfWeek, 2, 1, 7); // This is done to match the day-numbering in the base class.
	return iFirstOfWeek;
}

const CTimeEx CTimeEx::GetDay_FirstOfYear()
// Return time obj. of first of this year.
{ return GetDay_FirstOfYear(GetYear()); }

const CTimeEx CTimeEx::GetDay_FirstOfYear(const int iYear)
// Return time obj. of first of this year.
{ return CTimeEx(iYear, 1, 1); }

const CTimeEx CTimeEx::GetDay_LastOfMonth()
// Return time obj. of last of this month.
{ return CTimeEx(GetYear(), GetMonth(), GetMonth_Length(GetMonth())); }

const CTimeEx CTimeEx::GetDay_LastOfMonth(const int iMonth)
// Return time obj. of last of this month.
{ return CTimeEx(GetYear(), GetMonth(), iMonth); }

const CTimeEx CTimeEx::GetDay_LastOfYear()
// Return time obj. of last of this year.
{ return GetDay_LastOfYear(GetYear()); }

const CTimeEx CTimeEx::GetDay_LastOfYear(const int iYear)
// Return time obj. of last of this year.
{ return CTimeEx(iYear, 12, GetMonth_Length(12)); }

// ******************************
// * Week related.				*
// ******************************
const int CTimeEx::GetWeek_No()
// Return week number of this week.
{
	int iWeekNo = 0;
	CTimeEx t;
	const CTimeSpanEx ts1Day(1);
	const int iDayFOW = CTimeEx::GetDay_FirstOfWeek();

	// HOT FIX.
	if(GetDayOfWeek() == iDayFOW)
		*this += ts1Day; // A help to CTimeEx::GetWeek_No(...).
	// HOT FIX.

	// Get number of days in first week of this year (first week containing at least four days is the first week of that year).
	int iDaysFW = 0;
	for(t = GetDay_FirstOfYear(); t.GetDayOfWeek() != iDayFOW; t += ts1Day)
		iDaysFW++;
	//TRACE("Days in first week of year %d is %d\n", GetYear(), iDaysFW);
	// Get number of week to begin year (it is 52, 53 or 1).
	if(iDaysFW < 4)
		iWeekNo = GetWeek_No_LastOfYear(GetYear() - 1);
	else
		iWeekNo = 1;
	//TRACE("First week of year %d is %d\n", GetYear(), iWeekNo);
	// If current date are before first day in week we are finished now.
	if(*this < t)
		return iWeekNo;
	// If not we set a new base and finds the offset to t in days.
	const CTimeSpanEx tsDelta = *this - t;
	iWeekNo = ((iDaysFW < 4) ? 1 : 2) + (tsDelta.GetDays() / 7);
	return iWeekNo;
}

const int CTimeEx::GetWeek_No_FirstOfYear()
// Return week number of first of this year.
{ return GetWeek_No_FirstOfYear(GetDay_FirstOfYear().GetYear()); }

const int CTimeEx::GetWeek_No_FirstOfYear(const int iYear)
// Return week number of first of this year.
{
	CTimeEx tFirstDayOfYear(iYear);
	const int iWeek_No_FirstOfYear = tFirstDayOfYear.GetWeek_No();
	return iWeek_No_FirstOfYear;
}

const int CTimeEx::GetWeek_No_LastOfYear()
// Return week number of lastst of this year.
{ return GetWeek_No_LastOfYear(GetDay_LastOfYear().GetYear()); }

const int CTimeEx::GetWeek_No_LastOfYear(const int iYear)
// Return week number of lastst of this year.
{
	// We will always experience a week 53 when December 31st falls on a Thursday, or in 
	// the case of a leap year when December 31st falls on either a Thursday or a Friday.
	CTimeEx tLastDayOfYear(GetDay_LastOfYear(iYear));
	int iWeek_No_LastOfYear = 52;
	if((tLastDayOfYear.GetDayOfWeek() == DAY_THURSDAY) || 
	   (tLastDayOfYear.GetDayOfWeek() == DAY_FRIDAY && IsLeapYear()))
		iWeek_No_LastOfYear++;
	return iWeek_No_LastOfYear;
}

const int CTimeEx::GetWeek_No_TotalOfMonth(const int iMonth)
// Return weeks of this month.
{
	// Number of 'first day of week''s if first day in month is a 'first day of week' and 
	// number + 1 of 'first day of week' if not.
	const int iDayFOW = CTimeEx::GetDay_FirstOfWeek();
	CTimeEx t(GetDay_FirstOfMonth()), 
			tLast(GetDay_LastOfMonth());
	int iTotalInMonth = 1;
	const CTimeSpanEx ts1Day(1);
	do
	{
		t += ts1Day;
		if(t.GetDayOfWeek() == iDayFOW)
			iTotalInMonth++;
	}
	while(t < tLast);
	//TRACE("Number of weeks in %s is %d\n", GetMonth_Desc(), iTotalInMonth);
	return iTotalInMonth;
}

const int CTimeEx::GetWeek_No_TotalOfYear()
// Return number of weeks of this year.
{ return GetWeek_No_LastOfYear(); }

const bool CTimeEx::IsWeekend()
// Return T if this day in this week is in a weekend and F if not.
{ return IsWeekend(GetDayOfWeek()); }

const bool CTimeEx::IsWeekend(const int iDayOfWeek)
// Return T if this day in this week is in a weekend and F if not.
{
	return(iDayOfWeek == DAY_SATURDAY || 
		   iDayOfWeek == DAY_SUNDAY);
}

// ******************************
// * Month related.				*
// ******************************
const CString CTimeEx::GetMonth_Desc()
// Return desc. of month in year.
{ return GetMonth_Desc(GetMonth()); }

const CString CTimeEx::GetMonth_Desc(const int iMonth) // Is static.
// Return desc. of month in year.
{
	const CString cstrMonths[12] =
	{
		_T("january"),
		_T("february"),
		_T("march"),
		_T("april"),
		_T("may"),
		_T("june"),
		_T("july"),
		_T("august"),
		_T("september"),
		_T("october"),
		_T("november"),
		_T("december")
	};
	return cstrMonths[iMonth - 1];
}

const int CTimeEx::GetMonth_Length()
// Return length of this month.
{ return GetMonth_Length(GetMonth()); }

const int CTimeEx::GetMonth_Length(const int iMonth)
// Return length of this month.
{
	int iMonths[12] = 
	{ 
		31, // Lenght of january.
		(IsLeapYear() ? 29 : 28), // Lenght of february.
		31, // Lenght of march.
		30, // Lenght of april.
		31, // Lenght of may.
		30, // Lenght of june.
		31, // Lenght of july.
		31, // Lenght of august.
		30, // Lenght of september.
		31, // Lenght of october.
		30, // Lenght of november.
		31 // Lenght of december.
	};
	return iMonths[iMonth - 1];
}

// ******************************
// * Year related.				*
// ******************************
const bool CTimeEx::IsLeapYear()
// Return T if this year is a leap year and F if not.
{ return IsLeapYear(GetYear()); }

const bool CTimeEx::IsLeapYear(const int iYear) // Is static.
// Return T if this year is a leap year and F if not.
{ return((iYear % 100 == 0) && (iYear % 400 != 0)); }

// ******************************
// * General.					*
// ******************************
const CString CTimeEx::GetUSDate(const bool bShortFrom)
// Return US date.
{
	CString cstrUSDate(_T(""));
	if(bShortFrom)
		cstrUSDate.Format(TEXT("%s, %s %d, %d"),
						  (LPCTSTR)GetDay_Desc(GetDayOfWeek(), false),
						  (LPCTSTR)GetMonth_Desc(),
						  GetDay(),
						  GetYear());
	else
		cstrUSDate.Format(TEXT("%s, %s %d, %d - %0.2d:%0.2d:%0.2d"),
						  (LPCTSTR)GetDay_Desc(GetDayOfWeek(), false),
						  (LPCTSTR)GetMonth_Desc(),
						  GetDay(),
						  GetYear(),
						  GetHour(),
						  GetMinute(),
						  GetSecond());
	first_upper_rest_lower(cstrUSDate);
	return cstrUSDate;
}

// ------------------------------------------------------------------------------------------------------------------------------------------------------
// -																																				  	-
// ------------------------------------------------------------------------------------------------------------------------------------------------------

// 0. Data. (alphabetical).
// 1. Object. (alphabetical).
// 2. Event's. (alphabetical).
// 3.0. Menu choice. (menu order).
// 3.1. Menu choice, enablers. (menu order).
// 4. Slaves. (alphabetical).
// 5. Other. (alphabetical).

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -