Connecting Tech Pros Worldwide Help | Site Map

using boost::date_time for epoch time #secs

  #1  
Old July 17th, 2007, 08:05 PM
Mark
Guest
 
Posts: n/a

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
  #2  
Old July 17th, 2007, 08:25 PM
Victor Bazarov
Guest
 
Posts: n/a

re: using boost::date_time for epoch time #secs


Mark wrote:
Quote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  #3  
Old July 17th, 2007, 11:35 PM
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
 
Posts: n/a

re: using boost::date_time for epoch time #secs


On 2007-07-17 20:58, Mark wrote:
Quote:
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).

--
Erik Wikström
  #4  
Old July 19th, 2007, 09:55 AM
James Kanze
Guest
 
Posts: n/a

re: using boost::date_time for epoch time #secs


On Jul 18, 12:28 am, Erik Wikström <Erik-wikst...@telia.comwrote:
Quote:
On 2007-07-17 20:58, Mark wrote:
Quote:
Quote:
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:
Quote:
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.)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Closed Thread