EBernhardson (WMF) has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/79068


Change subject: Extract timestamps from UUID
......................................................................

Extract timestamps from UUID

Change-Id: Id20bba738d92fc1108398f6136811cf9db22f81f
---
M includes/Model/UUID.php
1 file changed, 12 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Flow 
refs/changes/68/79068/1

diff --git a/includes/Model/UUID.php b/includes/Model/UUID.php
index 2aee92c..2afba55 100644
--- a/includes/Model/UUID.php
+++ b/includes/Model/UUID.php
@@ -26,9 +26,9 @@
                } elseif ( strlen( $input ) == 32 && preg_match( 
'/^[a-fA-F0-9]+$/', $input ) ) {
                        return new self( pack( 'H*', $input ) );
                } elseif ( is_numeric( $input ) ) {
-                       return new self( pack( 'H*', wfBaseConvert( $input, 10, 
16 ) ) );
+                       return new self( pack( 'H*', wfBaseConvert( $input, 10, 
16, 32 ) ) );
                } elseif ( $input === false ) {
-                       return new self( pack( 'H*', 
\UIDGenerator::newTimestampedUID128( 16 ) ) );
+                       return new self( pack( 'H*', str_pad( 
\UIDGenerator::newTimestampedUID128( 16 ), 32, '0', STR_PAD_LEFT ) ) );
                } elseif ( $input === null ) {
                        return null;
                } else {
@@ -46,7 +46,7 @@
        }
 
        public function getHex() {
-               return bin2hex( $this->binaryValue );
+               return str_pad( bin2hex( $this->binaryValue ), 32, '0', 
STR_PAD_LEFT );
        }
 
        public function getBinary() {
@@ -57,6 +57,15 @@
                return wfBaseConvert( $this->getHex(), 16, 10 );
        }
 
+       public function getTimestamp() {
+               // First 6 bytes === 48 bits
+               $timePortion = substr( $this->getHex(), 0, 12 );
+               $bits_48 = wfBaseConvert( $timePortion, 16, 2, 48 );
+               $bits_46 = substr( $bits_48, 0, 46 );
+               $msTimestamp = wfBaseConvert( $bits_46, 2, 10 );
+               return wfTimestamp( TS_MW, intval( $msTimestamp / 1000 ) );
+       }
+
        public static function convertUUIDs( $array ) {
                foreach( ObjectManager::makeArray( $array ) as $key => $value ) 
{
                        if ( is_a( $value, 'Flow\Model\UUID' ) ) {

-- 
To view, visit https://gerrit.wikimedia.org/r/79068
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id20bba738d92fc1108398f6136811cf9db22f81f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Flow
Gerrit-Branch: master
Gerrit-Owner: EBernhardson (WMF) <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to