How to use the pendulum.duration function in pendulum

To help you get started, we’ve selected a few pendulum examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sdispater / pendulum / tests / duration / test_total_methods.py View on Github external
def test_in_seconds():
    it = pendulum.duration(seconds=72, microseconds=123456)
    assert it.total_seconds() == 72.123456
github AuHau / toggl-cli / tests / integration / test_time_entries.py View on Github external
def test_add_tags(self, cmd, fake, config):
        start = pendulum.instance(fake.past_datetime(start_date='-9d'))
        end = start + pendulum.duration(hours=2)
        result = cmd('add \'{}\' \'{}\' \'{}\' --tags \'some tag,another tag\''.format(start.format('MMM D HH:mm:ss'),
                                                                                       end.format('MMM D HH:mm:ss'),
                                                                                       fake.sentence()))
        assert result.obj.exit_code == 0

        entry = TimeEntry.objects.get(result.created_id(), config=config)  # type: TimeEntry
        assert len(entry.tags) == 2
        assert 'some tag' in entry.tags
        assert 'another tag' in entry.tags
github sdispater / pendulum / tests / datetime / test_sub.py View on Github external
def test_subtract_duration():
    duration = pendulum.duration(
        years=2, months=3, days=6, seconds=16, microseconds=654321
    )
    d = pendulum.datetime(2015, 3, 14, 3, 12, 15, 777777)

    d = d - duration
    assert 2012 == d.year
    assert 12 == d.month
    assert 8 == d.day
    assert 3 == d.hour
    assert 11 == d.minute
    assert 59 == d.second
    assert 123456 == d.microsecond
github sdispater / pendulum / tests / duration / test_in_words.py View on Github external
def test_in_french():
    pi = pendulum.duration(
        years=2, months=3, days=1177, seconds=7284, microseconds=1000000
    )

    expected = "2 ans 3 mois 168 semaines 1 jour 2 heures 1 minute 25 secondes"
    assert pi.in_words(locale="fr") == expected
github PrefectHQ / prefect / tests / test_schedules.py View on Github external
def test_next_n_with_different_timezones(self):
        east = schedules.CronSchedule(
            "0 9 * * 1-5", start_date=pendulum.parse("2019-03-14", tz="US/Eastern")
        )
        west = schedules.CronSchedule(
            "30 6 * * 1-5", start_date=pendulum.parse("2019-03-14", tz="US/Pacific")
        )
        main = schedules.UnionSchedule([east, west])

        ## have to do some additional work so this test passes
        ## regardless of what time it is
        now = pendulum.now("US/Pacific")
        after = east.next(1, after=now)[0] + pendulum.duration(minutes=-1)

        expected = [
            east.next(1, after=after)[0],
            west.next(1, after=after)[0],
            east.next(2, after=after)[1],
            west.next(2, after=after)[1],
        ]

        assert main.next(4, after=after) == expected
github sdispater / pendulum / tests / duration / test_add_sub.py View on Github external
def test_add_interval():
    p1 = pendulum.duration(days=23, seconds=32)
    p2 = pendulum.duration(days=12, seconds=30)

    p = p1 + p2
    assert_duration(p, 0, 0, 5, 0, 0, 1, 2)
github sdispater / pendulum / pendulum / parser.py View on Github external
days=duration.remaining_days,
                    hours=duration.hours,
                    minutes=duration.minutes,
                    seconds=duration.remaining_seconds,
                    microseconds=duration.microseconds,
                ),
                dt,
            )

        return pendulum.period(
            pendulum.instance(parsed.start, tz=options.get("tz", UTC)),
            pendulum.instance(parsed.end, tz=options.get("tz", UTC)),
        )

    if CDuration and isinstance(parsed, CDuration):
        return pendulum.duration(
            years=parsed.years,
            months=parsed.months,
            weeks=parsed.weeks,
            days=parsed.days,
            hours=parsed.hours,
            minutes=parsed.minutes,
            seconds=parsed.seconds,
            microseconds=parsed.microseconds,
        )

    return parsed
github AuHau / toggl-cli / toggl / cli.py View on Github external
def convert(self, value, param, ctx):
        matches = re.findall(self.SYNTAX_REGEX, value, re.IGNORECASE)

        # If nothing matches ==> unknown syntax ==> fallback to DateTime parsing
        if not matches:
            return super().convert(value, param, ctx)

        base = pendulum.duration()
        for match in matches:
            unit = self.MAPPING[match[1].lower()]

            base += pendulum.duration(**{unit: int(match[0])})

        return base
github AuHau / toggl-cli / toggl / api / models.py View on Github external
def set_duration(name, instance, value, init=False):  # type: (str, base.Entity, typing.Optional[int], bool) -> typing.Optional[bool]
    """
    Setter for Duration Property field.
    """
    if init is True:
        instance.is_running = False

    if value is None:
        return

    if value > 0:
        instance.is_running = False
        instance.stop = instance.start + pendulum.duration(seconds=value)
    else:
        instance.is_running = True
        instance.stop = None

    return True  # Any change will result in updated instance's state.