# Duration

Represents 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.

Obtainable from: Duration.days, Duration.hours, Duration.millis, Duration.minutes, Duration.parse, Duration.seconds

- Source: [`core/lib/duration.ts`](https://github.com/aws/aws-cdk/blob/v2.277.0/packages/aws-cdk-lib/core/lib/duration.ts#L13)

## Example

TypeScript:

```ts
import * as lambda from 'aws-cdk-lib/aws-lambda';

declare const fn: lambda.Function;
const fnUrl = fn.addFunctionUrl({ authType: lambda.FunctionUrlAuthType.NONE });

new cloudfront.Distribution(this, 'Distribution', {
  defaultBehavior: {
    origin: new origins.FunctionUrlOrigin(fnUrl, {
      readTimeout: Duration.seconds(30),
      responseCompletionTimeout: Duration.seconds(90),
      keepaliveTimeout: Duration.seconds(45),
    }),
  },
});
```

Python:

```python
import aws_cdk.aws_lambda as lambda_

# fn: lambda.Function

fn_url = fn.add_function_url(auth_type=lambda_.FunctionUrlAuthType.NONE)

cloudfront.Distribution(self, "Distribution",
    default_behavior=cloudfront.BehaviorOptions(
        origin=origins.FunctionUrlOrigin(fn_url,
            read_timeout=Duration.seconds(30),
            response_completion_timeout=Duration.seconds(90),
            keepalive_timeout=Duration.seconds(45)
        )
    )
)
```

Java:

```java
import software.amazon.awscdk.services.lambda.*;

Function fn;

FunctionUrl fnUrl = fn.addFunctionUrl(FunctionUrlOptions.builder().authType(FunctionUrlAuthType.NONE).build());

Distribution.Builder.create(this, "Distribution")
        .defaultBehavior(BehaviorOptions.builder()
                .origin(FunctionUrlOrigin.Builder.create(fnUrl)
                        .readTimeout(Duration.seconds(30))
                        .responseCompletionTimeout(Duration.seconds(90))
                        .keepaliveTimeout(Duration.seconds(45))
                        .build())
                .build())
        .build();
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
using Amazon.CDK.AWS.Lambda;

Function fn;

var fnUrl = fn.AddFunctionUrl(new FunctionUrlOptions { AuthType = FunctionUrlAuthType.NONE });

new Distribution(this, "Distribution", new DistributionProps {
    DefaultBehavior = new BehaviorOptions {
        Origin = new FunctionUrlOrigin(fnUrl, new FunctionUrlOriginProps {
            ReadTimeout = Duration.Seconds(30),
            ResponseCompletionTimeout = Duration.Seconds(90),
            KeepaliveTimeout = Duration.Seconds(45)
        })
    }
});
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
import lambda "github.com/aws/aws-cdk-go/awscdk"

var fn Function

fnUrl := fn.AddFunctionUrl(&FunctionUrlOptions{
	AuthType: lambda.FunctionUrlAuthType_NONE,
})

cloudfront.NewDistribution(this, jsii.String("Distribution"), &DistributionProps{
	DefaultBehavior: &BehaviorOptions{
		Origin: origins.NewFunctionUrlOrigin(fnUrl, &FunctionUrlOriginProps{
			ReadTimeout: awscdk.Duration_Seconds(jsii.Number(30)),
			ResponseCompletionTimeout: awscdk.Duration_*Seconds(jsii.Number(90)),
			KeepaliveTimeout: awscdk.Duration_*Seconds(jsii.Number(45)),
		}),
	},
})
```

## Methods

- `days(amount: number): Duration // chainable` (static) — Create a Duration representing an amount of days.

  **Parameters**

  - `amount` — the amount of Days the `Duration` will represent.

  **Returns:** a new `Duration` representing `amount` Days.

- `formatTokenToNumber(): string` — Returns stringified number of duration.

- `hours(amount: number): Duration // chainable` (static) — Create a Duration representing an amount of hours.

  **Parameters**

  - `amount` — the amount of Hours the `Duration` will represent.

  **Returns:** a new `Duration` representing `amount` Hours.

- `isUnresolved(): boolean` — Checks if duration is a token or a resolvable object.

- `millis(amount: number): Duration // chainable` (static) — Create a Duration representing an amount of milliseconds.

  **Parameters**

  - `amount` — the amount of Milliseconds the `Duration` will represent.

  **Returns:** a new `Duration` representing `amount` ms.

- `minus(rhs: Duration): Duration // chainable` — Substract two Durations together.

- `minutes(amount: number): Duration // chainable` (static) — Create a Duration representing an amount of minutes.

  **Parameters**

  - `amount` — the amount of Minutes the `Duration` will represent.

  **Returns:** a new `Duration` representing `amount` Minutes.

- `parse(duration: string): Duration // chainable` (static) — Parse a period formatted according to the ISO 8601 standard.

  Days are the largest ISO duration supported, i.e.,
  weeks, months, and years are not supported.

  **Parameters**

  - `duration` — an ISO-formatted duration to be parsed.

  **Returns:** the parsed `Duration`.

  ```ts
  // This represents 1 day, 2 hours, 3 minutes, 4 seconds, and 567 milliseconds.
  'P1DT2H3M4.567S'
  ```

- `plus(rhs: Duration): Duration // chainable` — Add two Durations together.

- `seconds(amount: number): Duration // chainable` (static) — Create a Duration representing an amount of seconds.

  **Parameters**

  - `amount` — the amount of Seconds the `Duration` will represent.

  **Returns:** a new `Duration` representing `amount` Seconds.

- `toDays(opts?: TimeConversionOptions): number` — Return the total number of days in this Duration.

  **Returns:** the value of this `Duration` expressed in Days.

- `toHours(opts?: TimeConversionOptions): number` — Return the total number of hours in this Duration.

  **Returns:** the value of this `Duration` expressed in Hours.

- `toHumanString(): string` — Turn this duration into a human-readable string.

- `toIsoString(): string` — Return an ISO 8601 representation of this period.

  **Returns:** a string starting with 'P' describing the period

- `toMilliseconds(opts?: TimeConversionOptions): number` — Return the total number of milliseconds in this Duration.

  **Returns:** the value of this `Duration` expressed in Milliseconds.

- `toMinutes(opts?: TimeConversionOptions): number` — Return the total number of minutes in this Duration.

  **Returns:** the value of this `Duration` expressed in Minutes.

- `toSeconds(opts?: TimeConversionOptions): number` — Return the total number of seconds in this Duration.

  **Returns:** the value of this `Duration` expressed in Seconds.

- `toString(): string` — Returns a string representation of this `Duration`.

  This is never the right function to use when you want to use the `Duration`
  object in a template. Use `toSeconds()`, `toMinutes()`, `toDays()`, etc. instead.

- `unitLabel(): string` — Returns unit of the duration.
