public function testRound() { $this->assertTrue(CMathi::round(1.2) === 1); $this->assertTrue(CMathi::round(2.8) === 3); $this->assertTrue(CMathi::round(3.5) === 4); $this->assertTrue(CMathi::round(-1.2) === -1); $this->assertTrue(CMathi::round(-2.8) === -3); $this->assertTrue(CMathi::round(3) === 3); $this->assertTrue(CMathi::round(-4) === -4); }
/** * Returns the milliseconds of a point in time. * * If not zero, the returned value is positive for any point in time after the midnight of January 1, 1970 UTC and * is negative for any point in time before it. * * @return int The milliseconds of the point in time (from 0 to 999 or from 0 to -999). */ public function MTime() { return CMathi::round(($this->m_FTime - (int) $this->m_FTime) * 1000); }
/** * @ignore */ public static function onThirdPartyUpdateByPackageManager() { if (!self::isInCliMode()) { // This method can be run in CLI mode only. assert('false', vs(isset($this), get_defined_vars())); } $timeoutPause = new CTimeoutPause(); CShell::speak("Processing third-party components ..."); $tpDps = CFile::listDirectories(CFilePath::absolute($GLOBALS["PHRED_PATH_TO_THIRD_PARTY"])); $ignorePackages = CConfiguration::option("upd.thirdPartyOopWrappingIgnorePackages"); $ignorePackagesL2 = CArray::filter($ignorePackages, function ($package) { return CString::find($package, "/"); }); $newTpDps = CArray::make(); $len = CArray::length($tpDps); for ($i = 0; $i < $len; $i++) { $tpDp = $tpDps[$i]; $dirName = CFilePath::name($tpDp); if (!CArray::find($ignorePackages, $dirName)) { $dpHasL2DirsToIgnore = CArray::find($ignorePackagesL2, $dirName, function ($packageL2, $dirName) { return CString::equals(CFilePath::directory($packageL2), $dirName); }); if (!$dpHasL2DirsToIgnore) { CArray::push($newTpDps, $tpDp); } else { $tpSubDps = CFile::listDirectories($tpDp); $tpSubDps = CArray::filter($tpSubDps, function ($subDp) use($ignorePackagesL2) { return !CArray::find($ignorePackagesL2, $subDp, function ($packageL2, $subDp) { return CString::endsWith($subDp, $packageL2); }); }); CArray::pushArray($newTpDps, $tpSubDps); } } } $tpDps = $newTpDps; $wrapProtectedMethods = CConfiguration::option("upd.thirdPartyOopWrappingInProtectedMethods"); $wrapPrivateMethods = CConfiguration::option("upd.thirdPartyOopWrappingInPrivateMethods"); static $s_stdPhpTag = "<?php"; static $s_progressResolution = 0.05; $prevProgressDivR = 0; $tpDpsLen = CArray::length($tpDps); for ($i0 = 0; $i0 < $tpDpsLen; $i0++) { $tpFps = CFile::reFindFilesRecursive($tpDps[$i0], "/\\.php\\d?\\z/"); $tpFpsLen = CArray::length($tpFps); for ($i1 = 0; $i1 < $tpFpsLen; $i1++) { $fileCode = CFile::read($tpFps[$i1]); if (!CString::find($fileCode, self::$ms_thirdPartyAlreadyOopWrappedMark)) { $parser = new PhpParser\Parser(new PhpParser\Lexer()); try { // Parse the code. $statements = $parser->parse($fileCode); // Wrap the code into OOP. $traverser = new PhpParser\NodeTraverser(); $mainVisitor = new CMainVisitor($wrapProtectedMethods, $wrapPrivateMethods); $traverser->addVisitor($mainVisitor); $statements = $traverser->traverse($statements); $wrappedCode = (new PhpParser\PrettyPrinter\Standard())->prettyPrint($statements); $phpTagPos = CString::indexOf($wrappedCode, $s_stdPhpTag); if ($phpTagPos == -1) { $wrappedCode = "{$s_stdPhpTag}\n\n{$wrappedCode}"; $phpTagPos = 0; } $wrappedCode = CString::insert($wrappedCode, $phpTagPos + CString::length($s_stdPhpTag), "\n\n" . self::$ms_thirdPartyAlreadyOopWrappedMark); // Save. CFile::write($tpFps[$i1], $wrappedCode); } catch (PhpParser\Error $parserError) { CShell::say("\nPhpParser: " . $tpFps[$i1] . ", at line " . $parserError->getRawLine() . ": " . $parserError->getRawMessage()); } } $progress = (double) ($i0 / $tpDpsLen + 1 / $tpDpsLen * $i1 / $tpFpsLen); $progressDivR = CMathi::floor($progress / $s_progressResolution); if ($progressDivR != $prevProgressDivR) { $perc = CMathi::round($progressDivR * $s_progressResolution * 100); CShell::speak("{$perc}%"); } $prevProgressDivR = $progressDivR; } } CShell::speak("100%"); CShell::say("Done."); $timeoutPause->end(); }
/** * Converts an integer quantity of mass from one unit into another and returns the result. * * @param int $quantity The quantity to be converted. * @param enum $fromUnit The source unit. * @param enum $toUnit The destination unit. * * @return int The converted quantity, after rounding to the nearest integer. */ public static function convertMassi($quantity, $fromUnit, $toUnit) { assert('is_int($quantity) && is_enum($fromUnit) && is_enum($toUnit)', vs(isset($this), get_defined_vars())); assert('$quantity >= 0', vs(isset($this), get_defined_vars())); if ($fromUnit == $toUnit) { return $quantity; } $floatQuantity = (double) $quantity; $milligramQty; switch ($fromUnit) { case self::MILLIGRAM: $milligramQty = $floatQuantity; break; case self::GRAM: $milligramQty = $floatQuantity * 1000; break; case self::KILOGRAM: $milligramQty = $floatQuantity * 1000000; break; case self::TON: $milligramQty = $floatQuantity * 1000000000; break; case self::OUNCE: $milligramQty = $floatQuantity * 28349.5231; break; case self::POUND: $milligramQty = $floatQuantity * 453592.37; break; case self::STONE: $milligramQty = $floatQuantity * 6350293.18; break; case self::SHORT_TON: $milligramQty = $floatQuantity * 907184740; break; case self::LONG_TON: $milligramQty = $floatQuantity * 1016046908.8; break; default: assert('false', vs(isset($this), get_defined_vars())); break; } $outputQty; switch ($toUnit) { case self::MILLIGRAM: $outputQty = CMathi::round($milligramQty); break; case self::GRAM: $outputQty = CMathi::round($milligramQty / 1000); break; case self::KILOGRAM: $outputQty = CMathi::round($milligramQty / 1000000); break; case self::TON: $outputQty = CMathi::round($milligramQty / 1000000000); break; case self::OUNCE: $outputQty = CMathi::round($milligramQty / 28349.5231); break; case self::POUND: $outputQty = CMathi::round($milligramQty / 453592.37); break; case self::STONE: $outputQty = CMathi::round($milligramQty / 6350293.18); break; case self::SHORT_TON: $outputQty = CMathi::round($milligramQty / 907184740); break; case self::LONG_TON: $outputQty = CMathi::round($milligramQty / 1016046908.8); break; default: assert('false', vs(isset($this), get_defined_vars())); break; } return $outputQty; }
/** * Returns the offset of a time zone from UTC, without adjusting it for daylight saving time. * * The offset is negative for the time zones located west of UTC (Greenwich, UK) and positive for the eastern ones. * * @return int The time zone's standard offset from UTC, in seconds. */ public function standardOffsetSeconds() { $itz = $this->ITimeZone(); $offset; $dstOffset; $itz->getOffset(time() * 1000, false, $offset, $dstOffset); return CMathi::round((double) $offset / 1000); }