COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
initialize_time.cpp File Reference
Include dependency graph for initialize_time.cpp:

Functions

int main (int argc, char *argv[])
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)
35 {
36  Agent *agent = new Agent();
37 
38 
39  FILE *fp = fopen(("/cosmos/nodes/" + agent->nodeName + "/last_date").c_str(), "r");
40  if (fp != nullptr)
41  {
42  calstruc date;
43  int32_t offset = 0;
44  fscanf(fp, "%02d%02d%02d%02d%04d%*c%02d\n", &date.month, &date.dom, &date.hour, &date.minute, &date.year, &date.second);
45  fclose(fp);
46  fp = fopen(("/cosmos/nodes/" + agent->nodeName + "/last_offset").c_str(), "r");
47  if (fp != nullptr)
48  {
49  fscanf(fp, "%d", &offset);
50  }
51  date.second += offset;
52  double delta = cal2mjd(date) - currentmjd();
53  if (delta > 3.5e-4)
54  {
55  delta = set_local_clock(cal2mjd(date));
56  printf("Initialized time from file: Delta %f Offset %d\n", delta, offset);
57  }
58  }
59  else {
60  double rmjd;
61  double epsilon;
62  double offset;
63  int32_t iretn = agent->get_agent_time(rmjd, epsilon, offset, "ntp");
64  if (iretn >= 0)
65  {
66  if (offset > 3.5e-4)
67  {
68  double delta = set_local_clock(currentmjd() + offset);
69  printf("Initialized time from server: Delta %f Offset %f\n", delta, offset*86400.);
70  }
71  }
72  else {
73  printf("Failed to find time server\n");
74  }
75  }
76 
77 }
int32_t dom
Definition: timelib.h:110
Definition: eci2kep_test.cpp:33
static double delta
Definition: agent_exec-2-0.cpp:141
string nodeName
Definition: agentclass.h:367
int iretn
Definition: rw_test.cpp:37
int32_t hour
Definition: timelib.h:112
static double epsilon
Definition: agent_exec-2-0.cpp:140
Definition: timelib.h:106
int32_t get_agent_time(double &agent_time, double &epsilon, double &delta, string agent, string node="any", double wait_sec=2.)
Definition: agentclass.cpp:2711
double set_local_clock(double utc_to)
Definition: timelib.cpp:1626
int32_t month
Definition: timelib.h:109
static Agent * agent
ensure the Agent constructor creates only one instance per process
Definition: agent_001.cpp:45
Definition: agentclass.h:139
double cal2mjd(int32_t year, int32_t month, int32_t day, int32_t hour, int32_t minute, int32_t second, int32_t nsecond)
Calendar representation to Modified Julian Day - full.
Definition: timelib.cpp:294
int32_t minute
Definition: timelib.h:113
double currentmjd(double offset)
Current UTC in Modified Julian Days.
Definition: timelib.cpp:65
int32_t year
Definition: timelib.h:108
FILE * fp
Definition: rw_test.cpp:38
int32_t second
Definition: timelib.h:114
unsigned offset
Definition: inflate.h:102