Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timezone in parse() is not converted to string #1958

Closed
stebogit opened this issue Dec 10, 2019 · 3 comments
Closed

Timezone in parse() is not converted to string #1958

stebogit opened this issue Dec 10, 2019 · 3 comments
Assignees
Labels
Milestone

Comments

@stebogit
Copy link

Hello,
I encountered an issue with the following PHPUnit code:

$date = '2018-02-01';
echo Carbon::parse($date, -8)->toIso8601String();
echo "\n";
echo Carbon::parse($date, '-8')->toIso8601String();

Carbon version: 2.27.0
PHP version: 7.3.4-1+ubuntu18.04.1+deb.sury.org+3 (from Homestead v7.2.1)

I expected to get:

2018-02-01T00:00:00-08:00
2018-02-01T00:00:00-08:00

But I actually get:

2018-02-01T00:00:00-01:00
2018-02-01T00:00:00-08:00

I might be wrong, but I would expect that the timezone would be interpreted as a string also in the constructor, like in setTimezone() where the type of the input does not matter:

Carbon::parse($date)->setTimezone(-8)->toIso8601String();   // 2018-01-31T16:00:00-08:00
Carbon::parse($date)->setTimezone('-8')->toIso8601String(); // 2018-01-31T16:00:00-08:00
@kylekatarnls
Copy link
Collaborator

It's a legacy stuff kept for backward compatibility. But I think we can get rid of it. I have to check the unit test that a fix for this would break.

You still can get no ambiguity using:

echo Carbon::parse($date, CarbonTimeZone::create(-8))->toIso8601String();

@kylekatarnls kylekatarnls self-assigned this Dec 11, 2019
@kylekatarnls kylekatarnls added this to the 2.28 milestone Dec 11, 2019
kylekatarnls added a commit to kylekatarnls/Carbon that referenced this issue Dec 12, 2019
@kylekatarnls
Copy link
Collaborator

Here is the unit test that show the fix:
https://github.com/briannesbitt/Carbon/pull/1959/files#diff-e647fc4a40b22cf61c3f64c24341ffaeL118

So next version will no longer try to auto-detect time zone name from the offset and will just use the offset.

Let me give some advice about timezones, you should work as long as you can with UTC, it's the universal way to represent time, while -8 is an arbitrary timezone offset that can be used by 0, 1 or many regions, and they can use it differently (with or without DST), and region can decide to change their timezone. So use UTC for create and manipulate server side dates, then change the timezone using tz() just before to display/send the date and prefer to use named timezone (America/New_York or Europe/London, etc.), or the best, send UTC ISO string and let the client handle the conversion.

I detailed the subject deeper here:
https://medium.com/@kylekatarnls/handle-dates-the-right-way-in-php-c215650fc4fa
https://medium.com/@kylekatarnls/always-use-utc-dates-and-times-8a8200ca3164

kylekatarnls added a commit that referenced this issue Dec 12, 2019
…ezone

Fix #1958 Standardize integer timezone input
@kylekatarnls
Copy link
Collaborator

Will be fixed in 2.28.0

jpmaga added a commit to jpmaga/Carbon that referenced this issue Jan 5, 2020
* Unbind $this in macros only for PHP < 8

* Privatize loadMixin* method as reserved for mixin() method

* Temporary disabling binding $this/static for PHP 8

* Set specific tests for each macros in PHP 7 and 8

* Persian language localization fixes

* Update Persian translations unit tests

* Update uz_Arab and mzn translations unit tests

* Fix briannesbitt#1816 from now translation in Chinese

* Update zh tests

* Add explicit imports and update composer description

* Cleanup PHPDoc

* Cleanup PHPDoc

* briannesbitt#1782 Fix from_now translation in Chinese

* Add comparison methods for CarbonPeriod bounds

* Add PHPDoc comments

* Allow string and interval in floor, round and ceil

* Implement carbonize() method get a Carbon date from interval/period/any

* Add rounding methods to CarbonInterval/CarbonPeriod

* Fix PHPDoc and add tests

* Fix PHPDoc for Carbon rounding methods

* Fix self references

* Add tests

* Add cast() method and traits mixins to CarbonInterval and CarbonPeriod

* Allow static methods in mixin traits

* Update CarbonInterface PHPDoc

* briannesbitt#1822 Use less short variants for short units in Polish

* Make zh_HK and zh_TW extend Hant/Hans

* Implement contains() and JsonSerializable in CarbonPeriod

* Implement new methods in CarbonPeriod
  - getIncludedStartDate()
  - getIncludedEndDate()
  - follows()
  - isFollowedBy()
  - isConsecutiveWith()

* Simplify duplicate code

* [Travis-CI] Simplify xdebug settings

* Mute deprecation notice

* Ignore mutability in equalTo comparison of periods

* Fix unit tests

* Fix typo

* Fix getIncludedEndDate

* Improve any native PHP classes and sub-classes as cast

* Fix briannesbitt#1831 Refresh global translator caching

* Add cast tests

* Take recurrences DatePeriod incrementation into account

* Mute type wrong PHPStan detections in tests

* Fix briannesbitt#1834 language detection in Lumen

* Make zh_Hans inherit from zh_Hant

* Add altNumbers option to forHumans/diffForHumans

* Fix alternate numbers in hours

* Ensure consistent output typing

* Add tests for altNumbers option briannesbitt#1817

* Fix briannesbitt#1837 fixed from_now translation in Fnnish and add few seconds

* briannesbitt#1837 Update Finnish tests

* Fix briannesbitt#1840 add short form or year for year in Turkish

* Update Turkish unit tests

* Fix PHP 8 compatibility

* Add doc for setDate()

* briannesbitt#1837 Add translation author

* Fix setISODate PHP 8 signature compatibility

* Implement methods that change of signature in PHP 8

* Allow null in __set_state for compatibility with PHP 8

Possible error to fix in PHP:
php/php-src#4526

* Remove temporary fix as fixed in PHP itself:
php/php-src@b17957e

* Remove obsolete tests

* Allow PHP 8 tests to fail until new PHP build is released

* Add test for setISODate()

* Fix Chinese Hant/Hans and meridiem

* [AR] Update with enhancements

* More enhancements

* Update files and fixing test unit

* Fix StyleCI issues

* Fix briannesbitt#1851 Update all Carbon classes on Laravel locale change

* Remove irrelevant test

* Fix PHP 8 compatibility

* Use require everywhere instead of include

* Implement briannesbitt#1856 Allow strings in isSame* methods

* Update PHPDoc and add tests

* Fix 1860 Allow Closure in setTestNow()

* Add create() test in Carbon::setTestNow()

* Fix briannesbitt#1861 Add aUnit option in Thai

* briannesbitt#1861 Add @nithisa authorship

* Allow to control timezone in testNow Closure

* Add PHPDoc

* Synchronize CarbonPeriod and CarbonInterval with Laravel locale

Thanks to @craig-sen to point out this

* Simplify swap expressions

* Reset language for all classes before running Laravel locale test

* Improve period/interval string detection in Carbon::make()

This allows to use make() with customized parse() methods

* Fix briannesbitt#1842 Latvian declension, aUnit and short units

* Add previous author complete name

* Fix zh locales merging

* Remove irrelevant zh_MO overrides

* Corrected sl-SI

* briannesbitt#1828 Make spaces around numbers consistent (both sides or none)

* briannesbitt#1824 Fix interpolations precedence and ordering

* Add missing tests

* Fix code style

* Update Slovenian unit tests

* Apply ago/from_now/before/after declension in multi-parts diffs

* briannesbitt#1849 Add Slovenian short units and "from now" declension

* briannesbitt#1800 Fix Lithuanian declensions

* briannesbitt#1800 Update Lithuanian tests

* briannesbitt#1800 Add Lithuanian calendar and ordinal numbers translations

* briannesbitt#1800 Fix Lithuanian year nominative 11+ plural

* briannesbitt#1800 Decline after translations like from_now ones

* briannesbitt#1800 Fix units plural

* briannesbitt#1857 Fix static methods typing autocompletion

* Add recurrences support in Carbon::toPeriod()

* Fix code style

* briannesbitt#1814 Remove error muter

* Fix briannesbitt#1877 Support timezone short offset format

* Nit: Grammar (briannesbitt#1880)

https://writingexplained.org/a-vs-an-difference

* Use static notation for called class

* Allow to get context from macros using self::this()

* Add missing import

* Remove typehint specificity

* Fix isBefore documentation

There was an error in the documentation of the function isBefore.

2018-07-25 12:45:16 is before 2018-07-25 12:45:18.

* Fix briannesbitt#1886 Support slashes in hasFormat()

* briannesbitt#1886 Add hasFormat() tests

* briannesbitt#1889 Fix Belarusian units translations

* briannesbitt#1893 Fix hour translation to година

* briannesbitt#1889 Fix Belarusian aUnit option translations

* briannesbitt#1893 Fix hour Ukrainian units translations

* briannesbitt#1889 Fix Belarusian aUnit option translations tests

* Add translation authors

* Fix briannesbitt#1902 Make hr_BA inherit hr language

* Replace $this with CarbonInterface in PHPDoc (briannesbitt#1904)

* Replace $this with CarbonInterface in PHPDoc
* [PHPStan] Ignore Carbon interface to instances mismatch

* Fix briannesbitt#1910 seconds Serbo-Croatian translation (briannesbitt#1911)

* Fix briannesbitt#1910 seconds Serbo-Croatian translation

* briannesbitt#356 Add toDateTimeLocalString precision parameter

* Add documentation for getTimeFormatByPrecision

* Add $unitPrecision parameters to relevant methods

Add $unitPrecision parameters to:
- toTimeString
- toDateTimeString
- toIso8601ZuluString

* Update PHPDoc

* Fixed all greater examples

Fixed bad one + changed date to have at least 1 true example

* Fix comparison examples

* Improve typings

* Apply fixes from StyleCI (briannesbitt#15)

* Fix typings

* Fix typehint for static create methods (Fix briannesbitt#1914)

* added new CreateFromFormat method to CarbonInterval

* fixed CarbonInterval::createFromFormat() dockblock

added example
fixed return type

* added CarbonInterval::createFromFormat() tests

* fixed tests inputs

* fixed tests testDefaults() and testNulls()

* Add diff translate

* Add option for rounding in forHumans

* Fix code style

* Implement custom interval formats parsing

* Fix too short variable name

* Use customizable factors for forHumans() rounding

* Add test for gap

* Use dedicated method to get the round factor

* Fix briannesbitt#1927 Finnish future interval translations

* Add rounding methods

* Implement FLOOR and CEIL options

* Fix code style

* Remove unused variable creation

* Remove unused method getRoundFactor

* Fix briannesbitt#1929 Indonesian from now translation

* Allow Symfony 5

* Show difference between FLOOR option and no option

* Fix briannesbitt#1908 Korean spacing and aUnit option

* Add 2.x-dev branch alias

* Added diff before_yesterday and after_tomorrow

* briannesbitt#398 Skip tests with easter_days() if ext-calendar is missing

* [Travis-CI] Remove unneeded dev dependency from the install

* Fix briannesbitt#1941 Symfony 5 compatibility

* Fix briannesbitt#1941 Replace Symfony custom rules with inline syntax

* briannesbitt#1941 Set me and scr locales as aliases

* Compare locales handling aliases

* Fix briannesbitt#1943 Add example for H:s.vi (briannesbitt#1944)

* Fix briannesbitt#1943 Add example for H:s.vi

* Fix briannesbitt#1943 Drop support of .v for PHP < 7.3

* Fix test PHP < 7.2 compatibility

* Add test for unit accumulation in interval rounding

* Fix briannesbitt#1949 Use format('T') to match ISO 8601 timezone format (briannesbitt#1950)

* Fix briannesbitt#1951 createFromTimestamp in DST chnge

* briannesbitt#1951 Add test for createFromTimestampMs

* [Travis-CI] Switch to PHP 7.4 stable release

* briannesbitt#1871 Improve translations using CLDR database (briannesbitt#1955)

* Fix briannesbitt#1953 Align Korean translation on CLDR (briannesbitt#1956)

* Fix briannesbitt#1954 Improvement from-now Armenian translation (briannesbitt#1957)

* Fix briannesbitt#1958 Standardize integer timezone input

* Fix briannesbitt#1958 Remove timezone auto-detection

* Fix PHPDoc alignment

* Add timezone unit tests

* Ease tests with OS locales dependency

* Fix briannesbitt#1968 Turkmen year and short days translations

* Added briannesbitt#1966 Tamil before and after translations

* Update Teso unit tests files

* Use CarbonInterface instead of self for Date trait constant as keys

* Speed up resetMessages

* Update PHPMD

* Add strict restrictions for translator requirements

* Fix Symfony 3 compatibility

* Fix month overflow sensibility of quarter tests

* Use common method for translators able to getLocale()

* Add unit tests for NotLocaleAwareException

* Fix method signature compatibility

* Use include instead of require to not stop the process if lang file is missing

* Fix briannesbitt#1970 Uzbek before/after translations

* Cleanup imports

* Fix briannesbitt#1977 Greek "ago" translation

Co-authored-by: Kyle <kylekatarnls@users.noreply.github.com>
Co-authored-by: Hassan Pezeshk <hpez58@gmail.com>
Co-authored-by: Mohamed Sabil <me@mohamedsabil83.com>
Co-authored-by: Gal Jakič <gal.jakic@gmail.com>
Co-authored-by: Arthur Cinader <700572+acinader@users.noreply.github.com>
Co-authored-by: Marc-Antoine Bouchard Marceau <marcantoine.bouchardm@gmail.com>
Co-authored-by: karlyn33 <karlyn33@users.noreply.github.com>
Co-authored-by: spaceemotion <spaceemotion@users.noreply.github.com>
Co-authored-by: silvio-gratani <42873063+silvio-gratani@users.noreply.github.com>
Co-authored-by: dima-bzz <dimabzz@gmail.com>
Co-authored-by: Tyler Collier <github@tylercollier.com>
Co-authored-by: Graham Campbell <GrahamCampbell@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants