Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TimeSynchronizationCluster: Wrong epoch time conversion ? (CON-1539) #1269

Closed
caipiblack opened this issue Feb 5, 2025 · 4 comments · Fixed by project-chip/connectedhomeip#37535

Comments

@caipiblack
Copy link

caipiblack commented Feb 5, 2025

Describe the bug
When we set the UTC time of a node running on esp32, the time being set seems to be wrong (or wrongly displayed).

For example:

I (2248785) chip[TS]: Last Known Good Time: 2025-02-05T17:51:33
I (2248788) chip[TS]: New proposed Last Known Good Time: 2025-02-05T17:51:33
I (2248796) chip[TS]: Updating Last Known Good Time to 2025-02-05T17:51:33
I (2248804) chip[DL]: Real time clock set to 1738777893 (0125/01/05 17:51:33 UTC)

The problem is on this line:

I (2248804) chip[DL]: Real time clock set to 1738777893 (0125/01/05 17:51:33 UTC)

Environment
FireBeetle 2 ESP32-C6

Any additional details

  1. Use any simple example and enable CONFIG_ENABLE_SNTP_TIME_SYNC
  2. Configure the example to implement the TimeSynchronization cluster:
// Time sync
#include <app/clusters/time-synchronization-server/time-synchronization-server.h>
#include <app/clusters/time-synchronization-server/DefaultTimeSyncDelegate.h>
#include <app-common/zap-generated/callback.h>

void main(void)
{
....
    endpoint_t *root_node_ep = endpoint::get_first(node);
    ABORT_APP_ON_FAILURE(root_node_ep != nullptr, ESP_LOGE(TAG, "Failed to find root node endpoint"));

    cluster::time_synchronization::config_t time_sync_cfg;
    static chip::app::Clusters::TimeSynchronization::DefaultTimeSyncDelegate time_sync_delegate;
    time_sync_cfg.delegate = &time_sync_delegate;
    cluster_t *time_sync_cluster = cluster::time_synchronization::create(root_node_ep, &time_sync_cfg, CLUSTER_FLAG_SERVER);
    ABORT_APP_ON_FAILURE(time_sync_cluster != nullptr, ESP_LOGE(TAG, "Failed to create time_sync_cluster"));

    cluster::time_synchronization::feature::time_zone::config_t tz_cfg;
    cluster::time_synchronization::feature::time_zone::add(time_sync_cluster, &tz_cfg);
...
}
  1. Commission the device using chiptool
  2. Send a command "SetUTCTime" to the device:
# WARNING: The UTC time is passed as microseconds, but it must be the difference between NOW and 1st January 2000
# Be carefull here, it's not unix timestamp
chip-tool timesynchronization set-utctime 792093093930488 4 1 0

Here a simple tool to get the compatible epoch time:

#!/bin/bash

now=$(date +%s%6N)
epoch_2000=$(date -d "2000-01-01 00:00:00" +%s%6N)
diff=$((now - epoch_2000))
echo "$diff"

It's possible that the problem is due to a wrong conversion: Matter specifications says, they use epoch us based on 2000-01-01, (it's not unix timestamp)

Currently I don't know where the problem is (stack: platform code for esp32, esp-matter ?)

@github-actions github-actions bot changed the title TimeSynchronizationCluster: Wrong epoch time conversion ? TimeSynchronizationCluster: Wrong epoch time conversion ? (CON-1539) Feb 5, 2025
@caipiblack
Copy link
Author

It seems that only the prinf it wrong, because I arrive to read the correct datetime using this:

static void vDisplayDateTime(void) {
    chip::System::Clock::Microseconds64 cTMs;
    chip::System::SystemClock().GetClock_RealTime(cTMs);
    time_t unixEpoch = std::chrono::duration_cast<chip::System::Clock::Seconds32>(cTMs).count();
	char buf[50];
    tm calendarTime{};
    localtime_r(&unixEpoch, &calendarTime);
	printf("The date and time is %s", asctime_r(&calendarTime, buf));
}

So at this point, this issue is very minor.

@pimpalemahesh
Copy link
Contributor

@caipiblack This issue is indeed related to how the log is printed. Please refer to the exact implementation of ChipLogProgress for reference.

Updating the log statement to:
ChipLogProgress(DeviceLayer, "Real time clock set to %lld (%04d/%02d/%02d %02d:%02d:%02d UTC)", static_cast<long long>(tv.tv_sec), calendar.tm_year + 1900, calendar.tm_mon + 1, calendar.tm_mday, calendar.tm_hour, calendar.tm_min, calendar.tm_sec);

will temporarily resolve the issue.

We will fix it soon. Thanks for reporting this!

@pimpalemahesh
Copy link
Contributor

@caipiblack The issue has been fixed, and the fix will be available after the submodule update. Could you please close this issue?

@caipiblack
Copy link
Author

Thanks it’s all good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants