Convert to C/C++?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SpreadTooThin

    #1

    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)
    # 0x01b21dd213814 000 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) + 0x01b21dd213814 000L
    self.clock_seq = random.randrang e(1<<14L) # instead of stable
    storage
    self.time_low = self.timestamp & 0xffffffffL
    self.time_mid = (self.timestamp >32L) & 0xffffL
    self.time_hi_ve rsion = (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_ve rsion
    #print 'clock_seq ', self.clock_seq, self.clock_seq_ low,
    self.clock_seq_ hi_variant

    vs unix gettimeofday... .

    int gettimeofday(st ruct 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 */
    };

  • Douglas Wells

    #2
    Re: Convert to C/C++?

    In article <1181852202.897 976.312280@j4g2 000prf.googlegr oups.com>, SpreadTooThin <bjobrien62@gma il.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.randrang e(value) is approximately equivalent to:
    long random_randrang e (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)
    # 0x01b21dd213814 000 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) + 0x01b21dd213814 000L
    self.clock_seq = random.randrang e(1<<14L) # instead of stable storage
    self.time_low = self.timestamp & 0xffffffffL
    self.time_mid = (self.timestamp >32L) & 0xffffL
    self.time_hi_ve rsion = (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_ve rsion
    #print 'clock_seq ', self.clock_seq, self.clock_seq_ low, self.clock_seq_ hi_variant
    >
    vs unix gettimeofday... .
    >
    int gettimeofday(st ruct 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- .

    Comment

    • Tim Roberts

      #3
      Re: Convert to C/C++?

      SpreadTooThin <bjobrien62@gma il.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, timr@probo.com
      Providenza & Boekelheide, Inc.

      Comment

      • Jorgen Grahn

        #4
        Re: Convert to C/C++?

        On Thu, 14 Jun 2007 13:16:42 -0700, SpreadTooThin <bjobrien62@gma il.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(st ruct 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.dyn dns.org R'lyeh wgah'nagl fhtagn!

        Comment

        Working...