# Chalk Python SDK Reference: Date and Time Functions
source: https://docs.chalk.ai/api-docs#section-date-and-time-functions

## `current_bucket_end(value, bucket_duration, initial_bucket_start)`
source: https://docs.chalk.ai/api-docs#current_bucket_end

Given a bucket duration, determines the end time of the currently active bucket. The currently active bucket is the bucket in which the feature `value` falls into. `value` should be a datetime feature and will often be set to `_.chalk_now`. If `initial_bucket_start` is passed this overrides the initial bucket startime, which otherwise defaults to unixtime 0: 1970-01-01. This function is typically used in conjunction with materialized aggs.

**Parameters**

- `value`: `Underscore`
  The datetime feature for which the current bucket end time is determined (likely _.chalk_now).
- `bucket_duration`: `str`
  The duration of the bucket (as a chalk duration): "1d", "12h", etc.
- `initial_bucket_start`: `dt.datetime | None` = `None`
  The start time of the bucket.

**Examples**

```python
import chalk.functions as F
@features
class MataggFeature:
   id: str
   current_bucket_end: float = F.current_bucket_end(_.chalk_now, "1d")
```

## `current_bucket_start(value, bucket_duration, initial_bucket_start)`
source: https://docs.chalk.ai/api-docs#current_bucket_start

Given a bucket duration, determines the start time of the currently active bucket. The currently active bucket is the bucket in which the feature `value` falls into. `value` should be a datetime feature and will often be set to `_.chalk_now`. If `initial_bucket_start` is passed this overrides the initial bucket startime, which otherwise defaults to unixtime 0: 1970-01-01. This function is typically used in conjunction with materialized aggs.

**Parameters**

- `value`: `Underscore`
  The datetime feature for which the current bucket end time is determined (likely _.chalk_now).
- `bucket_duration`: `str`
  The duration of the bucket (as a chalk duration): "1d", "12h", etc.
- `initial_bucket_start`: `dt.datetime | None` = `None`
  The start time of the bucket.

**Examples**

```python
import chalk.functions as F
@features
class MataggFeature:
   id: str
   current_bucket_start: float = F.current_bucket_start(_.chalk_now, "1d")
```

## `date_trunc(expr, unit)`
source: https://docs.chalk.ai/api-docs#date_trunc

For example, the following table shows the result of truncating the input datetime `2024-09-17 12:34:56.789` with the various units:

| Unit | Result |
| --- | --- |
| second | 2024-09-17 12:34:56 |
| minute | 2024-09-17 12:34 |
| hour | 2024-09-17 12:00 |
| day | 2024-09-17 |
| week | 2024-09-16 |
| month | 2024-09-01 |
| quarter | 2024-07-01 |
| year | 2024-01-01 |

## `day_of_month(expr)`
source: https://docs.chalk.ai/api-docs#day_of_month

Extract the day of the month from a date.

The supported types for x are date and datetime.

Ranges from 1 to 31 inclusive.

**Parameters**

- `expr`: `Underscore | Any`
  The date to extract the day of the month from.

**Examples**

```python
from datetime import date
import chalk.functions as F
from chalk.features import _, features
@features
class Transaction
   id: str
   date: date
   day: int = F.day_of_month(_.date)
```

## `day_of_week(expr, start_of_week)`
source: https://docs.chalk.ai/api-docs#day_of_week

Returns the ISO day of the week from x. The value ranges from 1 (`start_of_week`, default `MONDAY`) to 7 (`start_of_week + 6`, default `SUNDAY`).

**Parameters**

- `expr`: `Underscore | Any`
  The date to extract the day of the week from.
- `start_of_week`: `DayOfWeek` = `DayOfWeek.MONDAY`
  The day of the week that the week starts on. Defaults to Monday.

**Examples**

```python
from datetime import date
import chalk.functions as F
from chalk.features import _, features
@features
class Transaction
   id: str
   date: date
   day: int = F.day_of_week(_.date)
```

## `day_of_year(expr)`
source: https://docs.chalk.ai/api-docs#day_of_year

Extract the day of the year from a date.

The value ranges from 1 to 366.

**Parameters**

- `expr`: `Underscore | Any`
  The date to extract the day of the year from.

**Examples**

```python
from datetime import date
import chalk.functions as F
from chalk.features import _, features
@features
class Transaction:
   id: str
   date: date
   day: int = F.day_of_year(_.date)
```

## `DayOfWeek`
source: https://docs.chalk.ai/api-docs#DayOfWeek

Enum (bases: `int`, `Enum`)

## `format_datetime(input_dt, format, tz)`
source: https://docs.chalk.ai/api-docs#format_datetime

Format a datetime feature using a Joda-Time format string.` | Symbol | Meaning | Examples | |--------|------------------------------|------------------------------------| | G | era | AD | | C | century of era (>=0) | 20 | | Y | year of era (>=0) | 1996 | | x | weekyear | 1996 | | w | week of weekyear | 27 | | e | day of week | 2 | | E | day of week | Tuesday; Tue | | y | year | 1996 | | D | day of year | 189 | | M | month of year | July; Jul; 07 | | d | day of month | 10 | | a | halfday of day | PM | | K | hour of halfday (0~11) | 0 | | h | clockhour of halfday (1~12) | 12 | | H | hour of day (0~23) | 0 | | k | clockhour of day (1~24) | 24 | | m | minute of hour | 30 | | s | second of minute | 55 | | S | fraction of second | 978 | | z | time zone | Pacific Standard Time; PST | | Z | time zone offset/id | -0800; -08:00; America/Los_Angeles | | ' | escape for text | | | '' | single quote | ' | `

**Parameters**

- `input_dt`: `Any`
  A datetime feature.
- `format`: `str | Any`
  A Joda-Time format string.
- `tz`: `str | Any | None` = `None`
  An optional IANA timezone name (e.g. `"America/New_York"`). When provided, the timestamp is converted to local time before formatting. Can be a feature reference for per-row timezones. When [`None`](https://docs.python.org/3/library/constants.html#None) (the default), formats in UTC.

**Examples**

```python
from datetime import datetime
from chalk.features import _, features
@features
class Iso8601:
  id: int
  dt: datetime
  formatted_datetime: str = F.format_datetime(_.dt, "YYYY-MM-DD HH:mm:ss")
  other_formatted_datetime: str = F.format_datetime(_.dt, "YY-MM-DD HH:mm:ss.S")
  local_time: str = F.format_datetime(_.dt, "yyyy-MM-dd HH:mm:ss", tz="America/New_York")
```

## `from_iso8601_timestamp(expr)`
source: https://docs.chalk.ai/api-docs#from_iso8601_timestamp

Converts an ISO 8601 string into a datetime.

**Parameters**

- `expr`: `Underscore | Any`
  The ISO 8601 string to convert into a datetime.

## `from_unix_milliseconds(expr)`
source: https://docs.chalk.ai/api-docs#from_unix_milliseconds

Converts a Unix timestamp (in milliseconds) to a utc timestamp.

**Parameters**

- `expr`: `Underscore | Any`
  A date represented as the number of millisecods since the Unix timestamp.

**Examples**

```python
from datetime import datetime
import chalk.functions as F
from chalk.features import _, features
@features
class Linux:
   id: int
   unixtime_ms: int = 0
   unixtime: datetime = F.from_unix_milliseconds(_.unix)
```

## `from_unix_seconds(expr)`
source: https://docs.chalk.ai/api-docs#from_unix_seconds

Converts a Unix timestamp (in seconds) to a utc timestamp.

**Parameters**

- `expr`: `Underscore | Any`
  The Unix timestamp to convert.

**Examples**

```python
from datetime import datetime
import chalk.functions as F
from chalk.features import _, features
@features
class Linux:
   id: str
   unixtime_s: int = 0
   unix: int = F.unix_milliseconds(_.date)
```

## `hour_of_day(expr, tz)`
source: https://docs.chalk.ai/api-docs#hour_of_day

Extract the hour of the day from a datetime.

The value ranges from 0 to 23.tz The timezone to use for the hour. By default, UTC is used.

**Parameters**

- `expr`: `Underscore | Any`
  The datetime to extract the hour of the day from.
- `tz`: `dt.timezone | str | None` = `None`

**Examples**

```python
from datetime import datetime
import chalk.functions as F
from chalk.features import _, features
@features
class Transaction:
   id: str
   date: datetime
   hour: int = F.hour_of_day(_.date)
```

## `is_leap_year(expr)`
source: https://docs.chalk.ai/api-docs#is_leap_year

Determine whether the given date is in a leap year.

**Parameters**

- `expr`: `Underscore | Any`
  The date to test.

**Examples**

```python
from datetime import date
import chalk.functions as F
from chalk.features import _, features
@features
class Transaction:
   id: str
   date: date
   leap_year: bool = F.is_leap_year(_.date)
```

## `is_month_end(expr)`
source: https://docs.chalk.ai/api-docs#is_month_end

Determine whether the provided date is the last day of the month.

**Parameters**

- `expr`: `Underscore | Any`
  The date to test.

**Examples**

```python
from datetime import date
import chalk.functions as F
from chalk.features import _, features
@features
class Transaction:
   id: str
   date: date
   month_end: bool= F.is_month_end(_.date)
```

## `is_us_federal_holiday(expr)`
source: https://docs.chalk.ai/api-docs#is_us_federal_holiday

Returns [`True`](https://docs.python.org/3/library/constants.html#True) if the given date or datetime is a US Federal Holiday, else [`False`](https://docs.python.org/3/library/constants.html#False)

**Notes**

Here is a list of the US Federal Holidays:

- New Year's Day (January 1)*
- Martin Luther King's Birthday (3rd Monday in January)
- Washington's Birthday (3rd Monday in February)**
- Memorial Day (last Monday in May)
- Juneteenth National Independence Day (June 19)*
- Independence Day (July 4)*
- Labor Day (1st Monday in September)
- Columbus Day (2nd Monday in October)
- Veterans' Day (November 11)*
- Thanksgiving Day (4th Thursday in November)
- Christmas Day (December 25)*

- If one of these dates would fall on a Saturday/Sunday, the federal holiday will be observed on the proceeding Friday/following Monday, respectively

** More commonly known as "Presidents' Day"

_*_ Every four years, Inaguration Day (January 20) is recognized as a federal holiday exclusively in Washington D.C. Inaguration days are _not_ accounted for in this underscore

**Parameters**

- `expr`: `Underscore | Any`
  The date or datetime to be tested

**Examples**

```python
from datetime import datetime
import chalk.functions as F
from chalk.features import _, features
@features
class Event:
   id: str
   event_date: datetime
   is_us_federal_holiday: F.is_us_federal_holiday(_.event_date)
```

## `last_day_of_month(expr)`
source: https://docs.chalk.ai/api-docs#last_day_of_month

Given a date, returns the last day in that date's month.

**Parameters**

- `expr`: `Underscore | Any`
  The date whose corresponding month (and year) will be used to determine the last day of the month.

**Examples**

```python
from datetime import date
import chalk.functions as F
from chalk.features import _, features
@features
class Transaction:
   id: str
   date: date
   last_day_of_month: int = F.last_day_of_month(_.date)
```

## `month_of_year(expr)`
source: https://docs.chalk.ai/api-docs#month_of_year

Extract the month of the year from a date.

The value ranges from 1 to 12.

**Parameters**

- `expr`: `Underscore | Any`
  The date to extract the month of the year from.

**Examples**

```python
from datetime import date
import chalk.functions as F
from chalk.features import _, features
@features
class Transaction:
   id: str
   date: date
   month: int = F.month_of_year(_.date)
```

## `nth_bucket_end(value, bucket_duration, n, initial_bucket_start)`
source: https://docs.chalk.ai/api-docs#nth_bucket_end

Given a bucket duration, determines the end time of the bucket n positions away from the currently active bucket. The currently active bucket is the bucket in which the feature `value` falls into. `value` should be a datetime feature and will often be set to `_.chalk_now`. If n is positive this will return buckets in the future and if n is negative this will return buckets in the past. For instance, an n of -1 will return the previous buckets end time (which is equivalent to the current buckets start time).

If `initial_bucket_start` is passed this overrides the initial bucket startime, which otherwise defaults to unixtime 0: 1970-01-01. This function is typically used in conjunction with materialized aggs.

**Parameters**

- `value`: `Underscore`
  The datetime feature for which the current bucket end time is determined (likely _.chalk_now).
- `bucket_duration`: `str`
  The duration of the bucket (as a chalk duration): "1d", "12h", etc.
- `n`: `int`
  The nth bucket to calculate the absolute end time for.
- `initial_bucket_start`: `dt.datetime | None` = `None`
  The start time of the bucket.

**Examples**

```python
import chalk.functions as F
@features
class MataggFeature:
   id: str
   next_bucket_end: float = F.current_bucket_end(_.chalk_now, "1d", n=1)
```

## `nth_bucket_start(value, bucket_duration, n, initial_bucket_start)`
source: https://docs.chalk.ai/api-docs#nth_bucket_start

Given a bucket duration, determines the start time of the bucket n positions away from the currently active bucket. The currently active bucket is the bucket in which the feature `value` falls into. `value` should be a datetime feature and will often be set to `_.chalk_now`. If n is positive this will return buckets in the future and if n is negative this will return buckets in the past. For instance, an n of -1 will return the previous buckets start time.

If `initial_bucket_start` is passed this overrides the initial bucket startime, which otherwise defaults to unixtime 0: 1970-01-01. This function is typically used in conjunction with materialized aggs.

**Parameters**

- `value`: `Underscore`
  The datetime feature for which the current bucket end time is determined (likely _.chalk_now).
- `bucket_duration`: `str`
  The duration of the bucket (as a chalk duration): "1d", "12h", etc.
- `n`: `int`
  The nth bucket to calculate the absolute end time for.
- `initial_bucket_start`: `dt.datetime | None` = `None`
  The start time of the bucket.

**Examples**

```python
import chalk.functions as F
import datetime as dt
@features
class MataggFeature:
   id: str
   previous_bucket_start: dt.datetime=  F.nth_bucket_start(_.chalk_now, bucket_duration="1d", n=-1)
```

## `parse_datetime(expr, format)`
source: https://docs.chalk.ai/api-docs#parse_datetime

Converts an ISO 8601 string into a datetime.

**Parameters**

- `expr`: `Underscore | Any`
  The ISO 8601 string to convert into a datetime.
- `format`: `str | None` = `None`

## `quarter(expr)`
source: https://docs.chalk.ai/api-docs#quarter

Extract the quarter from the date.

The value ranges from 1 to 4.

**Parameters**

- `expr`: `Underscore | Any`
  The date to extract the quarter from.

**Examples**

```python
from datetime import date
import chalk.functions as F
from chalk.features import _, features
@features
class Transaction:
   id: str
   date: date
   quarter: int = F.quarter(_.date)
```

## `to_iso8601(expr)`
source: https://docs.chalk.ai/api-docs#to_iso8601

Formats input datetime as an ISO 8601 string

**Parameters**

- `expr`: `Underscore | Any`
  The datetime to convert into ISO 8601 string.

**Examples**

```python
from datetime import datetime
import chalk.functions as F
from chalk.features import _, features
@features
class IsoStr:
   id: str
   iso_str: str = F.to_iso8601(_.iso_date)
   iso_date: datetime
```

## `total_seconds(delta)`
source: https://docs.chalk.ai/api-docs#total_seconds

Compute the total number of seconds covered in a duration.

**Parameters**

- `delta`: `Underscore`
  The duration to convert to seconds.

**Examples**

```python
from datetime import date
from chalk.functions as F
from chalk.features import _, features
@features
class Transaction:
   id: str
   signup: date
   last_login: date
   signup_to_last_login_days: float = F.total_seconds(_.last_login - _.signup) / (60 * 60 * 24)
```

## `unix_milliseconds(expr)`
source: https://docs.chalk.ai/api-docs#unix_milliseconds

Extract the number of milliseconds since the Unix epoch. Returned as a float.

**Parameters**

- `expr`: `Underscore | Any`
  The datetime to extract the number of milliseconds since the Unix epoch from.

**Examples**

```python
from datetime import datetime
import chalk.functions as F
from chalk.features import _, features
@features
class Transaction:
   id: str
   date: datetime
   unix_milliseconds: float = F.unix_milliseconds(_.date)
```

## `unix_seconds(expr)`
source: https://docs.chalk.ai/api-docs#unix_seconds

Extract the number of seconds since the Unix epoch. Returned as a float.

**Parameters**

- `expr`: `Underscore | Any`
  The datetime to extract the number of seconds since the Unix epoch from.

**Examples**

```python
from datetime import datetime
import chalk.functions as F
from chalk.features import _, features
@features
class Transaction:
   id: str
   date: datetime
   unix_seconds: float = F.unix_seconds(_.date)
```

## `week_of_year(expr)`
source: https://docs.chalk.ai/api-docs#week_of_year

Extract the week of the year from a date.

The value ranges from 1 to 53.

**Parameters**

- `expr`: `Underscore | Any`
  The date to extract the week of the year from.

**Examples**

```python
from datetime import date
import chalk.functions as F
from chalk.features import _, features
@features
class Transaction:
   id: str
   date: date
   week: int = F.week_of_year(_.date)
```

## `year(expr)`
source: https://docs.chalk.ai/api-docs#year

Extract the year from the date.

**Parameters**

- `expr`: `Underscore | Any`
  The date to extract the year from.

**Examples**

```python
from datetime import date
import chalk.functions as F
from chalk.features import _, features
@features
class Transaction:
   id: str
   date: date
   year: int = F.year(_.date)
```
