Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for wrong date values #4554

Merged
merged 7 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix: wrong date values should not allowed in subscription's expires field (#4541)
66 changes: 66 additions & 0 deletions src/lib/common/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,53 @@ static int timezoneOffset(const char* tz)



/*****************************************************************************
*
* isLeapYear -
*
* This code will check, if the given year is a leap year or not.
*
*/
bool isLeapYear(int year)
{
// ref: https://www.geeksforgeeks.org/program-check-given-year-leap-year/
if (year % 400 == 0)
{
return true;
}
if ((year % 4 == 0) && (year % 100 != 0))
{
return true;
}
return false;
}



/*****************************************************************************
*
* daysInMonth -
*
* This code will check correct number of days in given month.
*
*/
int daysInMonth(int year, int month)
{
if (month == 1) //february
{
return isLeapYear(year) ? 29 : 28;
}
// for April, June, September, November
if (month == 3 || month == 5 || month == 8 || month == 10)
{
return 30;
}
// For all other months (Jan, March, May, July, Aug, October, December)
return 31;
}



/*****************************************************************************
*
* parse8601Time -
Expand Down Expand Up @@ -606,6 +653,25 @@ double parse8601Time(const std::string& ss)
time.tm_min = m; // 0-59
time.tm_sec = (int) s; // 0-61 (0-60 in C++11)

const int minYear = 0;
const int minMonth = 0;
const int maxMonth = 11;
const int minDay = 1;
const int maxDay = daysInMonth(y, time.tm_mon);;
const int minHour = 0;
const int maxHour = 23;
const int minMinute = 0;
const int maxMinute = 59;
const int minSecond = 0;
const int maxSecond = 59;

if (time.tm_year < minYear || time.tm_mon < minMonth || time.tm_mon > maxMonth || time.tm_mday < minDay ||
time.tm_mday > maxDay || time.tm_hour < minHour || time.tm_hour > maxHour || time.tm_min < minMinute ||
time.tm_min > maxMinute || time.tm_sec < minSecond || time.tm_sec > maxSecond)
Comment on lines +668 to +670
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about 2024-02-30? As far as I understand, this will pass this check, but February 30th is not a valid date ;)

Maybe you should make a function maxDay(month, year) which return the maxDay based on month and year. Basically:

  • January: 31
  • February: 29 in case of leap-year, 28 otherwise. Maybe you need an additional isLeapYear(year) function for this (maybe this reference is useful to implement it https://www.geeksforgeeks.org/program-check-given-year-leap-year)
  • March: 31
  • April: 30
  • May: 31
  • June: 30
  • July: 31
  • August: 31
  • September: 30
  • October: 31
  • November: 30
  • December: 31

Please include 2024-02-29 (should be correctly accepted) and 2023-02-29 (should return error) in your tests to check it works as expected.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 1a824bf

{
return -1;
}

int64_t totalSecs = timegm(&time) - offset;
float millis = s - (int) s;
double timestamp = totalSecs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ payload='{
"timestamp_0":
{
"type": "DateTime",
"value": "017-06-17T07:21:24.238Z",
"value": "2017-06-17T07:21:24.238Z",
"metadata": {
"very_hot_1":
{
Expand Down
Loading
Loading