time_t stc_time_get_month_start(time_t now, int month_start_date)
{
- struct tm *curr;
+ struct tm curr;
+ struct tm *res;
bool is_leap_year;
- curr = localtime(&now);
+ res = localtime_r(&now, &curr);
- curr->tm_sec = 0;
- curr->tm_min = 0;
- curr->tm_hour = 0;
+ curr.tm_sec = 0;
+ curr.tm_min = 0;
+ curr.tm_hour = 0;
- if (curr->tm_mday < month_start_date) {
- curr->tm_mon--;
- if (curr->tm_mon < 0) {
- curr->tm_mon = 11;
- curr->tm_year--;
+ if (curr.tm_mday < month_start_date) {
+ curr.tm_mon--;
+ if (curr.tm_mon < 0) {
+ curr.tm_mon = 11;
+ curr.tm_year--;
}
}
- is_leap_year = ((curr->tm_year + 1900) % 4 ? 0 : 1);
- curr->tm_mday = month_start_date;
+ is_leap_year = ((curr.tm_year + 1900) % 4 ? 0 : 1);
+ curr.tm_mday = month_start_date;
switch (month_start_date) {
case 29:
case 30:
- if (curr->tm_mon == 1 && !is_leap_year)
- curr->tm_mday = 28;
+ if (curr.tm_mon == 1 && !is_leap_year)
+ curr.tm_mday = 28;
- else if (curr->tm_mon == 1 && is_leap_year)
- curr->tm_mday = 29;
+ else if (curr.tm_mon == 1 && is_leap_year)
+ curr.tm_mday = 29;
break;
case 31:
- if (curr->tm_mon == 1 && !is_leap_year)
- curr->tm_mday = 28;
+ if (curr.tm_mon == 1 && !is_leap_year)
+ curr.tm_mday = 28;
- else if (curr->tm_mon == 1 && is_leap_year)
- curr->tm_mday = 29;
+ else if (curr.tm_mon == 1 && is_leap_year)
+ curr.tm_mday = 29;
- else if (curr->tm_mon == 3 || curr->tm_mon == 5 ||
- curr->tm_mon == 8 || curr->tm_mon == 10)
- curr->tm_mday = 30;
+ else if (curr.tm_mon == 3 || curr.tm_mon == 5 ||
+ curr.tm_mon == 8 || curr.tm_mon == 10)
+ curr.tm_mday = 30;
break;
default:
;//Do Nothing
};
- return mktime(curr);
+ return mktime(&curr);
}