using boost::date_time for epoch time #secs

M

Mark

I want to simply get the unix style epoch time
(number of secs to now from Jan 01, 1970 UTC) for
use as a timestamp to track the staleness of some
objects. So I don't care about time zones or whatever
as long as it's consistent. So I picked the epoch time
UTC getting the value I wanted, "timestamp", this way:

#include "boost/date_time/local_time/local_time.hpp"
using namespace boost::gregorian;
using namespace boost::local_time;
using namespace boost::posix_time;

namespace {
const ptime EPOCH(date(1970,1,1));
}

void
somefunc()
{
const ptime cur_time(second_clock::universal_time());
const unsigned int timestamp = (cur_time - EPOCH).total_seconds();
......
}


but I'd prefer a way that doesn't need the time_duration value (the cur_time - EPOCH)
and the EPOCH and the "total_seconds" call. I'd have thought there'd be so much
use for epoch time in seconds that there's be a prerolled function for this
in boost::date_time but I'm not seeing it. Is the above about as simple as it
gets with boost::date_time for this?

Mark
 
V

Victor Bazarov

Mark said:
I want to simply get the unix style epoch time
(number of secs to now from Jan 01, 1970 UTC) for
use as a timestamp to track the staleness of some
objects. So I don't care about time zones or whatever
as long as it's consistent. So I picked the epoch time
UTC getting the value I wanted, "timestamp", this way:

#include "boost/date_time/local_time/local_time.hpp"
[..]
Is the above about as
simple as it gets with boost::date_time for this?

This is not a Boost newsgroup. Please visit the Boost web site
to learn of the ways to access and of the location of their forums
where all this would be definitely on topic.

V
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

I want to simply get the unix style epoch time
(number of secs to now from Jan 01, 1970 UTC) for
use as a timestamp to track the staleness of some
objects. So I don't care about time zones or whatever
as long as it's consistent. So I picked the epoch time
UTC getting the value I wanted, "timestamp", this way:

What's wrong with just including time.h/ctime and using time()? While
it's not guaranteed to be the number of seconds since the epoch (could
be millisecond or something else) in the C and C++ standards it will be
on both Windows and POSIXS systems, and even if it's not, it will still
work as a timestamp (if you only want it to indicate staleness).
 
J

James Kanze

On 2007-07-17 20:58, Mark wrote:
What's wrong with just including time.h/ctime and using time()? While
it's not guaranteed to be the number of seconds since the epoch (could
be millisecond or something else) in the C and C++ standards it will be
on both Windows and POSIXS systems, and even if it's not, it will still
work as a timestamp (if you only want it to indicate staleness).

time_t isn't guaranteed to be an integral type---it could be
double. (In practice, of course, you're code probably isn't
portable everywhere, and I don't know of a system where time_t
isn't integral. It's what I'd do as well.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top