time.c 825 B

123456789101112131415161718192021222324252627282930313233
  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. strftime(tb1, sizeof tb1, "%a %Y-%m-%d %H:%M:%S", tm);
  9. strftime(tb2, sizeof tb2, "%z (%Z)", tm);
  10. printf("%s%s.%03lu %s\n", msg, tb1, tv->tv_usec/1000UL, tb2);
  11. }
  12. volatile bool time_net_sync_status;
  13. void time_net_sync(const struct timeval *tv)
  14. {
  15. bool synced = !!tv;
  16. if (synced != time_net_sync_status) {
  17. time_net_sync_status = synced;
  18. setvar_bool(status_net_sntp_sync, synced);
  19. if (!synced) {
  20. printf("[SNTP] Time synchronization lost\n");
  21. } else {
  22. print_time("[SNTP] Time synchronized: ", tv);
  23. }
  24. }
  25. if (synced)
  26. fpga_timesync_trigger(); /* Push time downstream to FPGA/RTC */
  27. }