471,342 Members | 1,984 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,342 software developers and data experts.

Convert to C/C++?

I am wondering if someone who knows the implemention of python's time
could help converting this to c/c++....

nanoseconds = int(time.time() * 1e9)
# 0x01b21dd213814000 is the number of 100-ns intervals between the
# UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01
00:00:00.
self.timestamp = int(nanoseconds/100) + 0x01b21dd213814000L
self.clock_seq = random.randrange(1<<14L) # instead of stable
storage
self.time_low = self.timestamp & 0xffffffffL
self.time_mid = (self.timestamp >32L) & 0xffffL
self.time_hi_version = (self.timestamp >48L) & 0x0fffL
self.clock_seq_low = self.clock_seq & 0xffL
self.clock_seq_hi_variant = (self.clock_seq >8L) & 0x3fL
#print 'timestamp ', self.timestamp, self.time_low, self.time_mid,
self.time_hi_version
#print 'clock_seq ', self.clock_seq, self.clock_seq_low,
self.clock_seq_hi_variant

vs unix gettimeofday....

int gettimeofday(struct timeval *tp, struct timezone *tzp);

struct timeval {
long tv_sec; /* seconds since Jan. 1, 1970 */
long tv_usec; /* and microseconds */
};

struct timezone {
int tz_minuteswest; /* of Greenwich */
int tz_dsttime; /* type of dst correction to apply */
};

Jun 14 '07 #1
3 2654
In article <11**********************@j4g2000prf.googlegroups. com>, SpreadTooThin <bj********@gmail.comwrites:
I am wondering if someone who knows the implemention of python's time
could help converting this to c/c++....
First the obligatory sermon:

If you are truly intending to utilize a UUID generator on a UNIX
platform, I urge you to seek out and use a native implementation.
Both Linux and FreeBSD distributions include such a capability,
and there are also functions available in freely available libraries.
Such a native function would offer several advantages over the
code you are attempting to copy:

1) it could utilize the full capability of the clock rather
than be limited to the microseconds precision in gettimeofday;
2) it is probably implemented utilizing a system locking function
to provide a truly unique-per-system UID;
3) it would almost certainly incorporate a per-machine unique
identifier, as called for by the spec, and which is missing
from your prototype code;
4) it would have easier access to better pseudo-random number
generators;
4) it would probably be coded better than the code you provide,
which squanders quite a bit of precision (I haven't counted
exactly, but I would guess about 10 bits worth);

Also, it sounds as though you are not comfortable coding in C (as
most of the information that you seek is straight-forwardly available
from the Python and UNIX documentation), and there are numerous
subtleties in the code that you must develop:
1) the management of endianness will require careful attention
to your use of this function;
2) conversion to and from 64-bit numbers requires attention to
the suffixes.

With that said;

Python doesn't define the time epoch except on UNIX platforms, but
it's almost certainly the same as on UNIX. So,

time.time() is equivalent to:
double time_time (void) { struct timeval tv;
gettimeofday (&tv, (void *) NULL);
return tv.tv_sec + tv.tv_usec / 1e6); }

random.randrange(value) is approximately equivalent to:
long random_randrange (unsigned long stop) {
return rand () % stop; }
- with the caveats that:
1) you should choose a better pseudo-random number generator
than rand();
2) you should be sure to initialize the random function, e.g.,
via srand();
nanoseconds = int(time.time() * 1e9)
# 0x01b21dd213814000 is the number of 100-ns intervals between the
# UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
self.timestamp = int(nanoseconds/100) + 0x01b21dd213814000L
self.clock_seq = random.randrange(1<<14L) # instead of stable storage
self.time_low = self.timestamp & 0xffffffffL
self.time_mid = (self.timestamp >32L) & 0xffffL
self.time_hi_version = (self.timestamp >48L) & 0x0fffL
self.clock_seq_low = self.clock_seq & 0xffL
self.clock_seq_hi_variant = (self.clock_seq >8L) & 0x3fL
You should look up the definition of the various fields (e.g.,
clock_seq, time_low, time_mid) in a UUID specification, for example
IETF RFC 4122. They have precise width requirements.
#print 'timestamp ', self.timestamp, self.time_low, self.time_mid, self.time_hi_version
#print 'clock_seq ', self.clock_seq, self.clock_seq_low, self.clock_seq_hi_variant

vs unix gettimeofday....

int gettimeofday(struct timeval *tp, struct timezone *tzp);
By the way, the UNIX gettimeofday function does not include timezone:
the prototype of the second argument is "void *" and must be passed
as NULL.

Good luck, - dmw

--
.. Douglas Wells . Connection Technologies .
.. Internet: -sp9804- -at - contek.com- .
Jun 15 '07 #2
SpreadTooThin <bj********@gmail.comwrote:
>
I am wondering if someone who knows the implemention of python's time
could help converting this to c/c++....
If you are running on Windows, you can replace this entire thing with one
call to CoCreateGuid, possibly with a call to StringFromGUID to make it
printable.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jun 15 '07 #3
On Thu, 14 Jun 2007 13:16:42 -0700, SpreadTooThin <bj********@gmail.comwrote:
I am wondering if someone who knows the implemention of python's time
could help converting this to c/c++....

nanoseconds = int(time.time() * 1e9)
[straightforward non-time arithmetic snipped]
>
vs unix gettimeofday....

int gettimeofday(struct timeval *tp, struct timezone *tzp);

struct timeval {
long tv_sec; /* seconds since Jan. 1, 1970 */
long tv_usec; /* and microseconds */
};
Quite simply, time.time() corresponds to tv_sec + tv_usec/1e6,
modulo any rounding and numerical errors introduced by the
floating-point format used.
struct timezone {
int tz_minuteswest; /* of Greenwich */
int tz_dsttime; /* type of dst correction to apply */
};
The struct timezone is not set by Linux/glibc gettimeofday(), so
you can happily ignore it. You only want UTC time anyway.

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org R'lyeh wgah'nagl fhtagn!
Jun 19 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by Lauren Quantrell | last post: by
1 post views Thread by Logan X via .NET 247 | last post: by
3 posts views Thread by Convert TextBox.Text to Int32 Problem | last post: by
7 posts views Thread by patang | last post: by
4 posts views Thread by Edwin Knoppert | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.