Python datetime.day() Examples

The following are 25 code examples for showing how to use datetime.day(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module datetime , or try the search function .

Example 1
Project: timefhuman   Author: alvinwan   File: data.py    License: Apache License 2.0 6 votes vote down vote up
def combine(self, other):
        """

        >>> dt = DayTimeToken(2018, 8, 18, 3, 0, 'pm')
        >>> day = DayToken(8, 20, 2018)
        >>> dt.combine(day)
        8/20/2018 3 pm
        >>> time = TimeToken(5, 'pm')
        >>> dt.combine(time)
        8/18/2018 5 pm
        """
        assert isinstance(other, (DayToken, TimeToken))
        if isinstance(other, DayToken):
            return other.combine(self.time)
        elif isinstance(other, TimeToken):
            self.time.apply(other)
            return self.day.combine(other) 
Example 2
Project: timefhuman   Author: alvinwan   File: data.py    License: Apache License 2.0 6 votes vote down vote up
def combine(self, time):
        """
        >>> day = DayToken(8, 5, 2018)
        >>> time = TimeToken(3, 'pm')
        >>> time_range = TimeRange(TimeToken(3, 'pm'), TimeToken(5, 'pm'))
        >>> day.combine(time)
        8/5/2018 3 pm
        >>> day.combine(time_range)
        8/5/2018 3-5 pm
        """
        assert isinstance(time, (TimeRange, TimeToken, DayTimeToken))
        if isinstance(time, TimeToken):
            return DayTimeToken.from_day_time(self, time)
        if isinstance(time, DayTimeToken):
            return self.combine(time.time)
        return DayTimeRange(
            DayTimeToken.from_day_time(self, time.start),
            DayTimeToken.from_day_time(self, time.end)) 
Example 3
Project: yaql   Author: openstack   File: date_time.py    License: Apache License 2.0 6 votes vote down vote up
def datetime_from_timestamp(timestamp, offset=ZERO_TIMESPAN):
    """:yaql:datetime

    Returns datetime object built by timestamp.

    :signature: datetime(timestamp, offset => timespan(0))
    :arg timestamp: timespan object to represent datetime
    :argType timestamp: number
    :arg offset: datetime offset in microsecond resolution, needed for tzinfo,
        timespan(0) by default
    :argType offset: timespan type
    :returnType: datetime object

    .. code::

        yaql> let(datetime(1256953732)) -> [$.year, $.month, $.day]
        [2009, 10, 31]
    """
    zone = _get_tz(offset)
    return DATETIME_TYPE.fromtimestamp(timestamp, tz=zone) 
Example 4
Project: yaql   Author: openstack   File: date_time.py    License: Apache License 2.0 6 votes vote down vote up
def now(offset=ZERO_TIMESPAN):
    """:yaql:now

    Returns the current local date and time.

    :signature: now(offset => timespan(0))
    :arg offset: datetime offset in microsecond resolution, needed for tzinfo,
        timespan(0) by default
    :argType offset: timespan type
    :returnType: datetime

    .. code::

        yaql> let(now()) -> [$.year, $.month, $.day]
        [2016, 7, 18]
        yaql> now(offset=>localtz()).hour - now().hour
        3
    """
    zone = _get_tz(offset)
    return DATETIME_TYPE.now(tz=zone) 
Example 5
Project: yaql   Author: openstack   File: date_time.py    License: Apache License 2.0 6 votes vote down vote up
def register(context):
    functions = (
        build_datetime, build_timespan, datetime_from_timestamp,
        datetime_from_string, now, localtz, utctz, utc,
        days, hours, minutes, seconds, milliseconds, microseconds,
        datetime_plus_timespan, timespan_plus_datetime,
        datetime_minus_timespan, datetime_minus_datetime,
        timespan_plus_timespan, timespan_minus_timespan,
        datetime_gt_datetime, datetime_gte_datetime,
        datetime_lt_datetime, datetime_lte_datetime,
        timespan_gt_timespan, timespan_gte_timespan,
        timespan_lt_timespan, timespan_lte_timespan,
        negative_timespan, positive_timespan,
        timespan_by_num, num_by_timespan, div_timespans, div_timespan_by_num,
        year, month, day, hour, minute, second, microsecond, weekday,
        offset, timestamp, date, time, replace, format_, is_datetime,
        is_timespan
    )

    for func in functions:
        context.register_function(func) 
Example 6
Project: FinanceHub   Author: Finance-Hub   File: AnbimaHolidays.py    License: MIT License 6 votes vote down vote up
def check_date(self, date):
        """This function checks if a specific date is an ANBIMA holiday or not
        
        Arguments:
            date : datetime object
                A Datetime Object which represents the date the user wants to check. 
        """

        if type(date) is datetime.day:
            y = date.year
            m = date.month
            d = date.day
            date = datetime.datetime(y, m, d)
        elif type(date) is not datetime.datetime:
            raise TypeError('Please input a Datetime object.')

        if date in self.holidays:
            return True
        return False 
Example 7
Project: timefhuman   Author: alvinwan   File: data.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, year, month, day, relative_hour, minute=0, time_of_day=None):
        self.day = DayToken(month, day, year)
        self.time = TimeToken(relative_hour, time_of_day, minute) 
Example 8
Project: timefhuman   Author: alvinwan   File: data.py    License: Apache License 2.0 5 votes vote down vote up
def datetime(self, now):
        # TODO: handle Nones
        return datetime.datetime(
            self.day.year, self.day.month, self.day.day, self.time.hour, self.time.minute) 
Example 9
Project: timefhuman   Author: alvinwan   File: data.py    License: Apache License 2.0 5 votes vote down vote up
def from_day_time(day, time):
        return DayTimeToken(
            day.year, day.month, day.day, time.relative_hour, time.minute,
            time.time_of_day) 
Example 10
Project: timefhuman   Author: alvinwan   File: data.py    License: Apache License 2.0 5 votes vote down vote up
def __repr__(self):
        return '{} {}'.format(repr(self.day), repr(self.time)) 
Example 11
Project: timefhuman   Author: alvinwan   File: data.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, month, day, year):   # TODO: default Nones?
        self.month = month
        self.day = day
        self.year = year

        assert month is None or 1 <= month <= 12
        assert day is None or 1 <= day <= 31 
Example 12
Project: timefhuman   Author: alvinwan   File: data.py    License: Apache License 2.0 5 votes vote down vote up
def __add__(self, other):
        """
        >>> d1 = DayToken(3, 2, None)
        >>> d1 + 3
        3/5
        """
        assert isinstance(other, int)
        return DayToken(self.month, self.day + other, self.year) 
Example 13
Project: timefhuman   Author: alvinwan   File: data.py    License: Apache License 2.0 5 votes vote down vote up
def __radd__(self, other):
        """
        >>> d1 = DayToken(3, 2, None)
        >>> 3 + d1
        3/5
        """
        assert isinstance(other, int)
        return DayToken(self.month, self.day + other, self.year) 
Example 14
Project: timefhuman   Author: alvinwan   File: data.py    License: Apache License 2.0 5 votes vote down vote up
def datetime(self, now):
        return datetime.datetime(self.year, self.month, self.day) 
Example 15
Project: timefhuman   Author: alvinwan   File: data.py    License: Apache License 2.0 5 votes vote down vote up
def __eq__(self, other):
        """
        >>> DayToken(5, 7, 2018) == DayToken(5, 7, 2018)
        True
        >>> DayToken(7, 4, 2018) == DayToken(7, 6, 2018)
        False
        """
        if not isinstance(other, DayToken):
            return False
        return self.month == other.month and self.day == other.day and \
            self.year == other.year 
Example 16
Project: timefhuman   Author: alvinwan   File: data.py    License: Apache License 2.0 5 votes vote down vote up
def __repr__(self):
        if not self.year:
            return '{}/{}'.format(self.month, self.day)
        if not self.day:
            return '{}/{}'.format(self.month, self.year)
        # either all fields populated or would be confusing w/o null fields
        # (e.g., only month is non-null)
        return '{}/{}/{}'.format(
            self.month, self.day, self.year) 
Example 17
Project: timefhuman   Author: alvinwan   File: data.py    License: Apache License 2.0 5 votes vote down vote up
def datetime(self, now):
        return datetime.datetime(now.year, now.month, now.day, self.hour, self.minute) 
Example 18
Project: pvlib-python   Author: pvlib   File: test_spa.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_julian_day_dt(self):
        # add 1us manually to the test timestamp (GH #940)
        dt = times.tz_convert('UTC')[0] + pd.Timedelta(1, unit='us')
        year = dt.year
        month = dt.month
        day = dt.day
        hour = dt.hour
        minute = dt.minute
        second = dt.second
        microsecond = dt.microsecond
        assert_almost_equal(JD + 1e-6 / (3600*24),  # modify expected JD by 1us
                            self.spa.julian_day_dt(
                                year, month, day, hour,
                                minute, second, microsecond), 6) 
Example 19
Project: yaql   Author: openstack   File: date_time.py    License: Apache License 2.0 5 votes vote down vote up
def build_datetime(year, month, day, hour=0, minute=0, second=0,
                   microsecond=0, offset=ZERO_TIMESPAN):
    """:yaql:datetime

    Returns datetime object built on year, month, day, hour, minute, second,
    microsecond, offset.

    :signature: datetime(year, month, day, hour => 0, minute => 0, second => 0,
                         microsecond => 0, offset => timespan(0))
    :arg year: number of years in datetime
    :argType year: integer between 1 and 9999 inclusive
    :arg month: number of months in datetime
    :argType month: integer between 1 and 12 inclusive
    :arg day: number of days in datetime
    :argType day: integer between 1 and number of days in given month
    :arg hour: number of hours in datetime, 0 by default
    :argType hour: integer between 0 and 23 inclusive
    :arg minute: number of minutes in datetime, 0 by default
    :argType minute: integer between 0 and 59 inclusive
    :arg second: number of seconds in datetime, 0 by default
    :argType second: integer between 0 and 59 inclusive
    :arg microsecond: number of microseconds in datetime, 0 by default
    :argType microsecond: integer between 0 and 1000000-1
    :arg offset: datetime offset in microsecond resolution, needed for tzinfo,
        timespan(0) by default
    :argType offset: timespan type
    :returnType: datetime object

    .. code::

        yaql> let(datetime(2015, 9, 29)) -> [$.year, $.month, $.day]
        [2015, 9, 29]
    """
    zone = _get_tz(offset)
    return DATETIME_TYPE(year, month, day, hour, minute, second,
                         microsecond, zone) 
Example 20
Project: yaql   Author: openstack   File: date_time.py    License: Apache License 2.0 5 votes vote down vote up
def datetime_from_string(string, format__=None):
    """:yaql:datetime

    Returns datetime object built by string parsed with format.

    :signature: datetime(string, format => null)
    :arg string: string representing datetime
    :argType string: string
    :arg format: format for parsing input string which should be supported
        with C99 standard of format codes. null by default, which means
        parsing with Python dateutil.parser usage
    :argType format: string
    :returnType: datetime object

    .. code::

        yaql> let(datetime("29.8?2015")) -> [$.year, $.month, $.day]
        [2015, 8, 29]
        yaql> let(datetime("29.8?2015", "%d.%m?%Y"))->[$.year, $.month, $.day]
        [2015, 8, 29]
    """
    if not format__:
        result = parser.parse(string)
    else:
        result = DATETIME_TYPE.strptime(string, format__)
    if not result.tzinfo:
        return result.replace(tzinfo=UTCTZ)
    return result 
Example 21
Project: yaql   Author: openstack   File: date_time.py    License: Apache License 2.0 5 votes vote down vote up
def day(dt):
    """:yaql:property day

    Returns day of given datetime.

    :signature: datetime.day
    :returnType: integer

    .. code::

        yaql> datetime(2006, 11, 21, 16, 30).day
        21
    """
    return dt.day 
Example 22
Project: yaql   Author: openstack   File: date_time.py    License: Apache License 2.0 5 votes vote down vote up
def time(dt):
    """:yaql:property time

    Returns timespan object built on datetime without year, month, day and
    tzinfo part of it.

    :signature: datetime.time
    :returnType: timespan object

    .. code::

        yaql> let(datetime(2006, 11, 21, 16, 30).time) -> [$.hours, $.minutes]
        [16.5, 990.0]
    """
    return dt - date(dt) 
Example 23
Project: yaql   Author: openstack   File: date_time.py    License: Apache License 2.0 5 votes vote down vote up
def weekday(dt):
    """:yaql:property weekday

    Returns the day of the week as an integer, Monday is 0 and Sunday is 6.

    :signature: datetime.weekday
    :returnType: integer

    .. code::

        yaql> datetime(2006, 11, 21, 16, 30).weekday
        1
    """
    return dt.weekday() 
Example 24
Project: fluids   Author: CalebBell   File: test_spa.py    License: MIT License 5 votes vote down vote up
def test_julian_day_dt(self):
        dt = times.tz_convert('UTC')[0]
        year = dt.year
        month = dt.month
        day = dt.day
        hour = dt.hour
        minute = dt.minute
        second = dt.second
        microsecond = dt.microsecond
        assert_almost_equal(JD,
                             self.spa.julian_day_dt(year, month, day, hour,
                                           minute, second, microsecond), 6) 
Example 25
Project: yaql   Author: openstack   File: date_time.py    License: Apache License 2.0 4 votes vote down vote up
def replace(dt, year=None, month=None, day=None, hour=None, minute=None,
            second=None, microsecond=None, offset=None):
    """:yaql:replace

    Returns datetime object with applied replacements.

    :signature: dt.replace(year => null, month => null, day => null,
                           hour => null, minute => null, second => null,
                           microsecond => null, offset => null)
    :receiverArg dt: input datetime object
    :argType dt: datetime object
    :arg year: number of years to replace, null by default which means
        no replacement
    :argType year: integer between 1 and 9999 inclusive
    :arg month: number of months to replace, null by default which means
        no replacement
    :argType month: integer between 1 and 12 inclusive
    :arg day: number of days to replace, null by default which means
        no replacement
    :argType day: integer between 1 and number of days in given month
    :arg hour: number of hours to replace, null by default which means
        no replacement
    :argType hour: integer between 0 and 23 inclusive
    :arg minute: number of minutes to replace, null by default which means
        no replacement
    :argType minute: integer between 0 and 59 inclusive
    :arg second: number of seconds to replace, null by default which means
        no replacement
    :argType second: integer between 0 and 59 inclusive
    :arg microsecond: number of microseconds to replace, null by default
        which means no replacement
    :argType microsecond: integer between 0 and 1000000-1
    :arg offset: datetime offset in microsecond resolution to replace, null
        by default which means no replacement
    :argType offset: timespan type
    :returnType: datetime object

    .. code::

        yaql> datetime(2015, 9, 29).replace(year => 2014).year
        2014
    """
    replacements = {}
    if year is not None:
        replacements['year'] = year
    if month is not None:
        replacements['month'] = month
    if day is not None:
        replacements['day'] = day
    if hour is not None:
        replacements['hour'] = hour
    if minute is not None:
        replacements['minute'] = minute
    if second is not None:
        replacements['second'] = second
    if microsecond is not None:
        replacements['microsecond'] = microsecond
    if offset is not None:
        replacements['tzinfo'] = _get_tz(offset)

    return dt.replace(**replacements)