In Magento 2 you need to create a module.

app/code/Vendorname/Mage2rewrite/etc/module.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
	<module name="Vendorname_Mage2rewrite" setup_version="1.0.0"/>
</config>

app/code/Vendorname/Mage2rewrite/composer.json

{
    "name": "vendorname/module-mage2rewrite",
    "description": "N/A",
    "require": {
        "php": "~7.0.13|~7.1.0",
        "magento/module-config": "101.0.*",
        "magento/module-store": "100.2.*",
        "magento/module-backend": "100.2.*",
        "magento/framework": "101.0.*"
    },
    "type": "magento2-module",
    "version": "100.0.2",
    "license": [
        "OSL-3.0",
        "AFL-3.0"
    ],
    "autoload": {
        "files": [
            "registration.php"
        ],
        "psr-4": {
            "Vendorname\\Mage2rewrite\\": ""
        }
    }
}

app/code/Vendorname/Mage2rewrite/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendorname_Mage2rewrite',
    __DIR__
);

app/code/Vendorname/Mage2rewrite/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/e†c/config.xsd">
 
   <!-- Overriding Timezone.php using preference method -->
   <preference for="Magento\Framework\Stdlib\DateTime\Timezone" type="Vendorname\Mage2rewrite\Stdlib\DateTime\Timezone"/>
   
   <!-- Overriding TimezoneTest.php using preference method -->
   <preference for="Magento\Framework\Stdlib\Test\Unit\DateTime\TimezoneTest" type="Vendorname\Mage2rewrite\Stdlib\Test\Unit\DateTime\TimezoneTest"/>
</config>

app/code/Vendorname/Mage2rewrite/Stdlib/DateTime/Timezone.php

<?php

namespace Vendorname\Mage2rewrite\Stdlib\DateTime;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ScopeInterface;
use Magento\Framework\App\ScopeResolverInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Locale\ResolverInterface;
use Magento\Framework\Phrase;

/**
* Message model factory
*/
class Timezone extends \Magento\Framework\Stdlib\DateTime\Timezone {

    public function scopeDate($scope = null, $date = null, $includeTime = false) {
        
        $timezone = new \DateTimeZone(
            $this->_scopeConfig->getValue($this->getDefaultTimezonePath(), $this->_scopeType, $scope)
        );
        switch (true) {
            case (empty($date)):
                $date = new \DateTime('now', $timezone);
                break;
            case ($date instanceof \DateTime):
            case ($date instanceof \DateTimeImmutable):
                $date = $date->setTimezone($timezone);
                break;
            default:
                $date = new \DateTime(is_numeric($date) ? '@' . $date : $date);
                $date->setTimezone($timezone);
                break;
        }
    
        if (!$includeTime) {
            $date->setTime(0, 0, 0);
        }
    
        return $date;
    }
}

app/code/Vendorname/Mage2rewrite/Stdlib/Test/Unit/DateTime/TimezoneTest.php

<?php

namespace Vendorname\Mage2rewrite\Stdlib\Test\Unit\DateTime;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ScopeResolverInterface;
use Magento\Framework\Locale\ResolverInterface;
use Magento\Framework\Stdlib\DateTime;
use Magento\Framework\Stdlib\DateTime\Timezone;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;

/**
* Message model factory
*/
class TimezoneTest extends \Magento\Framework\Stdlib\Test\Unit\DateTime\TimezoneTest {

    /**
     * @return array
     */
    public function scopeDateDataProvider(): array {
        
        $utcTz = new \DateTimeZone('UTC');

        return [
            ['2018-10-20 00:00:00', 'UTC', 'en_US', '2018-10-20 00:00:00'],
            ['2018-10-20 00:00:00', 'America/Los_Angeles', 'en_US', '2018-10-19 17:00:00'],
            ['2018-10-20 00:00:00', 'Asia/Qatar', 'en_US', '2018-10-20 03:00:00'],
            ['2018-10-20 00:00:00', 'America/Los_Angeles', 'en_GB', '2018-10-19 17:00:00'],
            ['10/20/18 00:00', 'UTC', 'en_US', '2018-10-20 00:00:00'],
            ['10/20/18 00:00', 'America/Los_Angeles', 'en_US', '2018-10-19 17:00:00'],
            ['10/20/18 00:00', 'Asia/Qatar', 'en_US', '2018-10-20 03:00:00'],
            ['10/20/18 00:00', 'UTC', 'fr_FR', '2018-10-20 00:00:00'],
            ['10/20/18 00:00', 'America/Los_Angeles', 'fr_FR', '2018-10-19 17:00:00'],
            ['10/20/18 00:00', 'Asia/Qatar', 'fr_FR', '2018-10-20 03:00:00'],
            [1539993600, 'UTC', 'en_US', '2018-10-20 00:00:00'],
            [1539993600, 'America/Los_Angeles', 'en_US', '2018-10-19 17:00:00'],
            [1539993600, 'Asia/Qatar', 'en_US', '2018-10-20 03:00:00'],
            [new \DateTime('2018-10-20', $utcTz), 'UTC', 'en_US', '2018-10-20 00:00:00'],
            [new \DateTime('2018-10-20', $utcTz), 'America/Los_Angeles', 'en_US', '2018-10-19 17:00:00'],
            [new \DateTime('2018-10-20', $utcTz), 'Asia/Qatar', 'en_US', '2018-10-20 03:00:00'],
            [new \DateTimeImmutable('2018-10-20', $utcTz), 'UTC', 'en_US', '2018-10-20 00:00:00'],
            [new \DateTimeImmutable('2018-10-20', $utcTz), 'America/Los_Angeles', 'en_US', '2018-10-19 17:00:00'],
            [new \DateTimeImmutable('2018-10-20', $utcTz), 'Asia/Qatar', 'en_US', '2018-10-20 03:00:00'],
        ];
    }
}

Categories: Magento

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *