# Convert Timestamps using Rust

> For the complete documentation index, see [llms.txt](https://docs.redpanda.com/llms.txt). Component-specific: [labs-full.txt](https://docs.redpanda.com/labs-full.txt)

---
title: Convert Timestamps using Rust
latest-operator-version: v26.1.4
latest-console-tag: v3.7.3
latest-connect-version: 4.93.0
latest-redpanda-tag: v26.1.9
docname: ts-converter-rust
page-component-name: labs
page-version: master
page-component-version: master
page-component-title: Labs
page-relative-src-path: ts-converter-rust.adoc
page-edit-url: https://github.com/redpanda-data/redpanda-labs/edit/main/docs/modules/data-transforms/pages/ts-converter-rust.adoc
description: Convert timestamps from various forms, such as epochs to strings.
page-git-created-date: "2025-05-06"
page-git-modified-date: "2025-05-06"
---

<!-- Source: https://docs.redpanda.com/labs/data-transforms/ts-converter-rust.md -->

This lab uses data transforms with the Redpanda Schema Registry to convert keys or values with timestamps across various formats. Written in Rust, the example shows how to transform numeric epoch values in milliseconds to string-based formats and vice versa.

## [](#prerequisites)Prerequisites

You must have the following:

-   At least version 1.75 of [Rust](https://rustup.rs/) installed on your host machine.

-   The Wasm target for Rust installed. To install this target, run the following:

    ```bash
    rustup target add wasm32-wasip1
    ```

-   [Install `rpk`](https://docs.redpanda.com/current/get-started/rpk-install/) on your host machine.

-   [Docker and Docker Compose](https://docs.docker.com/compose/install/) installed on your host machine.


## [](#run-the-lab)Run the lab

1.  Clone this repository:

    ```bash
    git clone https://github.com/redpanda-data/redpanda-labs.git
    ```

2.  Change into the `data-transforms/ts-converter/` directory:

    ```bash
    cd redpanda-labs/data-transforms/rust/ts-converter
    ```

3.  Set the `REDPANDA_VERSION` environment variable to at least version 24.1.2. For all available versions, see the [GitHub releases](https://github.com/redpanda-data/redpanda/releases).

    For example:

    ```bash
    export REDPANDA_VERSION=v26.1.9
    ```

4.  Set the `REDPANDA_CONSOLE_VERSION` environment variable to the version of Redpanda Console that you want to run. For all available versions, see the [GitHub releases](https://github.com/redpanda-data/redpanda/releases).

    For example:

    ```bash
    export REDPANDA_CONSOLE_VERSION=v3.7.3
    ```

5.  Start Redpanda in Docker by running the following command:

    ```bash
    docker compose up -d --wait
    ```

6.  Set up your rpk profile:

    ```bash
    rpk profile create ts-converter --from-profile profile.yml
    ```

7.  Create the required input topic:

    ```bash
    rpk topic create src sink
    ```

8.  Create a source schema:

    ```bash
    echo '{"type": "long", "name": "epoch"}' | tee epoch.avsc
    rpk registry schema create src-value --schema epoch.avsc
    ```

9.  Deploy the transforms function:

    ```bash
    rpk transform build
    rpk transform deploy --file ts-converter.wasm -i src -o sink --var "TIMESTAMP_TARGET_TYPE=string[%+]"
    ```

    This example accepts the following environment variables:

    -   `TIMESTAMP_TARGET_TYPE` (**required**): The output conversion time. Must be one of:

        -   `string[<pattern>]`: where `<pattern>` is a valid [chrono format](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) string

        -   `unix[<precision>]`: where `<precision>` is either `seconds`, `milliseconds`, `microseconds`, or `nanoseconds`

        -   `date`: which truncates to just the date portion of the timestamp

        -   `time`: which truncates to just the 24-hour time of the timestamp


    -   `TIMESTAMP_MODE`: one of `key`, `value`, or a fielded version like `value[my-field]`

    -   `TIMESTAMP_STRING_FORMAT`: if your input data is a string, provide the chrono format for parsing


10.  Produce an example epoch in milliseconds using `rpk topic produce`:

     ```bash
     echo "1704310686988" | rpk topic produce src --schema-id topic
     ```

11.  Consume the sink topic with `rpk` and using new schema to see the string-based timestamp:

     ```bash
     rpk topic consume sink --use-schema-registry=value -n 1 -o -1
     ```

     {
       "topic": "sink",
       "value": "\\"2024-01-03T19:38:06.988+00:00\\"",
       "timestamp": 1715890281087,
       "partition": 0,
       "offset": 0
     }


You can also see this in [Redpanda Console](http://localhost:8080/topics/sink?p=-1&s=50&o=-1#messages).

## [](#clean-up)Clean up

To shut down and delete the containers along with all your cluster data:

```bash
docker compose down -v
```