Esempio n. 1
0
void timestamp_format_per_credit (const timestamp_t *t)
{
	timestamp_t per_credit;
	timestamp_copy (&per_credit, t);
	timestamp_divide (&per_credit, system_audits.total_plays);
	timestamp_format (&per_credit);
}
Esempio n. 2
0
void timestamp_format_per_ball (const timestamp_t *t)
{
	timestamp_t per_ball;
	timestamp_copy (&per_ball, t);
	timestamp_divide (&per_ball, system_audits.balls_played);
	timestamp_format (&per_ball);
}
Esempio n. 3
0
        /**
         * Return the timestamp as string in ISO date/time
         * ("yyyy-mm-ddThh:mm:ssZ") format. If the timestamp is invalid, an
         * empty string will be returned.
         */
        std::string to_iso() const {
            std::string s;

            if (m_timestamp != 0) {
                struct tm tm;
                time_t sse = seconds_since_epoch();
#ifndef NDEBUG
                auto result =
#endif
#ifndef _MSC_VER
                              gmtime_r(&sse, &tm);
                assert(result != nullptr);
#else
                              gmtime_s(&tm, &sse);
                assert(result == 0);
#endif

                s.resize(timestamp_length);
                /* This const_cast is ok, because we know we have enough space
                in the string for the format we are using (well at least until
                the year will have 5 digits). And by setting the size
                afterwards from the result of strftime we make sure thats set
                right, too. */
                s.resize(strftime(const_cast<char*>(s.c_str()), timestamp_length, timestamp_format(), &tm));
            }

            return s;
        }
Esempio n. 4
0
        /**
         * Construct timestamp from ISO date/time string.
         * Throws std::invalid_argument, if the timestamp can not be parsed.
         */
        explicit Timestamp(const char* timestamp) {
#ifndef _WIN32
            struct tm tm {
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
            };
            if (strptime(timestamp, timestamp_format(), &tm) == nullptr) {
                throw std::invalid_argument("can't parse timestamp");
            }
            m_timestamp = static_cast<uint32_t>(timegm(&tm));
#else
            struct tm tm;
            int n = sscanf(timestamp, "%4d-%2d-%2dT%2d:%2d:%2dZ", &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
            if (n != 6) {
                throw std::invalid_argument("can't parse timestamp");
            }
            tm.tm_year -= 1900;
            tm.tm_mon--;
            tm.tm_wday = 0;
            tm.tm_yday = 0;
            tm.tm_isdst = 0;
            m_timestamp = static_cast<uint32_t>(_mkgmtime(&tm));
#endif
        }
Esempio n. 5
0
int pg_con_connect(db_con_t *con)
{
	struct pg_con *pcon;
	struct pg_uri *puri;
	char *port_str;
	int ret, i = 0;
	const char *keywords[10], *values[10];
	char to[16];

	pcon = DB_GET_PAYLOAD(con);
	puri = DB_GET_PAYLOAD(con->uri);

	/* Do not reconnect already connected connections */
	if(pcon->flags & PG_CONNECTED)
		return 0;

	DBG("postgres: Connecting to %.*s:%.*s\n", con->uri->scheme.len,
			ZSW(con->uri->scheme.s), con->uri->body.len, ZSW(con->uri->body.s));

	if(puri->port > 0) {
		port_str = int2str(puri->port, 0);
		keywords[i] = "port";
		values[i++] = port_str;
	} else {
		port_str = NULL;
	}

	if(pcon->con) {
		PQfinish(pcon->con);
		pcon->con = NULL;
	}

	keywords[i] = "host";
	values[i++] = puri->host;
	keywords[i] = "dbname";
	values[i++] = puri->database;
	keywords[i] = "user";
	values[i++] = puri->username;
	keywords[i] = "password";
	values[i++] = puri->password;
	if(pg_timeout > 0) {
		snprintf(to, sizeof(to) - 1, "%d", pg_timeout + 3);
		keywords[i] = "connect_timeout";
		values[i++] = to;
	}

	keywords[i] = values[i] = NULL;

	pcon->con = PQconnectdbParams(keywords, values, 1);

	if(pcon->con == NULL) {
		ERR("postgres: PQconnectdbParams ran out of memory\n");
		goto error;
	}

	if(PQstatus(pcon->con) != CONNECTION_OK) {
		ERR("postgres: %s\n", PQerrorMessage(pcon->con));
		goto error;
	}

	/* Override default notice processor */
	PQsetNoticeProcessor(pcon->con, notice_processor, 0);

#ifdef HAVE_PGSERVERVERSION
	DBG("postgres: Connected. Protocol version=%d, Server version=%d\n",
			PQprotocolVersion(pcon->con), PQserverVersion(pcon->con));
#else
	DBG("postgres: Connected. Protocol version=%d, Server version=%d\n",
			PQprotocolVersion(pcon->con), 0);
#endif

#if defined(SO_KEEPALIVE) && defined(TCP_KEEPIDLE)
	if(pg_keepalive) {
		i = 1;
		if(setsockopt(
				   PQsocket(pcon->con), SOL_SOCKET, SO_KEEPALIVE, &i, sizeof(i))
				< 0) {
			LM_WARN("failed to set socket option keepalive\n");
		}
		if(setsockopt(PQsocket(pcon->con), IPPROTO_TCP, TCP_KEEPIDLE,
				   &pg_keepalive, sizeof(pg_keepalive))
				< 0) {
			LM_WARN("failed to set socket option keepidle\n");
		}
	}
#endif

	ret = timestamp_format(pcon->con);
	if(ret == 1 || ret == -1) {
		/* Assume INT8 representation if detection fails */
		pcon->flags |= PG_INT8_TIMESTAMP;
	} else {
		pcon->flags &= ~PG_INT8_TIMESTAMP;
	}

	if(get_oids(con) < 0)
		goto error;

	pcon->flags |= PG_CONNECTED;
	return 0;

error:
	if(pcon->con)
		PQfinish(pcon->con);
	pcon->con = NULL;
	return -1;
}
Esempio n. 6
0
int pg_con_connect(db_con_t* con)
{
	struct pg_con* pcon;
	struct pg_uri* puri;
	char* port_str;
	int ret;
	
	pcon = DB_GET_PAYLOAD(con);
	puri = DB_GET_PAYLOAD(con->uri);
	
	/* Do not reconnect already connected connections */
	if (pcon->flags & PG_CONNECTED) return 0;

	DBG("postgres: Connecting to %.*s:%.*s\n",
		con->uri->scheme.len, ZSW(con->uri->scheme.s),
		con->uri->body.len, ZSW(con->uri->body.s));

	if (puri->port > 0) {
		port_str = int2str(puri->port, 0);
	} else {
		port_str = NULL;
	}

	if (pcon->con) {
		PQfinish(pcon->con);
		pcon->con = NULL;
	}

	pcon->con = PQsetdbLogin(puri->host, port_str,
							 NULL, NULL, puri->database,
							 puri->username, puri->password);
	
	if (pcon->con == NULL) {
		ERR("postgres: PQsetdbLogin ran out of memory\n");
		goto error;
	}
	
	if (PQstatus(pcon->con) != CONNECTION_OK) {
		ERR("postgres: %s\n", PQerrorMessage(pcon->con));
		goto error;
	}
	
	/* Override default notice processor */
	PQsetNoticeProcessor(pcon->con, notice_processor, 0);
	
	DBG("postgres: Connected. Protocol version=%d, Server version=%d\n", 
	    PQprotocolVersion(pcon->con),
#ifdef HAVE_PGSERVERVERSION
	    PQserverVersion(pcon->con)
#else
	    0
#endif
	    );

	ret = timestamp_format(pcon->con);
	if (ret == 1 || ret == -1) {
		/* Assume INT8 representation if detection fails */
		pcon->flags |= PG_INT8_TIMESTAMP;
	} else {
		pcon->flags &= ~PG_INT8_TIMESTAMP;
	}

	if (get_oids(con) < 0) goto error;

	pcon->flags |= PG_CONNECTED;
	return 0;

 error:
	if (pcon->con) PQfinish(pcon->con);
	pcon->con = NULL;
	return -1;
}