Python dateutil.tz.UTC Examples

The following are 30 code examples of dateutil.tz.UTC(). 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 also want to check out all available functions/classes of the module dateutil.tz , or try the search function .
Example #1
Source File: tz.py    From cloudkitty with Apache License 2.0 6 votes vote down vote up
def get_next_month(dt=None, naive=False):
    """Returns the start of the next month in the local timezone.

    If no parameter is provided, returns the start of the next month. If
    the provided parameter is naive, it will be considered as UTC.

    :param dt: Datetime to return the next month of.
    :type dt: datetime.datetime
    :param naive: If True, remove timezone information from the final object.
                  Defaults to False.
    :type naive: bool
    :rtype: datetime.datetime
    """
    start = get_month_start(dt, naive=naive)
    month_days = calendar.monthrange(start.year, start.month)[1]
    return add_delta(start, datetime.timedelta(days=month_days)) 
Example #2
Source File: test_imports.py    From plugin.video.emby with GNU General Public License v3.0 6 votes vote down vote up
def testTzAll(self):
        from dateutil.tz import tzutc
        from dateutil.tz import tzoffset
        from dateutil.tz import tzlocal
        from dateutil.tz import tzfile
        from dateutil.tz import tzrange
        from dateutil.tz import tzstr
        from dateutil.tz import tzical
        from dateutil.tz import gettz
        from dateutil.tz import tzwin
        from dateutil.tz import tzwinlocal
        from dateutil.tz import UTC
        from dateutil.tz import datetime_ambiguous
        from dateutil.tz import datetime_exists
        from dateutil.tz import resolve_imaginary

        tz_all = ["tzutc", "tzoffset", "tzlocal", "tzfile", "tzrange",
                  "tzstr", "tzical", "gettz", "datetime_ambiguous",
                  "datetime_exists", "resolve_imaginary", "UTC"]

        tz_all += ["tzwin", "tzwinlocal"] if sys.platform.startswith("win") else []
        lvars = locals()

        for var in tz_all:
            self.assertIsNot(lvars[var], None) 
Example #3
Source File: columns.py    From yui with GNU Affero General Public License v3.0 6 votes vote down vote up
def DateTimeAtColumn(prefix: str) -> hybrid_property:
    datetime_key = f'{prefix}_datetime'
    timezone_key = f'{prefix}_timezone'

    def getter(self) -> Optional[datetime.datetime]:
        dt: Optional[datetime.datetime] = getattr(self, datetime_key)
        tz: Optional[datetime.tzinfo] = getattr(self, timezone_key)
        if dt and tz:
            return dt.replace(tzinfo=UTC).astimezone(tz)
        if dt:
            return dt.replace(tzinfo=None)
        return dt

    def setter(self, dt: datetime.datetime):
        setattr(self, timezone_key, dt.tzinfo)
        setattr(self, datetime_key, dt.astimezone(UTC))

    def custom_comparator(cls):
        return DateTimeAtComparator(
            getattr(cls, datetime_key), getattr(cls, timezone_key),
        )

    return hybrid_property(
        fget=getter, fset=setter, custom_comparator=custom_comparator,
    ) 
Example #4
Source File: test_scheduler.py    From grimoirelab-kingarthur with GNU General Public License v3.0 6 votes vote down vote up
def test_task_not_rescheduled_archive_task(self):
        """Check if archive tasks are not rescheduled"""

        handler = CompletedJobHandler(self.task_scheduler)

        archiving_cfg = ArchivingTaskConfig('/tmp/archive', True)
        _ = self.task_scheduler.registry.add('mytask', 'git', 'commit', {}, archiving_cfg=archiving_cfg)

        result = JobResult(0, 1, 'mytask', 'git', 'commit')

        summary = Summary()
        summary.fetched = 10
        summary.last_updated_on = datetime.datetime(2010, 1, 1, 1, 0, 0, tzinfo=UTC)
        summary.max_updated_on = datetime.datetime(2014, 2, 12, 6, 10, 39, tzinfo=UTC)
        result.summary = summary

        event = JobEvent(JobEventType.COMPLETED, 0, 'mytask', result)

        handled = handler(event)
        self.assertEqual(handled, True)

        task = self.task_scheduler.registry.get('mytask')
        self.assertEqual(task.status, TaskStatus.COMPLETED) 
Example #5
Source File: icalparser.py    From icalevents with MIT License 6 votes vote down vote up
def normalize(dt, tz=UTC):
    """
    Convert date or datetime to datetime with timezone.

    :param dt: date to normalize
    :param tz: the normalized date's timezone
    :return: date as datetime with timezone
    """
    if type(dt) is date:
        dt = dt + relativedelta(hour=0)
    elif type(dt) is datetime:
        pass
    else:
        raise ValueError("unknown type %s" % type(dt))

    if dt.tzinfo:
        dt = dt.astimezone(tz)
    else:
        dt = dt.replace(tzinfo=tz)

    return dt 
Example #6
Source File: test_icalparser.py    From icalevents with MIT License 6 votes vote down vote up
def setUp(self):
        self.eventA = icalevents.icalparser.Event()
        self.eventA.uid = 1234
        self.eventA.start = datetime(year=2017, month=2, day=3, hour=12, minute=5, tzinfo=UTC)
        self.eventA.end = datetime(year=2017, month=2, day=3, hour=15, minute=5, tzinfo=UTC)
        self.eventA.all_day = False
        self.eventA.summary = "Event A"
        self.eventA.attendee = "name@example.com"
        self.eventA.organizer = "name@example.com"


        self.eventB = icalevents.icalparser.Event()
        self.eventB.uid = 1234
        self.eventB.start = datetime(year=2017, month=2, day=1, hour=15, minute=5, tzinfo=UTC)
        self.eventB.end = datetime(year=2017, month=2, day=1, hour=16, minute=5, tzinfo=UTC)
        self.eventB.all_day = False
        self.eventB.summary = "Event B"
        self.eventB.attendee = ["name@example.com", "another@example.com"]
        self.eventB.organizer = "name@example.com"


        self.dtA = datetime(2018, 6, 21, 12)
        self.dtB = datetime(2018, 6, 21, 12, tzinfo=gettz('Europe/Berlin')) 
Example #7
Source File: test_scheduler.py    From grimoirelab-kingarthur with GNU General Public License v3.0 6 votes vote down vote up
def test_task_rescheduling(self):
        """Check if the task related to the event is re-scheduled"""

        handler = CompletedJobHandler(self.task_scheduler)

        _ = self.registry.add('mytask', 'git', 'commit', {})

        result = JobResult(0, 1, 'mytask', 'git', 'commit')

        summary = Summary()
        summary.fetched = 10
        summary.last_updated_on = datetime.datetime(2010, 1, 1, 1, 0, 0, tzinfo=UTC)
        summary.max_updated_on = datetime.datetime(2014, 2, 12, 6, 10, 39, tzinfo=UTC)
        result.summary = summary

        event = JobEvent(JobEventType.COMPLETED, 0, 'mytask', result)

        handled = handler(event)
        self.assertEqual(handled, True)

        task = self.registry.get('mytask')
        self.assertEqual(task.status, TaskStatus.SCHEDULED) 
Example #8
Source File: test_icalevents.py    From icalevents with MIT License 6 votes vote down vote up
def test_events_recurring(self):
        ical = "test/test_data/recurring.ics"
        start = date(2018, 10, 15)
        end = date(2018, 11, 15)
        
        evs = icalevents.events(file=ical, start=start, end=end)
        
        e1 = evs[1]
        self.assertEqual(e1.start.hour, 10, "check time with DST")
        self.assertEqual(e1.start.tzinfo.utcoffset(e1.start), timedelta(seconds=7200), "check UTC offset with DST")
        
        e2 = evs[2]
        self.assertEqual(e2.start.hour, 10, "check time without DST")
        self.assertEqual(e2.start.tzinfo.utcoffset(e2.start), timedelta(seconds=3600), "check UTC offset without DST")
        
        self.assertEqual(e2.start.day, 5, "Check observance of exdate.") 
Example #9
Source File: test_icalevents.py    From icalevents with MIT License 6 votes vote down vote up
def test_event_str(self):
        ical = "test/test_data/duration.ics"
        start = date(2018, 1, 1)
        end = date(2018, 2, 1)
        n = datetime.now(UTC)
        m = relativedelta(hour=0, minute=0, second=0, microsecond=0)
        
        evs = icalevents.events(file=ical, start=start, end=end)
        
        e1 = evs[0]
        self.assertIsNotNone(search(r"ended", str(e1.copy_to(n - relativedelta(days=5) + m))), "stringify past event")
        self.assertIsNotNone(search(r"today", str(e1.copy_to(n - relativedelta(days=1) + m))), "stringify ongoing event")
        self.assertIsNotNone(search(r"days left", str(e1.copy_to(n + relativedelta(days=3) + m))), "stringify future event")
        
        e2 = evs[1]
        self.assertIsNotNone(search(r"ended", str(e2.copy_to(n - relativedelta(hours=5)))), "stringify past event")
        self.assertIsNotNone(search(r"now", str(e2.copy_to(n - relativedelta(hours=1)))), "stringify ongoing event")
        self.assertIsNotNone(search(r"hours left", str(e2.copy_to(n + relativedelta(hours=3)))), "stringify future event")
        self.assertIsNotNone(search(r"days left", str(e2.copy_to(n + relativedelta(days=3)))), "stringify future event") 
Example #10
Source File: isoparser.py    From pipenv with MIT License 6 votes vote down vote up
def parse_tzstr(self, tzstr, zero_as_utc=True):
        """
        Parse a valid ISO time zone string.

        See :func:`isoparser.isoparse` for details on supported formats.

        :param tzstr:
            A string representing an ISO time zone offset

        :param zero_as_utc:
            Whether to return :class:`dateutil.tz.tzutc` for zero-offset zones

        :return:
            Returns :class:`dateutil.tz.tzoffset` for offsets and
            :class:`dateutil.tz.tzutc` for ``Z`` and (if ``zero_as_utc`` is
            specified) offsets equivalent to UTC.
        """
        return self._parse_tzstr(tzstr, zero_as_utc=zero_as_utc)

    # Constants 
Example #11
Source File: converters.py    From qiskit-ibmq-provider with Apache License 2.0 6 votes vote down vote up
def utc_to_local(utc_dt: Union[datetime, str]) -> datetime:
    """Convert a UTC ``datetime`` object or string to a local timezone ``datetime``.

    Args:
        utc_dt: Input UTC `datetime` or string.

    Returns:
        A ``datetime`` with the local timezone.

    Raises:
        TypeError: If the input parameter value is not valid.
    """
    if isinstance(utc_dt, str):
        utc_dt = dateutil.parser.parse(utc_dt)
    if not isinstance(utc_dt, datetime):
        raise TypeError('Input `utc_dt` is not string or datetime.')
    utc_dt = utc_dt.replace(tzinfo=timezone.utc)  # type: ignore[arg-type]
    local_dt = utc_dt.astimezone(tz.tzlocal())  # type: ignore[attr-defined]
    return local_dt 
Example #12
Source File: converters.py    From qiskit-ibmq-provider with Apache License 2.0 6 votes vote down vote up
def utc_to_local_all(data: Any) -> Any:
    """Recursively convert all ``datetime`` in the input data from local time to UTC.

    Note:
        Only lists and dictionaries are traversed.

    Args:
        data: Data to be converted.

    Returns:
        Converted data.
    """
    if isinstance(data, datetime):
        return utc_to_local(data)
    elif isinstance(data, list):
        return [utc_to_local_all(elem) for elem in data]
    elif isinstance(data, dict):
        return {key: utc_to_local_all(elem) for key, elem in data.items()}
    return data 
Example #13
Source File: test_tz.py    From plugin.video.emby with GNU General Public License v3.0 6 votes vote down vote up
def testInZoneFoldEquality(self):
        # Two datetimes in the same zone are considered to be equal if their
        # wall times are equal, even if they have different absolute times.

        tzname = self._get_tzname('America/New_York')

        with self._gettz_context(tzname):
            NYC = self.gettz(tzname)
            UTC = tz.tzutc()

            dt0 = datetime(2011, 11, 6, 1, 30, tzinfo=NYC)
            dt1 = tz.enfold(dt0, fold=1)

            # Make sure these actually represent different times
            self.assertNotEqual(dt0.astimezone(UTC), dt1.astimezone(UTC))

            # Test that they compare equal
            self.assertEqual(dt0, dt1) 
Example #14
Source File: test_tz.py    From plugin.video.emby with GNU General Public License v3.0 6 votes vote down vote up
def testFoldPositiveUTCOffset(self):
        # Test that we can resolve ambiguous times
        tzname = 'AUS Eastern Standard Time'
        args = self.get_args(tzname)

        with self.context(tzname):
            # Calling fromutc() alters the tzfile object
            SYD = self.tzclass(*args)

            # Get the transition time in UTC from the object, because
            # Windows doesn't store historical info
            t_n, t0_u, t1_u = self.get_utc_transitions(SYD, 2012, False)

            # Using fresh tzfiles
            t0_syd = t0_u.astimezone(SYD)
            t1_syd = t1_u.astimezone(SYD)

            self.assertEqual(t0_syd.replace(tzinfo=None), t_n)

            self.assertEqual(t1_syd.replace(tzinfo=None), t_n)

            self.assertEqual(t0_syd.utcoffset(), timedelta(hours=11))
            self.assertEqual(t1_syd.utcoffset(), timedelta(hours=10))
            self.assertNotEqual(t0_syd.tzname(), t1_syd.tzname()) 
Example #15
Source File: tz.py    From cloudkitty with Apache License 2.0 6 votes vote down vote up
def add_delta(dt, delta):
    """Adds a timedelta to a datetime object.

    This is done by transforming the object to a naive UTC object, adding the
    timedelta and transforming it back to a localized object. This helps to
    avoid cases like this when transiting from winter to summertime:

    >>> dt, delta
    (datetime.datetime(2019, 3, 31, 0, 0, tzinfo=tzlocal()),
     datetime.timedelta(0, 3600))
    >>> dt += delta
    >>> dt.isoformat()
    '2019-03-31T01:00:00+01:00'
    >>> dt += delta
    >>> dt.isoformat()
    '2019-03-31T02:00:00+02:00' # This is the same time as the previous one
    """
    return utc_to_local(local_to_utc(dt, naive=True) + delta) 
Example #16
Source File: test_tz.py    From plugin.video.emby with GNU General Public License v3.0 6 votes vote down vote up
def testFoldLondon(self):
        tzname = self._get_tzname('Europe/London')

        with self._gettz_context(tzname):
            LON = self.gettz(tzname)
            UTC = tz.tzutc()

            t0_u = datetime(2013, 10, 27, 0, 30, tzinfo=UTC)   # BST
            t1_u = datetime(2013, 10, 27, 1, 30, tzinfo=UTC)   # GMT

            t0 = t0_u.astimezone(LON)
            t1 = t1_u.astimezone(LON)

            self.assertEqual(t0.replace(tzinfo=None),
                             datetime(2013, 10, 27, 1, 30))

            self.assertEqual(t1.replace(tzinfo=None),
                             datetime(2013, 10, 27, 1, 30))

            self.assertEqual(t0.utcoffset(), timedelta(hours=1))
            self.assertEqual(t1.utcoffset(), timedelta(hours=0)) 
Example #17
Source File: tz.py    From cloudkitty with Apache License 2.0 6 votes vote down vote up
def local_to_utc(dt, naive=False):
    """Converts a localized datetime object to UTC.

    If no tz info is provided, the object will be considered as being already
    in UTC, and the timezone will be set to UTC.

    :param dt: object to convert
    :type dt: datetime.datetime
    :param naive: If True, remove timezone information from the final object.
                  Defaults to False.
    :type naive: bool
    :rtype: datetime.datetime
    """
    # NOTE(peschk_l): In python2, astimezone() raises a ValueError if it is
    # applied to a naive datetime object. In python3 however, the naive object
    # is considered as being in the system's time.
    if dt.tzinfo is None:
        dt = dt.replace(tzinfo=tz.UTC)

    output = dt.astimezone(tz.UTC)
    if naive:
        output = output.replace(tzinfo=None)
    return output 
Example #18
Source File: converters.py    From qiskit-ibmq-provider with Apache License 2.0 6 votes vote down vote up
def local_to_utc(local_dt: Union[datetime, str]) -> datetime:
    """Convert a local ``datetime`` object or string to a UTC ``datetime``.

    Args:
        local_dt: Input local `datetime` or string.

    Returns:
        A ``datetime`` in UTC.

    Raises:
        TypeError: If the input parameter value is not valid.
    """
    if isinstance(local_dt, str):
        local_dt = dateutil.parser.parse(local_dt)
    if not isinstance(local_dt, datetime):
        raise TypeError('Input `local_dt` is not string or datetime.')

    # Input is considered local if it's ``utcoffset()`` is ``None`` or none-zero.
    if local_dt.utcoffset() is None or local_dt.utcoffset() != timedelta(0):
        local_dt = local_dt.replace(tzinfo=tz.tzlocal())
        return local_dt.astimezone(tz.UTC)
    return local_dt  # Already in UTC. 
Example #19
Source File: test_scheduler.py    From grimoirelab-kingarthur with GNU General Public License v3.0 5 votes vote down vote up
def test_task_rescheduling_unlimited_age(self):
        """Check if the task is re-scheduled when unlimited age is set"""

        handler = CompletedJobHandler(self.task_scheduler)

        scheduler_opts = SchedulingTaskConfig(delay=0, max_retries=0, max_age=None)
        task = self.registry.add('mytask', 'git', 'commit', {},
                                 scheduling_cfg=scheduler_opts)
        # Force the age to be large value
        task.age = 1000000

        result = JobResult(0, 1, 'mytask', 'git', 'commit')

        summary = Summary()
        summary.fetched = 10
        summary.last_updated_on = datetime.datetime(2010, 1, 1, 1, 0, 0, tzinfo=UTC)
        summary.max_updated_on = datetime.datetime(2014, 2, 12, 6, 10, 39, tzinfo=UTC)
        result.summary = summary

        event = JobEvent(JobEventType.COMPLETED, 0, 'mytask', result)

        handled = handler(event)
        self.assertEqual(handled, True)

        task = self.registry.get('mytask')
        self.assertEqual(task.status, TaskStatus.SCHEDULED) 
Example #20
Source File: test_scheduler.py    From grimoirelab-kingarthur with GNU General Public License v3.0 5 votes vote down vote up
def test_schedule_task(self):
        """Jobs should be added and executed"""

        args = {
            'uri': 'http://example.com/',
            'gitpath': os.path.join(self.dir, 'data/git_log.txt')
        }
        category = 'commit'
        archiving_opts = None
        scheduler_opts = SchedulingTaskConfig(delay=0, max_retries=0)

        registry = TaskRegistry(self.conn)
        task = registry.add('mytask', 'git', category, args,
                            archiving_cfg=archiving_opts,
                            scheduling_cfg=scheduler_opts)

        schlr = Scheduler(self.conn, registry, async_mode=False)
        schlr.schedule_task(task.task_id)

        task = registry.get('mytask')
        self.assertEqual(task.status, TaskStatus.SCHEDULED)
        self.assertEqual(task.age, 0)

        schlr.schedule()

        task = registry.get('mytask')
        self.assertEqual(task.age, 1)

        job = schlr._scheduler._queues[Q_CREATION_JOBS].fetch_job(task.jobs[0].id)
        result = job.return_value

        self.assertEqual(result.task_id, task.task_id)
        self.assertEqual(result.job_number, 1)
        self.assertEqual(result.summary.last_uuid, '1375b60d3c23ac9b81da92523e4144abc4489d4c')
        self.assertEqual(result.summary.max_updated_on,
                         datetime.datetime(2014, 2, 12, 6, 10, 39, tzinfo=UTC))
        self.assertEqual(result.summary.total, 9) 
Example #21
Source File: test_scheduler.py    From grimoirelab-kingarthur with GNU General Public License v3.0 5 votes vote down vote up
def test_schudule_task_retries_queue(self):
        """Failed task should be rescheduled to the retries queue"""

        args = {
            'uri': 'http://example.com/',
            'gitpath': os.path.join(self.dir, 'data/git_log.txt')
        }
        category = 'commit'
        archiving_opts = None
        scheduler_opts = SchedulingTaskConfig(delay=0, max_retries=0)

        registry = TaskRegistry(self.conn)
        task = registry.add('mytask', 'git', category, args,
                            archiving_cfg=archiving_opts,
                            scheduling_cfg=scheduler_opts)

        task.num_failures = 2  # Force number of failures before scheduling the task
        registry.update(task.task_id, task)

        schlr = Scheduler(self.conn, registry, async_mode=False)
        schlr.schedule_task(task.task_id)

        task = registry.get('mytask')
        self.assertEqual(task.status, TaskStatus.SCHEDULED)
        self.assertEqual(task.age, 0)

        schlr.schedule()

        task = registry.get('mytask')
        self.assertEqual(task.age, 1)

        job = schlr._scheduler._queues[Q_RETRYING_JOBS].fetch_job(task.jobs[0].id)
        result = job.return_value

        self.assertEqual(result.task_id, task.task_id)
        self.assertEqual(result.job_number, 1)
        self.assertEqual(result.summary.last_uuid, '1375b60d3c23ac9b81da92523e4144abc4489d4c')
        self.assertEqual(result.summary.max_updated_on,
                         datetime.datetime(2014, 2, 12, 6, 10, 39, tzinfo=UTC))
        self.assertEqual(result.summary.total, 9) 
Example #22
Source File: test_scheduler.py    From grimoirelab-kingarthur with GNU General Public License v3.0 5 votes vote down vote up
def test_task_not_rescheduled_age_limit(self):
        """Check if the task is not re-scheduled when age reaches its limit"""

        handler = CompletedJobHandler(self.task_scheduler)

        scheduler_opts = SchedulingTaskConfig(delay=0, max_retries=0, max_age=3)
        task = self.task_scheduler.registry.add('mytask', 'git', 'commit', {},
                                                scheduling_cfg=scheduler_opts)
        # Force the age to its pre-defined limit
        task.age = 3
        self.task_scheduler.registry.update('mytask', task)

        result = JobResult(0, 1, 'mytask', 'git', 'commit')

        summary = Summary()
        summary.fetched = 10
        summary.last_updated_on = datetime.datetime(2010, 1, 1, 1, 0, 0, tzinfo=UTC)
        summary.max_updated_on = datetime.datetime(2014, 2, 12, 6, 10, 39, tzinfo=UTC)
        result.summary = summary

        event = JobEvent(JobEventType.COMPLETED, 0, 'mytask', result)

        handled = handler(event)
        self.assertEqual(handled, True)

        task = self.task_scheduler.registry.get('mytask')
        self.assertEqual(task.status, TaskStatus.COMPLETED) 
Example #23
Source File: test_scheduler.py    From grimoirelab-kingarthur with GNU General Public License v3.0 5 votes vote down vote up
def test_task_rescheduled_with_next_from_date(self):
        """Check if tasks are rescheduled updating next_from_date"""

        handler = CompletedJobHandler(self.task_scheduler)

        _ = self.task_scheduler.registry.add('mytask', 'git', 'commit', {})
        result = JobResult(0, 1, 'mytask', 'git', 'commit')

        summary = Summary()
        summary.fetched = 10
        summary.last_updated_on = datetime.datetime(2010, 1, 1, 1, 0, 0, tzinfo=UTC)
        summary.max_updated_on = datetime.datetime(2014, 2, 12, 6, 10, 39, tzinfo=UTC)
        result.summary = summary

        event = JobEvent(JobEventType.COMPLETED, 0, 'mytask', result)

        handled = handler(event)
        self.assertEqual(handled, True)

        task = self.task_scheduler.registry.get('mytask')
        self.assertEqual(task.status, TaskStatus.SCHEDULED)

        # The field is updated to the max date received
        # within the result
        self.assertEqual(task.backend_args['next_from_date'],
                         datetime.datetime(2014, 2, 12, 6, 10, 39, tzinfo=UTC)) 
Example #24
Source File: test_scheduler.py    From grimoirelab-kingarthur with GNU General Public License v3.0 5 votes vote down vote up
def test_task_rescheduled_with_next_offset(self):
        """Check if tasks are rescheduled updating next_offset"""

        handler = CompletedJobHandler(self.task_scheduler)

        _ = self.task_scheduler.registry.add('mytask', 'git', 'commit', {})
        result = JobResult(0, 1, 'mytask', 'git', 'commit')

        summary = Summary()
        summary.fetched = 10
        summary.last_updated_on = datetime.datetime(2010, 1, 1, 1, 0, 0, tzinfo=UTC)
        summary.max_updated_on = datetime.datetime(2014, 2, 12, 6, 10, 39, tzinfo=UTC)
        summary.last_offset = 800
        summary.max_offset = 1000
        result.summary = summary

        event = JobEvent(JobEventType.COMPLETED, 0, 'mytask', result)

        handled = handler(event)
        self.assertEqual(handled, True)

        task = self.task_scheduler.registry.get('mytask')
        self.assertEqual(task.status, TaskStatus.SCHEDULED)

        # Both fields are updated to the max value received
        # within the result
        self.assertEqual(task.backend_args['next_from_date'],
                         datetime.datetime(2014, 2, 12, 6, 10, 39, tzinfo=UTC))
        self.assertEqual(task.backend_args['next_offset'], 1000) 
Example #25
Source File: test_scheduler.py    From grimoirelab-kingarthur with GNU General Public License v3.0 5 votes vote down vote up
def test_handle_event(self):
        """Check if the event is handled correctly"""

        handler = FailedJobHandler(self.task_scheduler)

        _ = self.registry.add('mytask', 'git', 'commit', {})

        result = JobResult(0, 1, 'mytask', 'git', 'commit')

        summary = Summary()
        summary.fetched = 2
        summary.last_updated_on = datetime.datetime(2010, 1, 1, 1, 0, 0, tzinfo=UTC)
        summary.max_updated_on = datetime.datetime(2010, 1, 1, 1, 0, 0, tzinfo=UTC)
        result.summary = summary

        payload = {
            'error': "Error",
            'result': result
        }
        event = JobEvent(JobEventType.FAILURE, 0, 'mytask', payload)

        # It won't be scheduled because max_retries is not set
        handled = handler(event)
        self.assertEqual(handled, True)

        task = self.registry.get('mytask')
        self.assertEqual(task.status, TaskStatus.FAILED) 
Example #26
Source File: test_scheduler.py    From grimoirelab-kingarthur with GNU General Public License v3.0 5 votes vote down vote up
def test_task_not_rescheduled_not_resume(self):
        """Check if tasks unable to resume are not rescheduled"""

        handler = FailedJobHandler(self.task_scheduler)

        scheduler_opts = SchedulingTaskConfig(delay=0, max_retries=3)
        task = self.registry.add('mytask', 'gerrit', 'review', {},
                                 scheduling_cfg=scheduler_opts)

        result = JobResult(0, 1, 'mytask', 'gerrit', 'review')

        summary = Summary()
        summary.fetched = 2
        summary.last_updated_on = datetime.datetime(2010, 1, 1, 1, 0, 0, tzinfo=UTC)
        summary.max_updated_on = datetime.datetime(2010, 1, 1, 1, 0, 0, tzinfo=UTC)
        result.summary = summary

        payload = {
            'error': "Error",
            'result': result
        }
        event = JobEvent(JobEventType.FAILURE, 0, 'mytask', payload)

        handled = handler(event)
        self.assertEqual(handled, True)

        task = self.registry.get('mytask')
        self.assertEqual(task.status, TaskStatus.FAILED)
        self.assertEqual(task.num_failures, 1) 
Example #27
Source File: test_scheduler.py    From grimoirelab-kingarthur with GNU General Public License v3.0 5 votes vote down vote up
def test_failed_task_not_rescheduled_max_retries(self):
        """Check if the task is not re-scheduled when num failures reaches its limit"""

        handler = FailedJobHandler(self.task_scheduler)

        scheduler_opts = SchedulingTaskConfig(delay=0, max_retries=3)
        task = self.task_scheduler.registry.add('mytask', 'git', 'commit', {}, scheduling_cfg=scheduler_opts)

        # Force to a pre-defined number of failures
        task.num_failures = 2
        self.task_scheduler.registry.update('mytask', task)

        result = JobResult(0, 1, 'mytask', 'git', 'commit')

        summary = Summary()
        summary.fetched = 2
        summary.last_updated_on = datetime.datetime(2010, 1, 1, 1, 0, 0, tzinfo=UTC)
        summary.max_updated_on = datetime.datetime(2010, 1, 1, 1, 0, 0, tzinfo=UTC)
        result.summary = summary

        payload = {
            'error': "Error",
            'result': result
        }
        event = JobEvent(JobEventType.FAILURE, 0, 'mytask', payload)

        handled = handler(event)
        self.assertEqual(handled, True)

        task = self.task_scheduler.registry.get('mytask')
        self.assertEqual(task.status, TaskStatus.FAILED)
        self.assertEqual(task.num_failures, 3) 
Example #28
Source File: test_scheduler.py    From grimoirelab-kingarthur with GNU General Public License v3.0 5 votes vote down vote up
def test_schedule_task_user_queue(self):
        """Task should be added and executed in the user's queue"""

        args = {
            'uri': 'http://example.com/',
            'gitpath': os.path.join(self.dir, 'data/git_log.txt')
        }
        category = 'commit'
        archiving_opts = None
        scheduler_opts = SchedulingTaskConfig(delay=0, max_retries=0,
                                              queue='myqueue')

        registry = TaskRegistry(self.conn)
        task = registry.add('mytask', 'git', category, args,
                            archiving_cfg=archiving_opts,
                            scheduling_cfg=scheduler_opts)

        schlr = Scheduler(self.conn, registry, async_mode=False)
        schlr.schedule_task(task.task_id)

        task = registry.get('mytask')
        self.assertEqual(task.status, TaskStatus.SCHEDULED)
        self.assertEqual(task.age, 0)

        schlr.schedule()

        task = registry.get('mytask')
        self.assertEqual(task.age, 1)

        job = schlr._scheduler._queues['myqueue'].fetch_job(task.jobs[0].id)
        result = job.return_value

        self.assertEqual(result.task_id, task.task_id)
        self.assertEqual(result.job_number, 1)
        self.assertEqual(result.summary.last_uuid, '1375b60d3c23ac9b81da92523e4144abc4489d4c')
        self.assertEqual(result.summary.max_updated_on,
                         datetime.datetime(2014, 2, 12, 6, 10, 39, tzinfo=UTC))
        self.assertEqual(result.summary.total, 9) 
Example #29
Source File: test_icalparser.py    From icalevents with MIT License 5 votes vote down vote up
def test_normalize(self):
        dt = date(year=2016, month=11, day=13)
        norm = icalevents.icalparser.normalize(dt)

        self.assertTrue(type(norm) is datetime, "type is datetime")
        self.assertEqual(2016, norm.year, "year")
        self.assertEqual(11, norm.month, "month")
        self.assertEqual(13, norm.day, "day")
        self.assertEqual(0, norm.hour, "hour")
        self.assertEqual(0, norm.minute, "minute")
        self.assertEqual(0, norm.second, "second")
        self.assertEqual(0, norm.microsecond, "microsecond")
        self.assertEqual(UTC, norm.tzinfo, "timezone")

        dt = datetime(year=2016, month=11, day=13, hour=1, minute=2, second=3)
        norm = icalevents.icalparser.normalize(dt)

        self.assertTrue(type(norm) is datetime, "type is datetime")
        self.assertEqual(2016, norm.year, "year")
        self.assertEqual(11, norm.month, "month")
        self.assertEqual(13, norm.day, "day")
        self.assertEqual(1, norm.hour, "hour")
        self.assertEqual(2, norm.minute, "minute")
        self.assertEqual(3, norm.second, "second")
        self.assertEqual(0, norm.microsecond, "microsecond")
        self.assertEqual(UTC, norm.tzinfo, "timezone")
        
        with self.assertRaises(ValueError, msg="type check effective"):
            icalevents.icalparser.normalize(None) 
Example #30
Source File: test_icalevents.py    From icalevents with MIT License 5 votes vote down vote up
def test_event_created_last_modified(self):
        ical = "test/test_data/created_last_modified.ics"
        start = date(2017, 7, 12)
        end = date(2017, 7, 15)

        events = icalevents.events(url=None, file=ical, start=start, end=end)

        self.assertEqual(events[0].created, datetime(2017, 1, 3, 7, 4, 1, tzinfo=UTC))
        self.assertEqual(events[0].last_modified, datetime(2017, 7, 11, 14, 0, 50, tzinfo=UTC))

        self.assertEqual(events[1].created, datetime(2017, 1, 4, 8, 4, 1, tzinfo=UTC))
        self.assertEqual(events[1].last_modified, datetime(2017, 1, 4, 8, 4, 1, tzinfo=UTC))

        self.assertEqual(events[2].created, None)
        self.assertEqual(events[2].last_modified, None)