Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP long integers for thrift

My Thrift service expects to receive a Long integer representing a timestamp in milliseconds, but coming from PHP, I know PHP thrift is supposed to automagically turn my PHP types into thrift types, but which PHP type does it expect for Long integers? I think my computer is 64-bit, but since I think that PHP integers' length is platform dependent, I don't really want to depend upon a platform-dependent length for my integers.

I am currently grabbing microtime() and multiplying by 1000, then converting to integer. Is this the "correct" way to work with PHP & thrift long ints?

like image 356
nnythm Avatar asked Jul 11 '12 14:07

nnythm


2 Answers

You are right,

The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18. PHP does not support unsigned integers. Integer size can be determined using the constant PHP_INT_SIZE, and maximum value using the constant PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5.

http://www.php.net/manual/en/language.types.integer.php

If you use microtime() you need not to divide it by 1000. Its float, you may want to multiply it by 1000.

You may use BC Math for calclulate it as numbers, using string types. I guess string are OK to communicate with any other thing.

In case of multiplying by 1000, you even not need BCMath. Just delete comma from string representation of microtime(true) (or space from microtime)

like image 60
RiaD Avatar answered Oct 11 '22 12:10

RiaD


Maybe you should use id of string type as like twitter: https://dev.twitter.com/docs/twitter-ids-json-and-snowflake

like image 28
Frank He Avatar answered Oct 11 '22 11:10

Frank He