473,320 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 2723
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Lauren Quantrell | last post by:
I have a stored procedure using Convert where the exact same Convert string works in the SELECT portion of the procedure but fails in the WHERE portion. The entire SP is listed below....
1
by: Logan X via .NET 247 | last post by:
It's official....Convert blows. I ran a number of tests converting a double to an integer usingboth Convert & CType. I *ASSUMED* that CType would piggy-back ontop of Convert, and that performance...
4
by: Eric Lilja | last post by:
Hello, I've made a templated class Option (a child of the abstract base class OptionBase) that stores an option name (in the form someoption=) and the value belonging to that option. The value is...
7
by: whatluo | last post by:
Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done...
3
by: Convert TextBox.Text to Int32 Problem | last post by:
Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In "old school C" we just used the atoi function...
7
by: patang | last post by:
I want to convert amount to words. Is there any funciton available? Example: $230.30 Two Hundred Thirty Dollars and 30/100
4
by: Edwin Knoppert | last post by:
In my code i use the text from a textbox and convert it to a double value. I was using Convert.ToDouble() but i'm used to convert comma to dot. This way i can assure the text is correct. However...
1
by: johnlim20088 | last post by:
Hi, Currently I have 6 web projects located in Visual Source Safe 6.0, as usual, everytime I will open solution file located in my local computer, connected to source safe, then check out/check in...
6
by: Ken Fine | last post by:
This is a basic question. What is the difference between casting and using the Convert.ToXXX methods, from the standpoint of the compiler, in terms of performance, and in other ways? e.g. ...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.