numpy 2.0.0
src/multiarray/datetime.c File Reference
#include <Python.h>
#include <datetime.h>
#include <time.h>
#include <numpy/ndarrayobject.h>
#include "npy_config.h"
#include "numpy/npy_3kcompat.h"
#include "_datetime.h"

Data Structures

struct  ymdstruct
struct  hmsstruct

Defines

#define PY_SSIZE_T_CLEAN
#define _MULTIARRAYMODULE
#define NPY_NO_PREFIX
#define NPY_FR_ERR   -1
#define Py_AssertWithArg(x, errortype, errorstr, a1)   {if (!(x)) {PyErr_Format(errortype,errorstr,a1);goto onError;}}
#define secs_from_hms(hour, min, sec, multiplier)
#define _DAYS_PER_MONTH   30.436875
#define _DAYS_PER_YEAR   365.2425

Functions

static int is_leapyear (long year)
static int day_of_week (npy_longlong absdate)
static npy_longlong year_offset (npy_longlong year)
static npy_longlong days_from_ymd (int year, int month, int day)
static ymdstruct days_to_ymdstruct (npy_datetime dlong)
static hmsstruct seconds_to_hmsstruct (npy_longlong dlong)
NPY_NO_EXPORT npy_datetime PyArray_DatetimeStructToDatetime (NPY_DATETIMEUNIT fr, npy_datetimestruct *d)
NPY_NO_EXPORT npy_datetime PyArray_TimedeltaStructToTimedelta (NPY_DATETIMEUNIT fr, npy_timedeltastruct *d)
NPY_NO_EXPORT void PyArray_DatetimeToDatetimeStruct (npy_datetime val, NPY_DATETIMEUNIT fr, npy_datetimestruct *result)
NPY_NO_EXPORT void PyArray_TimedeltaToTimedeltaStruct (npy_timedelta val, NPY_DATETIMEUNIT fr, npy_timedeltastruct *result)

Variables

static int month_offset [2][13]
static int days_in_month [2][12]

Define Documentation

#define _DAYS_PER_MONTH   30.436875
Uses Average values when frequency is Y, M, or B
#define _DAYS_PER_YEAR   365.2425
#define _MULTIARRAYMODULE
#define NPY_FR_ERR   -1
For defaults and errors
#define NPY_NO_PREFIX
#define Py_AssertWithArg (   x,
  errortype,
  errorstr,
  a1 
)    {if (!(x)) {PyErr_Format(errortype,errorstr,a1);goto onError;}}

== Beginning of section borrowed from mx.DateTime ==

Functions in the following section are borrowed from mx.DateTime version 2.0.6, and hence this code is subject to the terms of the egenix public license version 1.0.0
#define PY_SSIZE_T_CLEAN
#define secs_from_hms (   hour,
  min,
  sec,
  multiplier 
)
Value:
(\
  ((hour)*3600 + (min)*60 + (sec)) * (npy_int64)(multiplier)\
)
Returns absolute seconds from an hour, minute, and second

Function Documentation

static int day_of_week ( npy_longlong  absdate) [static]
Return the day of the week for the given absolute date. Monday is 0 and Sunday is 6

Add in four for the Thursday on Jan 1, 1970 (epoch offset)

static npy_longlong days_from_ymd ( int  year,
int  month,
int  day 
) [static]
Modified version of mxDateTime function Returns absolute number of days since Jan 1, 1970 assuming a proleptic Gregorian Calendar Raises a ValueError if out of range month or day day -1 is Dec 31, 1969, day 0 is Jan 1, 1970, day 1 is Jan 2, 1970

Calculate the absolute date
Is it a leap year ?
Negative month values indicate months relative to the years end
Negative values indicate days relative to the months end
Number of days between Dec 31, (year - 1) and Dec 31, 1969
(can be negative).
Calculate the number of days using yearoffset Jan 1, 1970 is day 0 and thus Dec. 31, 1969 is day -1

static ymdstruct days_to_ymdstruct ( npy_datetime  dlong) [static]
Takes a number of days since Jan 1, 1970 (positive or negative) and returns the year. month, and day in the proleptic Gregorian calendar
Examples:
-1 returns 1969, 12, 31 0 returns 1970, 1, 1 1 returns 1970, 1, 2

Approximate year
Apply corrections to reach the correct year
Calculate the year offset
Backward correction: absdate must be greater than the yearoffset
Forward correction: non leap years only have 365 days
Now iterate to find the month

Referenced by PyArray_TimedeltaStructToTimedelta().

static int is_leapyear ( long  year) [static]
Return 1/0 iff year points to a leap year in calendar.
NPY_NO_EXPORT npy_datetime PyArray_DatetimeStructToDatetime ( NPY_DATETIMEUNIT  fr,
npy_datetimestruct d 
)

== End of section adapted from mx.DateTime ==

System Message: SEVERE/4 (<string>, line 1)
Missing matching underline for section title overline.

==================================================
Parsing DateTime struct and returns a date-time number
=================================================

<blockquote> Structure is assumed to be already normalized</blockquote>

Create a datetime value from a filled datetime struct and resolution unit.

<

The absolute number of days since Jan 1, 1970
This is just 7-days for now.
Invalid business day
offset to adjust first week
only 2.6 hours
get number of seconds as a postive or negative number
only 9.2 secs
Shouldn't get here

NPY_NO_EXPORT void PyArray_DatetimeToDatetimeStruct ( npy_datetime  val,
NPY_DATETIMEUNIT  fr,
npy_datetimestruct result 
)
Fill the datetime struct from the value and resolution unit.

Note that what looks like val / N and val % N for positive numbers maps to [val - (N-1)] / N and [N-1 + (val+1) % N] for negative numbers (with the 2nd value, the remainder, being positive in both cases).
A week is the same as 7 days
Number of business days since Thursday, 1-1-70
A buisness day is M T W Th F (i.e. all but Sat and Sun.) Convert the business day to the number of actual days.

Must convert [0,1,2,3,4,5,6,7,...] to
[0,1,4,5,6,7,8,11,...]
and [...,-9,-8,-7,-6,-5,-4,-3,-2,-1,0] to
[...,-13,-10,-9,-8,-7,-6,-3,-2,-1,0]
Recall how C computes / and % with negative numbers
entire range is only += 2.6 hours
tmp (number of fs) will be positive after this segment
we are at the last second
entire range is only += 9.2 seconds

NPY_NO_EXPORT npy_datetime PyArray_TimedeltaStructToTimedelta ( NPY_DATETIMEUNIT  fr,
npy_timedeltastruct d 
)
Create a timdelta value from a filled timedelta struct and resolution unit.

This is just 7-days for now.
What is the meaning of a relative Business day?
This assumes you want to take the day difference and convert it to business-day difference (removing 2 every 7).
only 2.6 hours
only 9.2 secs
Shouldn't get here

References ymdstruct::day, days_to_ymdstruct(), hmsstruct::hour, hmsstruct::min, ymdstruct::month, NPY_FR_B, NPY_FR_D, NPY_FR_h, NPY_FR_m, NPY_FR_M, NPY_FR_s, NPY_FR_W, NPY_FR_Y, hmsstruct::sec, seconds_to_hmsstruct(), and ymdstruct::year.

NPY_NO_EXPORT void PyArray_TimedeltaToTimedeltaStruct ( npy_timedelta  val,
NPY_DATETIMEUNIT  fr,
npy_timedeltastruct result 
)
FIXME: Overflow is not handled at all
To convert from Years, Months, and Business Days, multiplication by the average is done
Fill the timedelta struct from the timedelta value and resolution unit.

Note that what looks like val / N and val % N for positive numbers maps to [val - (N-1)] / N and [N-1 + (val+1) % N] for negative numbers (with the 2nd value, the remainder, being positive in both cases).
Number of business days since Thursday, 1-1-70
entire range is only += 9.2 hours
entire range is only += 2.6 seconds

static hmsstruct seconds_to_hmsstruct ( npy_longlong  dlong) [static]
Converts an integer number of seconds in a day to hours minutes seconds. It assumes seconds is between 0 and 86399.

Referenced by PyArray_TimedeltaStructToTimedelta().

static npy_longlong year_offset ( npy_longlong  year) [static]
Return the year offset, that is the absolute date of the day 31.12.(year-1) since 31.12.1969 in the proleptic Gregorian calendar.

Note that 477 == 1969/4 - 1969/100 + 1969/400


Variable Documentation

int days_in_month[2][12] [static]
Initial value:
 {
    { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
    { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
}
Table of number of days in a month (0-based, without and with leap)
int month_offset[2][13] [static]
Initial value:
 {
    { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
    { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
}
Table with day offsets for each month (0-based, without and with leap)