[zephyr] Do not initialize real time clock (#20490)

When there was no support for Last Known UTC Time in the
common code we added a similar but incomplete solution
to the Zephyr platform code that initializes the real time
clock to the firmware build time.

That solution is incorrect as it doesn't allow for updating
the Last Known UTC Time, so we should rather return an error
if no time synchronization is in place.

Signed-off-by: Damian Krolik <damian.krolik@nordicsemi.no>
diff --git a/config/nrfconnect/chip-module/CMakeLists.txt b/config/nrfconnect/chip-module/CMakeLists.txt
index 92d88e9..70edc0c 100644
--- a/config/nrfconnect/chip-module/CMakeLists.txt
+++ b/config/nrfconnect/chip-module/CMakeLists.txt
@@ -236,11 +236,6 @@
     chip_gn_arg_string("chip_mdns" "platform")
 endif()
 
-if (CONFIG_CHIP_FIRMWARE_BUILD_UNIX_TIME)
-    string(TIMESTAMP CHIP_FIRMWARE_BUILD_UNIX_TIME "%s")
-    chip_gn_arg_string("chip_device_config_firmware_build_unix_time" ${CHIP_FIRMWARE_BUILD_UNIX_TIME})
-endif()
-
 if (CHIP_PROJECT_CONFIG)
     chip_gn_arg_string("chip_project_config_include"        ${CHIP_PROJECT_CONFIG})
     chip_gn_arg_string("chip_system_project_config_include" ${CHIP_PROJECT_CONFIG})
diff --git a/config/zephyr/Kconfig b/config/zephyr/Kconfig
index b197f04..b17cb07 100644
--- a/config/zephyr/Kconfig
+++ b/config/zephyr/Kconfig
@@ -229,13 +229,6 @@
 
 endif
 
-config CHIP_FIRMWARE_BUILD_UNIX_TIME
-	bool "Make Unix time of compilation available in source code"
-	default y
-	help
-	  When enabled, the Unix time of the firmware build is exposed to the
-	  source code and used to initialize the last known UTC time.
-
 config APP_LINK_WITH_CHIP
 	bool "Link 'app' with Connected Home over IP"
 	default y
diff --git a/scripts/examples/nrfconnect_example.sh b/scripts/examples/nrfconnect_example.sh
index c737892..9c2ffa6 100755
--- a/scripts/examples/nrfconnect_example.sh
+++ b/scripts/examples/nrfconnect_example.sh
@@ -23,7 +23,7 @@
 shift 2
 
 # Disable debug symbols and firmware build time to increase ccache hit ratio in CI
-COMMON_CI_FLAGS=(-DCONFIG_CHIP_DEBUG_SYMBOLS=n -DCONFIG_CHIP_FIRMWARE_BUILD_UNIX_TIME=n)
+COMMON_CI_FLAGS=(-DCONFIG_CHIP_DEBUG_SYMBOLS=n)
 
 if [[ ! -f "$APP/nrfconnect/CMakeLists.txt" || -z "$BOARD" ]]; then
     echo "Usage: $0 <application> <board>" >&2
diff --git a/src/platform/BUILD.gn b/src/platform/BUILD.gn
index a5bb4df..1ca7fee 100644
--- a/src/platform/BUILD.gn
+++ b/src/platform/BUILD.gn
@@ -41,9 +41,6 @@
     # Time the firmware was built.
     chip_device_config_firmware_build_time = ""
 
-    # Unix time the firmware was built at (seconds since the epoch)
-    chip_device_config_firmware_build_unix_time = ""
-
     # Use OpenThread ftd or mtd library
     chip_device_config_thread_ftd = chip_openthread_ftd
 
@@ -142,9 +139,6 @@
     if (chip_device_config_firmware_build_time != "") {
       defines += [ "CHIP_DEVICE_CONFIG_FIRMWARE_BUILD_TIME=\"${chip_device_config_firmware_build_time}\"" ]
     }
-    if (chip_device_config_firmware_build_unix_time != "") {
-      defines += [ "CHIP_DEVICE_CONFIG_FIRMWARE_BUILD_UNIX_TIME=${chip_device_config_firmware_build_unix_time}" ]
-    }
 
     if (chip_use_transitional_commissionable_data_provider) {
       defines += [ "CHIP_USE_TRANSITIONAL_COMMISSIONABLE_DATA_PROVIDER=1" ]
diff --git a/src/platform/Zephyr/SystemTimeSupport.cpp b/src/platform/Zephyr/SystemTimeSupport.cpp
index 3e847e1..08d8408 100644
--- a/src/platform/Zephyr/SystemTimeSupport.cpp
+++ b/src/platform/Zephyr/SystemTimeSupport.cpp
@@ -42,15 +42,10 @@
 
 namespace {
 
-// Last known UTC time in Unix format.
-#ifdef CHIP_DEVICE_CONFIG_FIRMWARE_BUILD_UNIX_TIME
-Microseconds64 gRealTime = Seconds64(CHIP_DEVICE_CONFIG_FIRMWARE_BUILD_UNIX_TIME);
-#else
-Microseconds64 gRealTime = Seconds64(CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD);
-#endif
+constexpr Microseconds64 kUnknownRealTime = Seconds64::zero();
 
-// Monotonic time of setting the last known UTC time.
-Microseconds64 gRealTimeSetTime;
+// Unix epoch time of boot event
+Microseconds64 gBootRealTime = kUnknownRealTime;
 
 } // namespace
 
@@ -66,7 +61,11 @@
 
 CHIP_ERROR ClockImpl::GetClock_RealTime(Microseconds64 & aCurTime)
 {
-    aCurTime = GetMonotonicMicroseconds64() - gRealTimeSetTime + gRealTime;
+    // The real time can be configured by an application if it has access to a reliable time source.
+    // Otherwise, just return an error so that Matter stack can fallback to Last Known UTC Time.
+    ReturnErrorCodeIf(gBootRealTime == kUnknownRealTime, CHIP_ERROR_INCORRECT_STATE);
+
+    aCurTime = gBootRealTime + GetMonotonicMicroseconds64();
 
     return CHIP_NO_ERROR;
 }
@@ -83,8 +82,7 @@
 
 CHIP_ERROR ClockImpl::SetClock_RealTime(Microseconds64 aNewCurTime)
 {
-    gRealTime        = aNewCurTime;
-    gRealTimeSetTime = GetMonotonicMicroseconds64();
+    gBootRealTime = aNewCurTime - GetMonotonicMicroseconds64();
 
     return CHIP_NO_ERROR;
 }