Laravel + Carbon 2 Carbon::parse always uses apps default timezone - laravel

Just upgrading from Laravel 5 to Laravel 6, and with that Carbon is also upgraded to version 2.
Running in to a weird problem with Carbon::parse(). It seems that no matter what I put in as the second argument to parse - the date is always parsed with the apps default timezone (Europe/Stockholm).
$bookingDate = Carbon::parse('2020-08-17 15:00:00', 'Europe/London');
$bookingDate->getTimezone()->getName(); // Europe/Stockholm (mismatch)
However, using Carbon::createFromFormat(), it works as expected:
$bookingDate = Carbon::createFromFormat('Y-m-d H:i:s', '2020-08-17 15:00:00', 'Europe/London');
$bookingDate->getTimezone()->getName(); // Europe/London
This is not a problem when testing in, for example, tinker - as there is no default timezone set in that execution.
So why ->getName() in the first code returns Europe/Stockholm?

You can parse using this:
Carbon::parse($value)->format('d/m/Y');

Related

Laravel - Carbon giving wrong time when using shiftTimeZone and setTimeZone

Laravel carbon is returning time in UTC when printing Carbon::now() but doesn't give the correct time while using shiftTimezone and setTimezone
Below are the results:
Carbon::now(); //"2021-07-20T07:30:29.775871Z"
Carbon::now()->timezoneName; //UTC
Carbon::now()->shiftTimezone('Asia/Kolkata'); //"2021-07-20T02:00:29.452997Z"
Carbon::now()->shiftTimezone('+05:30'); //"2021-07-20T02:00:29.452997Z"
Carbon::now()->shiftTimezone('Asia/Kolkata'); //"2021-07-20T07:30:29.775871Z"
In case of shiftTimeZone, it should add 05:30 hours to the time. But what it actually does is reduces 05:30 from the UTC time.
And for setTimezone it is taking the same time as UTC.
Any idea where I'm getting it wrong?
In my application, I never use shiftTimezone or setTimezone.
I always go with something like the following:
Carbon::now()->timezone('Asia/Kolkata');
It returns me the date in Asia/Kolkata timezone, without any issue.
What you put in comments here is not the content of the instance, it's an UTC ISO-8601 string as it's converted to be rendered in JSON output for instance:
echo Carbon::now()->format('Y-m-d H:i:s.u p'); // 2021-07-20 09:36:08.596951 Z
echo Carbon::now()->shiftTimezone('Asia/Kolkata')->format('Y-m-d H:i:s.u p'); // 2021-07-20 09:36:08.596951 +05:30
echo Carbon::now()->shiftTimezone('+05:30')->format('Y-m-d H:i:s.u p'); // 2021-07-20 09:36:08.596951 +05:30
echo Carbon::now()->shiftTimezone('Asia/Kolkata')->format('Y-m-d H:i:s.u p'); // 2021-07-20 09:36:08.596951 +05:30
JSON is converted to UTC because it's a standard way to exchange datetime information, timezone should be a separated information and you only need it to force a timezone, but 99% you want the user/client/browser to use its own timezone so new Date('2021-07-20T09:36:08.596951Z') in JavaScript or equivalent in other languages will work just fine and apply the current device timezone, which is the correct way for proper localization. Very specific cases where you want to display a date in a timezone that IS NOT the user device timezone, then you should pass "Asia/Kolkata" explicitly in your JSON output.

Does anyone know what TimezoneOffset does on LuisPredictionOptions?

I'm sending LUIS a query that is based on a time value (e.g. "what is the time 10 minutes from now" - just an example). I want the time to come back in the local timezone, so on the LuisPredictionOptions object (C#) I set the TimezoneOffset (as an example I set it to 2 hours ahead, or 120 minutes).
In Fiddler I can see when it calls the LUIS endpoint it's correctly adding "timezoneOffset=120.0".
However, the timezone comes back as UTC - it doesn't matter whether the timezoneOffset is set, or even what it is set to, the time always comes back UTC, using the builtin datetimeV2 entity.
Does anyone know what the TimezoneOffset property is for? Am I just using it incorrectly? Is there another way perhaps to get a local time from LUIS?
[Update]: Here are some examples: https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/[AppId]?verbose=true&timezoneOffset=0&subscription-key=[subscriptionkey]&q=/luis/v2.0/apps/c1be57f4-3850-489e-8266-db376b82c011?timezoneOffset=120&log=true
https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/[AppId]?verbose=true&timezoneOffset=0&subscription-key=[subscriptionkey]&q=/luis/v2.0/apps/c1be57f4-3850-489e-8266-db376b82c011?timezoneOffset=240&log=true
and I'm trying the following example utterance: "in 10 minutes".
When I do this, the timex is in UTC (e.g. timex=2020-01-11T16:08:25) and the "value" comes back with the same value, minus the "T", as follows: value=2020-01-11 16:08:25
I could understand perhaps if the timex is in UTC, but then possibly "value" should be adjusted by the timezoneOffset?
It looks like there's an incorrect question mark in your URL, right before timezoneOffset.
Using the same query I was able to get the expected behavior, where the returned value is different by 10 minutes.
Which SDK are you using? Perhaps you're using the V3 Runtime SDK which uses the V3 endpoint that doesn't use timeZoneOffset but instead uses datetimeReference, and need to use the V2 Runtime SDK instead.
https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/[app-id]?verbose=true&timezoneOffset=10&subscription-key=[key]&q=in 10 minutes
The TimeZoneInfo class's FindSystemTimeZoneById method can be used to determine the correct timezoneOffset based on system time. An example in C# is shown below:
// Get CST zone id
TimeZoneInfo targetZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
// Get local machine's value of Now
DateTime utcDatetime = DateTime.UtcNow;
// Get Central Standard Time value of Now
DateTime cstDatetime = TimeZoneInfo.ConvertTimeFromUtc(utcDatetime, targetZone);
// Find timezoneOffset
int timezoneOffset = (int)((cstDatetime - utcDatetime).TotalMinutes);
Reference:
https://learn.microsoft.com/en-us/azure/cognitive-services/luis/luis-concept-data-alteration?tabs=V2#c-code-determines-correct-value-of-timezoneoffset

Unknown setter '_date_time' Carbon / Laravel 4

I have my installation of laravel 4 under HHVM, and i set up as a accessory on my eloquent an converter date from SQL date to human.
doing something like that:
public function getDateSpanAttribute($value)
{
return $value = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s',$this->created_at)->diffForHumans();
}
This method work well under php5, no error just work. On HHVM this accessory throw me an exception saying,
{"error":{"type":"InvalidArgumentException","message":"Unknown setter
'_date_time'","file":"/var/www/mynextmatch/vendor/nesbot/carbon/src/Carbon/Carbon.php","line":542}}
I saw the source of Carbon on that row and it is the magic method __get() to give this problem but i cannot figure out why it happening.
I will appreciate same help. Thanks
Not sure about the error on HHVM but one thing is for sure that an accessor method doesn't contain any argument, it should be (Manual):
public function getDateSpanAttribute()
{
return \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $this->created_at)->diffForHumans();
}
You just need to return the property not setting anything, if you want to set anything then it would be a setter/mutator method which acepts a value as argument.
I am having the same issue but think mine is tied to creating a session creation after logging in (
InvalidArgumentException Unknown setter '_date_time'). Mine is installed on HHVM 3.1 running on Ubuntu 14.04. I have the same set up using Ubuntu 12.04 and am not having this issue.
10. InvalidArgumentException
…/­vendor/­nesbot/­carbon/­src/­Carbon/­Carbon.php542
9. Carbon\Carbon __set
<#unknown>0
8. DateTime __sleep
<#unknown>0
7. serialize
…/­vendor/­laravel/­framework/­src/­Illuminate/­Session/­Store.php222
6. Illuminate\Session\Store save
…/­vendor/­laravel/­framework/­src/­Illuminate/­Session/­Middleware.php126

Rails change default timezone

I'm trying to change default timezone in Rails 3.2.8 app to GMT+04:00, but I don't know how to do it. The following solutions do not work for me:
config.time_zone = 'Moscow'
config.time_zone = "(GMT+04:00) Moscow"
config.active_record.default_timezone = 'Moscow'
config.active_record.default_timezone = :local
Also I've tried in rails console the following:
ActiveSupport::TimeZone.all.map(&:name)
which returned a list of values, including "Moscow".
Time.zone returned (GMT+00:00) UTC which is not correct, it should be (GMT+04:00) UTC. Then I changed Time.zone = "Moscow" and Time.now returned the correct value (... +0400).
So to fix it I simply used Time.now + 4.hour, but I also need datetime_select to display my local time. Time.now + 4.hour is not a correct solution.
How one can set default time zone to their local value?
All of a sudden I've localized the problem.
I run Win7 and my WEBrick server is showing the correct time, but the project itself shows a wrong one. In the same time, my production server is working correctly, so, obviously, the problem is with the OS.
UPD: The problem is fixed. I should have restarted the WEBrick server after changing the application.rb file.

issues with date conversion, using rails 3.0.7 and ruby 1.9.2

i am using datepicker to insert date on my form, which of course returns date to the controller as a string.
the issue is when I try to convert this string to date, I get an error "invalid date"
Date.parse(params[:abc][:date])
To verify if I'm doing it right, I wrote the following ruby program:
string_date = "06/18/2011"
date = Date.parse(string_date)
puts date
This works perfectly fine. But when I try the same thing in my rails controller, it gives invalid date error.
please help.
When I do this in my Rails 3.1 console:
date = Date.parse("06/18/2011")
I get an "ArgumentError: invalid date" exception. However, it works fine with an ISO-8601 date:
date = Date.parse('2011-06-18')
So perhaps you're having a locale problem. Your script could be using your standard locale setting (which is probably some US locale judging by the date format) but your server is probably using something else.
Try changing the jQuery datepicker date format to something standard and unambiguous by adding this:
dateFormat: 'yy-mm-dd'
to the datepicker's options. The datepicker's default is 'mm/dd/yy' and Ruby's Date class doesn't like seem to like it without a special locale setting.

Resources