1+ <?php
2+ /**
3+ * Copyright © Magento, Inc. All rights reserved.
4+ * See COPYING.txt for license details.
5+ */
6+ declare (strict_types=1 );
7+
8+ namespace Magento \Framework \Stdlib \DateTime \Timezone ;
9+
10+ use Magento \Framework \Locale \ResolverInterface ;
11+ use Magento \Framework \App \Config \ScopeConfigInterface ;
12+
13+ /**
14+ * Class LocalizedDateToUtcConverter
15+ */
16+ class LocalizedDateToUtcConverter implements LocalizedDateToUtcConverterInterface
17+ {
18+ /**
19+ * Contains default date format
20+ *
21+ * @var string
22+ */
23+ private $ defaultFormat = 'Y-m-d H:i:s ' ;
24+
25+ /**
26+ * @var ResolverInterface
27+ */
28+ private $ localeResolver ;
29+
30+ /**
31+ * @var ScopeConfigInterface
32+ */
33+ private $ scopeConfig ;
34+
35+ /**
36+ * @var string
37+ */
38+ private $ scopeType ;
39+
40+ /**
41+ * @var string
42+ */
43+ private $ defaultTimezonePath ;
44+
45+ /**
46+ * LocalizedDateToUtcConverter constructor.
47+ *
48+ * @param ResolverInterface $localeResolver
49+ */
50+ public function __construct (
51+ ResolverInterface $ localeResolver ,
52+ ScopeConfigInterface $ scopeConfig ,
53+ $ scopeType ,
54+ $ defaultTimezonePath
55+ )
56+ {
57+ $ this ->localeResolver = $ localeResolver ;
58+ $ this ->scopeConfig = $ scopeConfig ;
59+ $ this ->scopeType = $ scopeType ;
60+ $ this ->defaultTimezonePath = $ defaultTimezonePath ;
61+ }
62+
63+ /**
64+ * @inheritdoc
65+ */
66+ public function convertLocalizedDateToUtc ($ date )
67+ {
68+ $ locale = $ this ->localeResolver ->getLocale ();
69+ $ formatter = new \IntlDateFormatter (
70+ $ locale ,
71+ \IntlDateFormatter::MEDIUM ,
72+ \IntlDateFormatter::MEDIUM ,
73+ $ this ->getConfigTimezone (),
74+ null ,
75+ null
76+ );
77+ $ unixTime = $ formatter ->parse ($ date );
78+ $ dateTime = new DateTime ($ this );
79+ $ dateUniversal = $ dateTime ->gmtDate (null , $ unixTime );
80+ $ date = new \DateTime ($ dateUniversal , new \DateTimeZone ($ this ->getConfigTimezone ()));
81+
82+ $ date ->setTimezone (new \DateTimeZone ('UTC ' ));
83+
84+ return $ date ->format ($ this ->defaultFormat );
85+ }
86+ }
0 commit comments