Checking if parsed date is within a date range
So I am using a scripting language with c++-like syntax, and I am trying to think of the best way to check if a date is within range. The problem I am running into is that if the current day is in a new month, the check is failing.
Here is what my code looks like:
if(iMonth >= iStartMonth && iMonth <= iEndMonth)
{
if(iDay >= iStartDay && iDay <= iEndDay)
{
if(iYear >= iStartYear && iYear <= iEndYear)
{
bEnabled = true;
return;
When I have something like this:
Start date: 3 27 2010 End Date: 4 15 2010 Current Date: 3 31 2010
The day check fails because if (iDay <= iEndDay) does not pass. The scripting language doesn't have a lot of time related functions, and I can't compare timestamps because I'm allowing users to put like "03:27:2010" and "04:15:2010" as start/end dates in a config file. I'm assuming I am ju开发者_JS百科st not thinking straight and missing an easy fix.
You can convert all dates to a strings in YYYY-MM-DD
format and then do lexicographical compares.
a good way that I found was using the following formula
(year * 10000) + (month * 100) + day
the date 25/09/2013 (day/month/year) becomes 20130925 do it for the start date, end date and the date you want to compare and compare them, the resulting code will look like this
bool isDateInRange(int day, int month, int year, int startDay, int startMonth, int startYear, int endDay, int endMonth, int endYear){
int entryDate = (year * 10000) + (month * 100) + day;
int startDate = (startYear * 10000) + (startMonth * 100) + startDay;
int endDate = (endYear * 10000) + (endMonth * 100) + endDay;
if (entryDate >= startDate && entryDate <= endDate){
return true;
}
else{
return false;
}
}
You should really use boost::DateTime instead of attempting to rewrite the wheel (which when the wheel is a date/time framework it's not as trivial as it may seem). This only if the code you pasted is C++ and not your scripting language (it wasn't clear). Also may I suggest to use Lua instead? :)
Anyway the problem is here:
if(iDay >= iStartDay && iDay <= iEndDay)
You should only check this if iMonth == iStartMonth, and the same for the end month. Otherwise iDay = 31
, iEndDay = 15
and 31 <= 15 will fail.
Also you should check for the year first, then the month, then the day.
If you don't find a library that does what you need, or if you prefer to do things yourself, I'm a huge fan of Calendrical Calculations, which shows how to represent each day as an integer, and how to convert back and forth to month/day/year in a large number of dating systems. The integer representation makes adjustment and comparsion trivial—and you get the day of the week by taking the value modulo 7! The original work was a journal article and has been turned into a book.
I've put Lua code at http://www.cs.tufts.edu/~nr/drop/lua/cal.lua.
#include <iostream>
#include <ctime>
using namespace std;
bool isDateInRange(int day, int month, int year, int startDay, int startMonth, int startYear, int endDay, int endMonth, int endYear){
int entryDate = (year * 10000) + (month * 100) + day;
int startDate = (startYear * 10000) + (startMonth * 100) + startDay;
int endDate = (endYear * 10000) + (endMonth * 100) + endDay;
if (entryDate >= startDate && entryDate <= endDate){
return true;
}
else{
return false;
}
}
void main()
{
int day, month, year, startDay, startMonth, startYear, endDay, endMonth, endYear;
// 11 June 2015
// current date
//day = 11; month = 6; year = 2015;
time_t t = time(NULL);
tm* timePtr = localtime(&t);
day = timePtr->tm_mday;
//cout << "day: " << day;
month = timePtr->tm_mon + 1;
//cout << "month: " << month;
year = timePtr->tm_year + 1900;
//cout << "year: " << year;
// 1 Jan 2015
startDay = 1; startMonth = 1; startYear = 2015;
// 12 July 2015
endDay = 12; endMonth = 5; endYear = 2015;
bool isInRange = isDateInRange(day, month, year, startDay, startMonth, startYear, endDay, endMonth, endYear);
if (isInRange)
{
cout << "In range " << endl;
}
else
{
cout << "Not in range " << endl;
}
精彩评论