time.c 843 B

12345678910111213141516171819202122232425262728293031323334
  1. #include "common.h"
  2. #include "config.h"
  3. #include "fpga.h"
  4. void print_time(const char *msg, const struct timeval *tv)
  5. {
  6. const struct tm *tm = localtime(&tv->tv_sec);
  7. char tb1[48], tb2[16];
  8. size_t len;
  9. strftime(tb1, sizeof tb1, "%a %Y-%m-%d %H:%M:%S", tm);
  10. strftime(tb2, sizeof tb2, "%z (%Z)", tm);
  11. printf("%s%s.%03lu %s\n", msg, tb1, tv->tv_usec/1000UL, tb2);
  12. }
  13. volatile bool time_net_sync_status;
  14. void time_net_sync(const struct timeval *tv)
  15. {
  16. bool synced = !!tv;
  17. if (synced != time_net_sync_status) {
  18. time_net_sync_status = synced;
  19. setenv_bool("status.net.sntp.sync", synced);
  20. if (!synced) {
  21. printf("[SNTP] Time synchronization lost\n");
  22. } else {
  23. print_time("[SNTP] Time synchronized: ", tv);
  24. }
  25. }
  26. if (synced)
  27. fpga_timesync_trigger(); /* Push time downstream to FPGA/RTC */
  28. }