LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
Util.cpp
Go to the documentation of this file.
2 
3 #include <libpq-fe.h>
4 
5 #include <iostream>
6 
7 namespace nutools {
8 
9  namespace dbi {
10 
11  //************************************************************
12 
13  bool Util::CheckConnection(std::string dbname, std::string host,
14  std::string user, std::string port)
15  {
16  bool retVal = true;
17 
18  std::string cmd = "dbname = " + dbname + " host = " + host;
19  if (user != "")
20  cmd += " user = " + user;
21  if (port != "")
22  cmd += " port = " + port;
23 
24  PGconn* conn = PQconnectdb(cmd.c_str());
25  if (conn) {
26  if (PQstatus(conn) != CONNECTION_OK) {
27  std::cerr << "Connection to " << host << ":" << dbname << " failed: "
28  << PQerrorMessage(conn) << std::endl;
29  retVal = false;
30  }
31  else
32  PQfinish(conn);
33  }
34  else
35  retVal = false;
36 
37  return retVal;
38 
39  }
40 
41  //************************************************************
43  {
44  time_t rawtime;
45  struct tm* timeinfo;
46  char buffer[256];
47  time(&rawtime);
48  timeinfo = gmtime(&rawtime);
49  strftime(buffer,256,"%Y-%m-%d %H:%M:%S",timeinfo);
50 
51  return std::string(buffer);
52 
53  }
54 
55  //************************************************************
57  {
58  time_t rawtime;
59  struct tm* timeinfo;
60  char buffer[256];
61  time(&rawtime);
62  timeinfo = gmtime(&rawtime);
63  strftime(buffer,256,"%Y-%m-%d",timeinfo);
64 
65  return std::string(buffer);
66 
67  }
68 
69  //************************************************************
71  {
72  return "1900-01-01 00:00:00";
73  }
74  //************************************************************
76  {
77  return "2099-12-31 23:59:59";
78  }
79 
80  //************************************************************
81  bool Util::TimeAsStringToTime_t(std::string ts, time_t& t)
82  {
83  bool isOk = false;
84  struct tm ta;
85  if ( (sscanf(ts.c_str(),"%d/%d/%d %d:%d:%d",
86  &ta.tm_year,&ta.tm_mon,&ta.tm_mday,
87  &ta.tm_hour,&ta.tm_min,&ta.tm_sec) == 6))
88  isOk = true;
89  if (!isOk)
90  if ( (sscanf(ts.c_str(),"%d-%d-%d %d:%d:%d",
91  &ta.tm_year,&ta.tm_mon,&ta.tm_mday,
92  &ta.tm_hour,&ta.tm_min,&ta.tm_sec) == 6))
93  isOk = true;
94 
95  if (isOk) {
96  ta.tm_year -= 1900;
97  ta.tm_mon -= 1;
98  t = timegm(&ta);
99  }
100 
101  return isOk;
102 
103  }
104 
105  //************************************************************
106  bool Util::DateAsStringToTime_t(std::string ts, time_t& t)
107  {
108  bool isOk = false;
109  struct tm ta;
110  if ( (sscanf(ts.c_str(),"%d/%d/%d",
111  &ta.tm_year,&ta.tm_mon,&ta.tm_mday) == 3))
112  isOk = true;
113  if (!isOk)
114  if ( (sscanf(ts.c_str(),"%d-%d-%d",
115  &ta.tm_year,&ta.tm_mon,&ta.tm_mday) == 3))
116  isOk = true;
117 
118  if (isOk) {
119  ta.tm_year -= 1900;
120  ta.tm_mon -= 1;
121  ta.tm_hour = 0;
122  ta.tm_min = 0;
123  ta.tm_sec = 0;
124  t = timegm(&ta);
125  }
126 
127  return isOk;
128 
129  }
130 
131  //************************************************************
133  {
134  char* tStr = getenv("_CONDOR_SCRATCH_DIR");
135  return (tStr != 0);
136  }
137 
138  }
139 }
static bool RunningOnGrid()
Definition: Util.cpp:132
static std::string GetCurrentTimeAsString()
Definition: Util.cpp:42
Simple service to provide a RunHistory configured to the right run.
Definition: Column.cpp:14
static std::string GetFarFutureTimeAsString()
Definition: Util.cpp:75
static bool CheckConnection(std::string dbname, std::string server, std::string user="", std::string port="")
Definition: Util.cpp:13
pg_conn PGconn
Definition: Table.h:17
static std::string GetFarPastTimeAsString()
Definition: Util.cpp:70
static bool TimeAsStringToTime_t(std::string ts, time_t &t)
Definition: Util.cpp:81
static bool DateAsStringToTime_t(std::string date, time_t &t)
Definition: Util.cpp:106
static std::string GetCurrentDateAsString()
Definition: Util.cpp:56