Laravel - What's the difference between timestamp() and timestampTz()? - laravel

What's the difference between timestamps() and timestampsTz() methods in the Laravel's Schema Builder? I tried searching google but couldn't find any help.

Basicly timestampsTz() stands for Timestamp with Timezone while timestamps() is like Timestamp without timezone
TimestampsTz() is a representation for a specific point in time. The adjustment to this time is made by the timezone that is related to your system.
The normal timestamps() is more a representation like normal clock.
Have a look at this example:
If you are using the timestamps() function you will also store the timezone that was chosen by your environment. If you are using the timestampsTz() you will not safe the timezone.
I guess it would be good practice to use timestamp without timezone when all your data is for sure in the same zone, or you got another layer over there which handles the time conversion.
Update:
In the Database the normal timestamps() will look like
2004-10-19 10:23:54
while the timestampsTz() looks like
2004-10-19 10:23:54+02
Update 2:
These functions are in Laravel available for every type of Database. But this only differs for PostgreSQL. Have a look at the docs: PostgreSQL Timestamp. In the other databases the timezone information is included in the timestamp automatically.
In all other databases this will have the same output.

timestampsTz() = Adds nullable created_at and updated_at TIMESTAMP (with timezone) equivalent columns.
timestamps() = Adds nullable created_at and updated_at TIMESTAMP equivalent columns.
The difference is that timestampsTz() adds a timezone.

Related

How to convert output timezone per query in sequel ruby?

I wanted to be able to set timezone in each query depending on my user's preferred timezone without adding timezone conversion in each raw sql generated by my application.
I was able to query/retrieve the records in 'Asia/Manila' TZ using this config
Sequel.extension :named_timezones
Sequel.application_timezone = 'Asia/Manila'
Is it possible to set application_timezone per query so that I will pass the current application user's timezone in each request.
You probably want to use Sequel's thread_local_timezones extension: http://sequel.jeremyevans.net/rdoc-plugins/files/lib/sequel/extensions/thread_local_timezones_rb.html
This is per thread, not per query, but should hopefully still work for your needs.
Store everything in UTC and then convert in the UI/presentation layer.

Laravel migration timestamp - Separate data and time

In my migration I have a timestamp which gets the current data and time when a row is posted.
Is it possible to get just the time or just the date from this, or would I have to use a separate timestamp for each?
$table->timestamps();
As DaveCarruthers recommended, use the default timestamps() columns.
Then separate date and time when you need to:
$model->created_at->toDateString();
$model->created_at->toTimeString();

Laravel: How to deal with dates in different timezones

A lot of questions have been asked about this subject. The best answer that I found is this one: How to set local timezone in laravel
So the main rule is to keep all database entries in the same timezone.
But I have a specific case where this answer does not work for me. For some models, I have only a date (no datestamp). Example: suppose that I only store the date of when this question was asked (= 2018-01-25). However in Europe it is already 2018-01-26. Someone has a solution for this?
Changing my date field to a datestamp? What with existing dates?
You can use this library jamesmills/laravel-timezone
OR
If you need custom configuration:
Configure your app timezone to UTC.
'timezone' => 'UTC',
You can store different timezones in database column.
When outputting/displaying dates, just format it to use that timezone.
$timezone = 'America/Vancouver';
$model->created_at->setTimezone($timezone);
created_at and updated_at are automatically converted to carbon instances, which makes this easier. If you have other dates that you're using, add them to the protected $dates array on the model and laravel will convert them to carbon instance too. Then you can use carbons setTimezone() to change the date/time to the timezone.
If you're only talking about a date, then there is no time component and thus time zones are irrelevant. For this reason, most platforms do not have a separate date-with-zone type.
You're correct that not every time zone experiences the same date at all times, and that the start of a date and the end of the date occur at different times in different time zones. However, did you notice that in the prior sentence that I had to use the word "time" to rationalize about these points? :-)
Because date and time zone don't come together without time, there's no purpose in keeping them in the same field. Instead, keep two fields in your model - one for the date, and one for the time zone. In many cases, you may even find they belong in two different models.
As a use case example, consider birthdays. Mine is 1976-08-27. That's all it is - just a date. The time zone of my birth is irrelevant, and so is the time zone I'm located in - until I want to evaluate whether it's currently my birthday (or how long until my birthday, etc.) For those operations, my current time zone is important, and so is the start time-of-day and end time-of-day of that time zone. Thus - two different fields.

TO_TIMESTAMP_TZ Functionality

Any one have any idea how 'TO_TIMESTAMP_TZ' function works internally in oracle.
I want to know how it converts timestamp to timezone
The documentation is very handy for questions like this.
TO_TIMESTAMP_TZ converts a string into a timestamp with timezone information. It doesn't convert something that's already a timestamp into a timestamp with timezone information, without first having a conversion back into a string - which, as I'm sure you're aware, you should always do explicitly.

hibernate JDBC type not found

Does hibernate have any mapping for this oracle data type:(10G)
TIMESTAMP(6) WITH TIME ZONE
I am getting:
No Dialect mapping for JDBC type: -101
My manager does not want to do the: registerHibernateType(-101, Hibernate.getText().getname())
He thinks it is too much.:)
What alternative can I have?
The answer you provide to yourself is more like a workaround than a proper solution. For the sake of the visitors looking for an answer, I'll provide my view on this:
1) Database date-based fields should be always set to UTC, never with a specific timezone. Date calculation with timezone information is an unneeded complexity. Remember that timezones usually changes twice a year for a lot of countries in the world ("daylight saving time"). There's a reason why only a few RDMBS' supports this, and there's a reason why Hibernate developers refuse to support this data-type. The patch for Hibernate is simple enough (one line of code), the implications aren't.
2) Converting your "timestamp with timezone" to a String will only cause problems later. Once you retrieve it as String, you'll need to convert it again to a Date/Calendar object, an unneeded overhead. Not to mention the risks associated with this operation.
3) If you need to know in which timezone is some user, just store the String representing the timezone offset (like "Europe/Prague"). You can use this in Java to build a Calendar with date/time and timezone, as it'll take care of DST for you.
For now, I solved the problem by:
`select TO_CHAR(TRUNC(field)) from table` //field is the one having type= timestamp with timezone
This ensures that when the query returns, the field has datatype 'String'

Resources