[Canonical page](<https://docs.dhtmlx.com/planning-engine/rust/api/core/data/time-unit/>)
[Full documentation index](<https://docs.dhtmlx.com/planning-engine/llms.txt>)


# TimeUnit

The unit in which a duration or lag value is counted. It's the type of [`SchedulingConfig::duration_unit`](<https://docs.dhtmlx.com/planning-engine/rust/api/core/data/scheduling-config.md#duration_unit>) (default `TimeUnit::Day`), [`TaskData::duration_unit`](<https://docs.dhtmlx.com/planning-engine/rust/api/core/data/task-data.md#duration_unit>), and the `lag_unit` field of [`LinkData`](<https://docs.dhtmlx.com/planning-engine/rust/api/core/data/link-data.md>), [`LinkAdd`](<https://docs.dhtmlx.com/planning-engine/rust/api/core/changes/link-add.md>) and [`LinkUpdate`](<https://docs.dhtmlx.com/planning-engine/rust/api/core/changes/link-update.md>). Convert a string with `TimeUnit::parse`.

## Declaration

```rust
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TimeUnit {
    Minute,
    Hour,
    Day,
}
```

With the `serde` feature, `TimeUnit` implements `Serialize` and `Deserialize`, using the lowercase names `"minute"`, `"hour"` and `"day"`.

## Variants

| Variant | Description |
|---|---|
| `Minute` | Values are counted in minutes. |
| `Hour` | Values are counted in hours. |
| `Day` | Values are counted in days. |

The `week`, `month` and `year` units are reserved and not supported yet.

## Associated functions

### parse

```rust
pub fn parse(s: &str) -> Result<TimeUnit, String>
```

Converts a lowercase unit name to a `TimeUnit`.

**Parameters**

| Name | Type | Description |
|---|---|---|
| `s` | `&str` | `"minute"`, `"hour"` or `"day"`. Matching is case-sensitive. |

**Returns** - the matching variant.

**Errors** - `Err(String)` with a message when `s` is `"week"`, `"month"` or `"year"` (reserved, not supported yet) or any other unknown name.

## Methods

### as_str

```rust
pub fn as_str(&self) -> &'static str
```

Returns the lowercase name of the unit: `"minute"`, `"hour"` or `"day"`. `TimeUnit::parse` accepts this name back.

## Example

```rust
use plen_core::{SchedulingConfig, TimeUnit};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let unit = TimeUnit::parse("hour")?;
    assert_eq!(unit.as_str(), "hour");
    assert!(TimeUnit::parse("week").is_err());

    let config = SchedulingConfig {
        duration_unit: unit,
        ..SchedulingConfig::default()
    };
    assert_eq!(config.duration_unit, TimeUnit::Hour);
    Ok(())
}
```

## Related

- [`SchedulingConfig`](<https://docs.dhtmlx.com/planning-engine/rust/api/core/data/scheduling-config.md>) - sets the project-wide unit
- [`TaskData`](<https://docs.dhtmlx.com/planning-engine/rust/api/core/data/task-data.md>) - overrides the unit for one task
- [`Basis`](<https://docs.dhtmlx.com/planning-engine/rust/api/core/data/basis.md#for_unit>) - the load bucket derived from a unit
- [Project data](<https://docs.dhtmlx.com/planning-engine/rust/scheduling/project-data.md>) - choosing a duration unit for a snapshot
- [Working time and calendars](<https://docs.dhtmlx.com/planning-engine/rust/scheduling/working-time-calendars.md>) - how durations in each unit map onto working time
