offline

Example of the component embedblocks/time-service v1.0.3
# Example: offline

Demonstrates `time_service` in an environment with **no network stack**.

Time is set manually via `time_service_set_time()` — simulating a value received from an external RTC module, read from NVS after a reboot, or supplied by user configuration. The component runs entirely on this manually provided time and prints a human-readable timestamp every two seconds.

---

## What This Example Shows

- Calling `time_service_init()` safely without any network initialization
- Running on build timestamp immediately after boot — time is always available
- Applying a manual correction via `time_service_set_time()` and observing the jump in logs
- Using `time_service_now_str()` to produce formatted log timestamps with a fixed UTC offset

---

## When to Use This Pattern

- No WiFi or Ethernet in your application
- WiFi available but time accuracy is not critical — build time is sufficient
- An external RTC module (DS3231, PCF8563, etc.) is the time source
- Time was saved to NVS before the last reboot and is being restored

---

## Expected Output

```
I (312)  offline_example: Initialized, origin=2
I (322)  offline_example: Running on build time for 10 seconds...
I (332)  offline_example: [build time] 2026-04-11 13:10:05 +0500
I (2342) offline_example: [build time] 2026-04-11 13:10:07 +0500
I (4342) offline_example: [build time] 2026-04-11 13:10:09 +0500
I (6342) offline_example: [build time] 2026-04-11 13:10:11 +0500
I (8342) offline_example: [build time] 2026-04-11 13:10:13 +0500
I (10342) offline_example: Manual time set — jumped -XXXXX sec (epoch: XXXXXXXXXX → 1776087799)
I (10352) offline_example: Running on corrected time...
I (10362) offline_example: [corrected]  2026-04-14 18:03:19 +0500
I (12362) offline_example: [corrected]  2026-04-14 18:03:21 +0500
```

The `[build time]` lines show the component running immediately after boot. The jump log shows the exact correction applied. The `[corrected]` lines show the component running on the manually set time.

---

## Build and Flash

```bash
cd examples/offline
idf.py build flash monitor
```

---

## Configuration

The timezone offset is defined at the top of `main.c`:

```c
#define TIMEZONE_OFFSET_SEC  (5 * 3600)  // UTC+5 Pakistan Standard Time
```

Change this to match your local offset in seconds. Examples:

| Timezone | Offset (sec) |
|---|---|
| UTC | `0` |
| PKT (UTC+5) | `18000` |
| IST (UTC+5:30) | `19800` |
| EST (UTC-5) | `-18000` |

To use a real time source instead of the hardcoded epoch, replace:

```c
time_t manual_time = 1776087799;
```

with your actual source, for example reading from an RTC module over I2C or restoring from NVS.

To create a project from this example, run:

idf.py create-project-from-example "embedblocks/time-service=1.0.3:offline"

or download archive (~3.01 KB)