Convert PHP code into Ruby!
RSS icon Email icon Home icon
  • microtime

    Posted on May 17th, 2009 RubyLove 1 comment

    Returns the current Unix timestamp (epoch) with microseconds.

    PHP

    echo microtime();
    // => 0.63928794 1242744141

    Ruby

    epoch_mirco = Time.now.to_f;
    epoch_full = Time.now.to_i;
     
    epoch_fraction = epoch_mirco - epoch_full;
     
    puts epoch_fraction.to_s + ' ' + epoch_full.to_s;
    # => 0.639287948608398 1242744141
     

    One response to “microtime”

    1. There looks to be a slight difference in the precision of the two implementations. The ruby is much more precise to make the match we’d want to reduce that especially if using this function in an encryption scheme….

      epoch_mirco = Time.now.to_f;
      epoch_full = Time.now.to_i;

      epoch_fraction = epoch_mirco - epoch_full;

      puts epoch_fraction.to_s + ‘ ‘ + epoch_full.to_s;
      # => 0.639287948608398 1242744141

      # reduce 0.77425300
      puts sprintf(”%.8f %s\n”, epoch_fraction, epoch_full.to_s)

    Leave a reply