📄 gdate.cpp
字号:
/* Copyright (C) 2006, Mike Gashler This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. see http://www.gnu.org/copyleft/lesser.html*/#include <string.h>#include <stdio.h>#include "GDate.h"#include "GString.h"#include "GMacros.h"#ifndef WIN32#include <stdlib.h>#endif // not WIN32// ConstructorGDate::GDate(){ m_nDay = 0; m_nMonth = 0; m_nYear = 0; m_nHours = 0; m_nMinutes = 0; m_nSeconds = 0;}// DestructorGDate::~GDate(){}// -1 = This date is before pDate// 0 = This date is the same as pDate// 1 = This date is after pDateint GDate::Compare(GDate* pDate){ if(pDate == this) return 0; if(m_nYear < pDate->m_nYear) return -1; if(m_nYear > pDate->m_nYear) return 1; if(m_nMonth < pDate->m_nMonth) return -1; if(m_nMonth > pDate->m_nMonth) return 1; if(m_nDay < pDate->m_nDay) return -1; if(m_nDay > pDate->m_nDay) return 1; //todo add seconds return 0;}int GDate::GetYear(){ return(m_nYear);}// Constant strings used by the month methodsconst char* MONTHS = "JanFebMarAprMayJunJulAugSepOctNovDec";// This Converts a month number like 12 into text like "Dec"/*static */ void GDate::GetMonthText(int nMonth, char* szBuff, int nMaxSize){ if(nMaxSize < 1) return; if(nMonth < 1) { strcpy(szBuff, ""); return; } int n; for(n = 0; n < 3 && n < nMaxSize - 1; n++) szBuff[n] = MONTHS[3 * (nMonth - 1) + n]; szBuff[n] = '\0';}// This converts a string like "July" into a number like 7 [even if it is spelled wrong, like Julyaaaaa/*static */ int GDate::GetMonthNumber(char* szMonth){ int n; int i; char c1; char c2; bool bSame; // loop through each month and check if it is the same for(n = 0; n < 12; n++) { bSame = true; for(i = 0; i < 3; i++) { c1 = szMonth[i]; c2 = MONTHS[3 * n + i]; if(c1 >= 'a') c1 = c1 - 'a' + 'A'; if(c2 >= 'a') c2 = c2 - 'a' + 'A'; if(c1 != c2) bSame = false; } if(bSame) return(n + 1); } return(0);}// This produces a string like: "4 Jul 1776"void GDate::GetDateText(char* szBuff, int nMaxSize){ char szDay[20]; char szMonth[20]; char szYear[20]; if(m_nDay > 0) sprintf(szDay, "%d", m_nDay); else strcpy(szDay, ""); GetMonthText(m_nMonth, szMonth, 20); if(m_nYear > 0) sprintf(szYear, "%d", m_nYear); else strcpy(szYear, ""); char szTot[64]; sprintf(szTot, "%s %s %s", szDay, szMonth, szYear); GString::StrCpy(szBuff, szTot, nMaxSize); // todo add the seconds, etc., if they exist}int GDate::getAsSecondsPast1970WithAllMonthsAsThirtyDays(){ return (m_nYear-1970)*(12*30)*24*60*60+m_nMonth*30*24*60*60+m_nDay*24*60*60+m_nHours*60*60+m_nMinutes*60 + m_nSeconds; // phew!}// Parses a string into the date object like 12 May 2005 12:34:35 [last are for times!]void GDate::SetDate(char* szDate){// searches for either a recognizable month, or numbers, takes the larger of the two numbers as the day... [takes the first two numbers or three...or something...with just numbers I don't know what it does] m_nDay = 0; m_nMonth = 0; m_nYear = 0; int nData[3]; bool bMonth[3]; int nPieces = 0; int n = 0; int i; char c; while(true) { if(nPieces >= 3) break; // Search for the start of the next piece of info while(true) { if(szDate[n] == '\0') break; if(szDate[n] >= '0' && szDate[n] <= '9') break; if(szDate[n] >= 'A' && szDate[n] <= 'Z') break; if(szDate[n] >= 'a' && szDate[n] <= 'z') break; n++; } if(szDate[n] == '\0') break; // reached the end. if(szDate[n] >= '0' && szDate[n] <= '9') { // We found a number for(i = n + 1; true; i++) { if(szDate[i] < '0' || szDate[i] > '9') break; } c = szDate[i]; szDate[i] = '\0'; nData[nPieces] = atoi(szDate + n); bMonth[nPieces] = false; nPieces++; szDate[i] = c; n = i; } else if ( (szDate[n] <= 'z' && szDate[n] >= 'a') || (szDate[n] <= 'Z' && szDate[n] >= 'A') ) { // We found some letters -- continue parsing until we are past them for(i = n + 1; true; i++) { bool isLetter = false; if(szDate[i] >= 'A' && szDate[i] <= 'Z') isLetter = true; else if(szDate[i] <= 'z' && szDate[i] >= 'a') isLetter = true; if(!isLetter) break; } c = szDate[i]; szDate[i] = '\0'; // temporarily add a zero in there, so that GetMonthNumber will be able to parse it 'quickly' when called int monthNumberGot = GetMonthNumber(szDate + n); if(monthNumberGot != 0) { nData[nPieces] = monthNumberGot; bMonth[nPieces] = true; nPieces++; } szDate[i] = c; n = i; }else { //weird char! n++; } } // Put the data we found into the right field for(int j = 0; j < nPieces; j++) { if(bMonth[j]) m_nMonth = nData[j]; else { if(nData[j] <= 31) m_nDay = nData[j]; else m_nYear = nData[j]; } } // now look for 00:00:00 -- or rather three more numbers... int numberWeHaveSoFar = 0; int allNumbersToHere[3]; while(numberWeHaveSoFar < 3 && szDate[n] != '\0') { while(true) { if(szDate[n] == '\0') break; if(szDate[n] >= '0' && szDate[n] <= '9') break; n++; } if(szDate[n] == '\0') break; else if((szDate[n] >= '0' && szDate[n] <= '9')) { for(i = n + 1; true; i++) { if(szDate[i] < '0' || szDate[i] > '9') break; } // go and get it with atoi c = szDate[i]; szDate[i] = '\0'; allNumbersToHere[numberWeHaveSoFar] = atoi(szDate + n); numberWeHaveSoFar++; szDate[i] = c; n = i;} else { //garbage n++; } } if(numberWeHaveSoFar > 0) { m_nHours = allNumbersToHere[0]; } if(numberWeHaveSoFar > 1) { m_nMinutes = allNumbersToHere[1]; } if(numberWeHaveSoFar > 2) { m_nMinutes = allNumbersToHere[2]; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -