unsigned char DateTime::get_week() const { throw_if_null(); int day_of_week = 1 + ((get_day_of_week()+6)%7); DateTime dt = *this; DateTime nearest_thursday = dt.add_days(4 - day_of_week); DateTime jan1(nearest_thursday.year, 1, 1); int days = jan1.get_difference_in_days(nearest_thursday); int week = 1 + days / 7; // Count of Thursdays return week; }
void print_date_xy(time t, uint8_t x, uint8_t y) { lcd_gotoxy(x, y); lcd_puts_p(dayStrings[get_day_of_week(t)]); lcd_putc(' '); lcd_puts_p(monthStrings[t.month - 1]); lcd_putc(' '); lcd_puts(digit_string(t.date)); lcd_puts(", "); lcd_puts(digit_string(t.year)); }
std::string DateTime::to_string() const { throw_if_null(); // Mon Feb 3 12:32:54 2008 std::string months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; std::string days[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; StringFormat format("%1 %2 %3 %4:%5:%6 %7"); format.set_arg(1, days[get_day_of_week()]); format.set_arg(2, months[get_month() - 1]); format.set_arg(3, get_day()); format.set_arg(4, get_hour(), 2); format.set_arg(5, get_minutes(), 2); format.set_arg(6, get_seconds(), 2); format.set_arg(7, get_year()); return format.get_result(); }
/* * Returns a boolean array with True for input dates which are valid * business days, and False for dates which are not. This is the * low-level function which requires already cleaned input data. * * dates: An array of dates with 'datetime64[D]' data type. * out: Either NULL, or an array with 'bool' data type * in which to place the resulting dates. * weekmask: A 7-element boolean mask, 1 for possible business days and 0 * for non-business days. * busdays_in_weekmask: A count of how many 1's there are in weekmask. * holidays_begin/holidays_end: A sorted list of dates matching '[D]' * unit metadata, with any dates falling on a day of the * week without weekmask[i] == 1 already filtered out. */ NPY_NO_EXPORT PyArrayObject * is_business_day(PyArrayObject *dates, PyArrayObject *out, npy_bool *weekmask, int busdays_in_weekmask, npy_datetime *holidays_begin, npy_datetime *holidays_end) { PyArray_DatetimeMetaData temp_meta; PyArray_Descr *dtypes[2] = {NULL, NULL}; NpyIter *iter = NULL; PyArrayObject *op[2] = {NULL, NULL}; npy_uint32 op_flags[2], flags; PyArrayObject *ret = NULL; if (busdays_in_weekmask == 0) { PyErr_SetString(PyExc_ValueError, "the business day weekmask must have at least one " "valid business day"); return NULL; } /* First create the data types for the dates and the bool output */ temp_meta.base = NPY_FR_D; temp_meta.num = 1; dtypes[0] = create_datetime_dtype(NPY_DATETIME, &temp_meta); if (dtypes[0] == NULL) { goto fail; } dtypes[1] = PyArray_DescrFromType(NPY_BOOL); if (dtypes[1] == NULL) { goto fail; } /* Set up the iterator parameters */ flags = NPY_ITER_EXTERNAL_LOOP| NPY_ITER_BUFFERED| NPY_ITER_ZEROSIZE_OK; op[0] = dates; op_flags[0] = NPY_ITER_READONLY | NPY_ITER_ALIGNED; op[1] = out; op_flags[1] = NPY_ITER_WRITEONLY | NPY_ITER_ALLOCATE | NPY_ITER_ALIGNED; /* Allocate the iterator */ iter = NpyIter_MultiNew(2, op, flags, NPY_KEEPORDER, NPY_SAFE_CASTING, op_flags, dtypes); if (iter == NULL) { goto fail; } /* Loop over all elements */ if (NpyIter_GetIterSize(iter) > 0) { NpyIter_IterNextFunc *iternext; char **dataptr; npy_intp *strideptr, *innersizeptr; iternext = NpyIter_GetIterNext(iter, NULL); if (iternext == NULL) { goto fail; } dataptr = NpyIter_GetDataPtrArray(iter); strideptr = NpyIter_GetInnerStrideArray(iter); innersizeptr = NpyIter_GetInnerLoopSizePtr(iter); do { char *data_dates = dataptr[0]; char *data_out = dataptr[1]; npy_intp stride_dates = strideptr[0]; npy_intp stride_out = strideptr[1]; npy_intp count = *innersizeptr; npy_datetime date; int day_of_week; while (count--) { /* Check if it's a business day */ date = *(npy_datetime *)data_dates; day_of_week = get_day_of_week(date); *(npy_bool *)data_out = weekmask[day_of_week] && !is_holiday(date, holidays_begin, holidays_end) && date != NPY_DATETIME_NAT; data_dates += stride_dates; data_out += stride_out; } } while (iternext(iter)); } /* Get the return object from the iterator */ ret = NpyIter_GetOperandArray(iter)[1]; Py_INCREF(ret); goto finish; fail: Py_XDECREF(ret); ret = NULL; finish: Py_XDECREF(dtypes[0]); Py_XDECREF(dtypes[1]); if (iter != NULL) { if (NpyIter_Deallocate(iter) != NPY_SUCCEED) { Py_XDECREF(ret); ret = NULL; } } return ret; }
/* * Applies a single business day count operation. See the function * business_day_count for the meaning of all the parameters. * * Returns 0 on success, -1 on failure. */ static int apply_business_day_count(npy_datetime date_begin, npy_datetime date_end, npy_int64 *out, npy_bool *weekmask, int busdays_in_weekmask, npy_datetime *holidays_begin, npy_datetime *holidays_end) { npy_int64 count, whole_weeks; int day_of_week = 0; int swapped = 0; /* If we get a NaT, raise an error */ if (date_begin == NPY_DATETIME_NAT || date_end == NPY_DATETIME_NAT) { PyErr_SetString(PyExc_ValueError, "Cannot compute a business day count with a NaT (not-a-time) " "date"); return -1; } /* Trivial empty date range */ if (date_begin == date_end) { *out = 0; return 0; } else if (date_begin > date_end) { npy_datetime tmp = date_begin; date_begin = date_end; date_end = tmp; swapped = 1; } /* Remove any earlier holidays */ holidays_begin = find_earliest_holiday_on_or_after(date_begin, holidays_begin, holidays_end); /* Remove any later holidays */ holidays_end = find_earliest_holiday_on_or_after(date_end, holidays_begin, holidays_end); /* Start the count as negative the number of holidays in the range */ count = -(holidays_end - holidays_begin); /* Add the whole weeks between date_begin and date_end */ whole_weeks = (date_end - date_begin) / 7; count += whole_weeks * busdays_in_weekmask; date_begin += whole_weeks * 7; if (date_begin < date_end) { /* Get the day of the week for 'date_begin' */ day_of_week = get_day_of_week(date_begin); /* Count the remaining days one by one */ while (date_begin < date_end) { if (weekmask[day_of_week]) { count++; } ++date_begin; if (++day_of_week == 7) { day_of_week = 0; } } } if (swapped) { count = -count; } *out = count; return 0; }
/* * Applies the 'roll' strategy to 'date', placing the result in 'out' * and setting 'out_day_of_week' to the day of the week that results. * * Returns 0 on success, -1 on failure. */ static int apply_business_day_roll(npy_datetime date, npy_datetime *out, int *out_day_of_week, NPY_BUSDAY_ROLL roll, npy_bool *weekmask, npy_datetime *holidays_begin, npy_datetime *holidays_end) { int day_of_week; /* Deal with NaT input */ if (date == NPY_DATETIME_NAT) { *out = NPY_DATETIME_NAT; if (roll == NPY_BUSDAY_RAISE) { PyErr_SetString(PyExc_ValueError, "NaT input in busday_offset"); return -1; } else { return 0; } } /* Get the day of the week for 'date' */ day_of_week = get_day_of_week(date); /* Apply the 'roll' if it's not a business day */ if (weekmask[day_of_week] == 0 || is_holiday(date, holidays_begin, holidays_end)) { npy_datetime start_date = date; int start_day_of_week = day_of_week; switch (roll) { case NPY_BUSDAY_FOLLOWING: case NPY_BUSDAY_MODIFIEDFOLLOWING: { do { ++date; if (++day_of_week == 7) { day_of_week = 0; } } while (weekmask[day_of_week] == 0 || is_holiday(date, holidays_begin, holidays_end)); if (roll == NPY_BUSDAY_MODIFIEDFOLLOWING) { /* If we crossed a month boundary, do preceding instead */ if (days_to_month_number(start_date) != days_to_month_number(date)) { date = start_date; day_of_week = start_day_of_week; do { --date; if (--day_of_week == -1) { day_of_week = 6; } } while (weekmask[day_of_week] == 0 || is_holiday(date, holidays_begin, holidays_end)); } } break; } case NPY_BUSDAY_PRECEDING: case NPY_BUSDAY_MODIFIEDPRECEDING: { do { --date; if (--day_of_week == -1) { day_of_week = 6; } } while (weekmask[day_of_week] == 0 || is_holiday(date, holidays_begin, holidays_end)); if (roll == NPY_BUSDAY_MODIFIEDPRECEDING) { /* If we crossed a month boundary, do following instead */ if (days_to_month_number(start_date) != days_to_month_number(date)) { date = start_date; day_of_week = start_day_of_week; do { ++date; if (++day_of_week == 7) { day_of_week = 0; } } while (weekmask[day_of_week] == 0 || is_holiday(date, holidays_begin, holidays_end)); } } break; } case NPY_BUSDAY_NAT: { date = NPY_DATETIME_NAT; break; } case NPY_BUSDAY_RAISE: { *out = NPY_DATETIME_NAT; PyErr_SetString(PyExc_ValueError, "Non-business day date in busday_offset"); return -1; } } } *out = date; *out_day_of_week = day_of_week; return 0; }