Duration
- class aws_cdk.core.Duration(*args: Any, **kwargs)
Bases:
objectRepresents a length of time.
The amount can be specified either as a literal value (e.g:
10) which cannot be negative, or as an unresolved number token.When the amount is passed as a token, unit conversion is not possible.
- ExampleMetadata:
infused
Example:
import aws_cdk.aws_lambda as lambda_ fn = lambda_.Function(self, "MyFunc", runtime=lambda_.Runtime.NODEJS_14_X, handler="index.handler", code=lambda_.Code.from_inline("exports.handler = handler.toString()") ) rule = events.Rule(self, "rule", event_pattern=events.EventPattern( source=["aws.ec2"] ) ) queue = sqs.Queue(self, "Queue") rule.add_target(targets.LambdaFunction(fn, dead_letter_queue=queue, # Optional: add a dead letter queue max_event_age=cdk.Duration.hours(2), # Optional: set the maxEventAge retry policy retry_attempts=2 ))
Methods
- format_token_to_number()
Returns stringified number of duration.
- Return type:
str
- is_unresolved()
Checks if duration is a token or a resolvable object.
- Return type:
bool
- to_days(*, integral=None)
Return the total number of days in this Duration.
- Parameters:
integral (
Optional[bool]) – Iftrue, conversions into a larger time unit (e.g.SecondstoMinutes) will fail if the result is not an integer. Default: true- Return type:
Union[int,float]- Returns:
the value of this
Durationexpressed in Days.
- to_hours(*, integral=None)
Return the total number of hours in this Duration.
- Parameters:
integral (
Optional[bool]) – Iftrue, conversions into a larger time unit (e.g.SecondstoMinutes) will fail if the result is not an integer. Default: true- Return type:
Union[int,float]- Returns:
the value of this
Durationexpressed in Hours.
- to_human_string()
Turn this duration into a human-readable string.
- Return type:
str
- to_iso_string()
Return an ISO 8601 representation of this period.
- Return type:
str- Returns:
a string starting with ‘P’ describing the period
- See:
- to_milliseconds(*, integral=None)
Return the total number of milliseconds in this Duration.
- Parameters:
integral (
Optional[bool]) – Iftrue, conversions into a larger time unit (e.g.SecondstoMinutes) will fail if the result is not an integer. Default: true- Return type:
Union[int,float]- Returns:
the value of this
Durationexpressed in Milliseconds.
- to_minutes(*, integral=None)
Return the total number of minutes in this Duration.
- Parameters:
integral (
Optional[bool]) – Iftrue, conversions into a larger time unit (e.g.SecondstoMinutes) will fail if the result is not an integer. Default: true- Return type:
Union[int,float]- Returns:
the value of this
Durationexpressed in Minutes.
- to_seconds(*, integral=None)
Return the total number of seconds in this Duration.
- Parameters:
integral (
Optional[bool]) – Iftrue, conversions into a larger time unit (e.g.SecondstoMinutes) will fail if the result is not an integer. Default: true- Return type:
Union[int,float]- Returns:
the value of this
Durationexpressed in Seconds.
- to_string()
Returns a string representation of this
Duration.This is is never the right function to use when you want to use the
Durationobject in a template. UsetoSeconds(),toMinutes(),toDays(), etc. instead.- Return type:
str
- unit_label()
Returns unit of the duration.
- Return type:
str
Static Methods
- classmethod days(amount)
Create a Duration representing an amount of days.
- Parameters:
amount (
Union[int,float]) – the amount of Days theDurationwill represent.- Return type:
- Returns:
a new
DurationrepresentingamountDays.
- classmethod hours(amount)
Create a Duration representing an amount of hours.
- Parameters:
amount (
Union[int,float]) – the amount of Hours theDurationwill represent.- Return type:
- Returns:
a new
DurationrepresentingamountHours.
- classmethod millis(amount)
Create a Duration representing an amount of milliseconds.
- Parameters:
amount (
Union[int,float]) – the amount of Milliseconds theDurationwill represent.- Return type:
- Returns:
a new
Durationrepresentingamountms.
- classmethod minutes(amount)
Create a Duration representing an amount of minutes.
- Parameters:
amount (
Union[int,float]) – the amount of Minutes theDurationwill represent.- Return type:
- Returns:
a new
DurationrepresentingamountMinutes.
- classmethod parse(duration)
Parse a period formatted according to the ISO 8601 standard.
- Parameters:
duration (
str) – an ISO-formtted duration to be parsed.- Return type:
- Returns:
the parsed
Duration.- See: