Python datetime.hour() Examples

The following are 12 code examples for showing how to use datetime.hour(). 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: 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 2
Project: yaql   Author: openstack   File: date_time.py    License: Apache License 2.0 6 votes vote down vote up
def date(dt):
    """:yaql:property date

    Returns datetime object with only year, month, day and tzinfo
    part of given datetime.

    :signature: datetime.date
    :returnType: datetime object

    .. code::

        yaql> let(datetime(2006, 11, 21, 16, 30, 2, 123).date) ->
            [$.year, $.month, $.day, $.hour]
        [2006, 11, 21, 0]
    """
    return DATETIME_TYPE(
        year=dt.year, month=dt.month, day=dt.day, tzinfo=dt.tzinfo) 
Example 3
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 4
Project: rl-testbed-for-energyplus   Author: IBM   File: energyplus_model.py    License: MIT License 6 votes vote down vote up
def _parse_datetime(self, dstr):
        # ' MM/DD  HH:MM:SS' or 'MM/DD  HH:MM:SS'
        # Dirty hack
        if dstr[0] != ' ':
            dstr = ' ' + dstr
        #year = 2017
        year = 2013 # for CHICAGO_IL_USA TMY2-94846
        month = int(dstr[1:3])
        day = int(dstr[4:6])
        hour = int(dstr[8:10])
        minute = int(dstr[11:13])
        sec = 0
        msec = 0
        if hour == 24:
            hour = 0
            dt = datetime(year, month, day, hour, minute, sec, msec) + timedelta(days=1)
        else:
            dt = datetime(year, month, day, hour, minute, sec, msec)
        return dt

    # Convert list of date/time string to list of datetime objects 
Example 5
Project: rl-testbed-for-energyplus   Author: IBM   File: energyplus_model.py    License: MIT License 6 votes vote down vote up
def _convert_datetime24(self, dates):
        # ' MM/DD  HH:MM:SS'
        dates_new = []
        for d in dates:
            #year = 2017
            #month = int(d[1:3])
            #day = int(d[4:6])
            #hour = int(d[8:10])
            #minute = int(d[11:13])
            #sec = 0
            #msec = 0
            #if hour == 24:
            #    hour = 0
            #    d_new = datetime(year, month, day, hour, minute, sec, msec) + dt.timedelta(days=1)
            #else:
            #    d_new = datetime(year, month, day, hour, minute, sec, msec)
            #dates_new.append(d_new)
            dates_new.append(self._parse_datetime(d))
        return dates_new

    # Generate x_pos and x_labels 
Example 6
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 7
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 8
Project: yaql   Author: openstack   File: date_time.py    License: Apache License 2.0 5 votes vote down vote up
def hour(dt):
    """:yaql:property hour

    Returns hour of given datetime.

    :signature: datetime.hour
    :returnType: integer

    .. code::

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

    Returns datetime converted to UTC.

    :signature: datetime.utc
    :returnType: datetime object

    .. code::

        yaql> datetime(2006, 11, 21, 16, 30, offset =>
            timespan(hours => 3)).utc.hour
        13
    """
    return dt - dt.utcoffset() 
Example 10
Project: PyRat   Author: vgripon   File: pyrat.py    License: GNU General Public License v3.0 5 votes vote down vote up
def convert_time_to_int(datetime):
    return datetime.hour * 3600000 + datetime.minute * 60000 + datetime.second * 1000 + datetime.microsecond / 1000.0

# Convert the decision taken by an AI into an actual new location 
Example 11
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 12
Project: rl-testbed-for-energyplus   Author: IBM   File: energyplus_model.py    License: MIT License 5 votes vote down vote up
def generate_x_pos_x_labels(self, dates):
        time_delta  = self._parse_datetime(dates[1]) - self._parse_datetime(dates[0])
        x_pos = []
        x_labels = []
        for i, d in enumerate(dates):
            dt = self._parse_datetime(d) - time_delta
            if dt.hour == 0 and dt.minute == 0:
                x_pos.append(i)
                x_labels.append(dt.strftime('%m/%d'))
        return x_pos, x_labels