Rails 3.1 - timestamps in model and delayed time - time

I have generated a model in my Rails app (3.1) and I have a problem about saving the time into the DB table (through timestamps). The time is delayed of one hour. Exist any global solution of set up the time for timestamps?
Thanks in advance

I think the timestamps will always be saved in the database in UTC format. It's when you display them that they should be translated in the desired timezone.
To change the default timezone of your app: config.time_zone = "UTC".
You can find all timezones here: http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html

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 - What's the difference between timestamp() and timestampTz()?

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.

Catalog Price Rules are set in the past

I have a very weird problem with Catalog Price Rules. As you know normally when price rules are applied, they are applied for current day, past day, and next day.
The problem is that the price rules are generated only for current day, and two past days.
So if taking today example on catalogrule_product_price on rule_date row the dates set are:
2014-03-12
2014-03-13
2014-03-14 - today date
Instead of:
2014-03-13
2014-03-14 - today date
2014-03-15 - tomorrow
Any suggestions?
This happens when your database is using a different time zone to your Magento site. You need to check and verify that they are both using the same time zone.
For Magento you can check this from System -> Configuration -> Locale Options.
For your database, you can run the following query to the the current local time: SELECT NOW();
If these don't match then you will get the behaviour described above (rules are not correctly set +/- current day). Most likely you need to correct the timezone that your database is using.
Edit: it seems that the best way to handle this is to ensure that you set the global timezone to UTC in Magento, and then set the timezone for your individual site(s) to the local time in your region. Your database should also be set to default to UTC.
Edit 2: Also make sure that the default locale set in app/etc/config.xml is correct.
Could it be related to time zones? Either the time zone of your server clock or the time zone set in Magento?
Try setting your Default Store Timezone to match your server timezone.

Mongoid DateTime: What is the right date?

I'm using Mongoid to store DateTime. But now i'm confusing with the real date.
In mongodb , the date is stored as:
{"2013-01-14T12:50:00.000Z"}
But when i print that value, it says:
2013-01-14T19:50:00+07:00
I don't really understand whether those Date formats are the same, and which one is "right" in my current timezone ?
Thank you for your help.
Date is stored in GMT, when "printed", it is displayed in your local timezone (GMT+7?)
The default Ruby date object should be able to handle offsets in time:
http://ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/Date.html
Whereby some way down the page it even talks about how to start manipulating it I believe:
An optional argument the offset indicates the difference between the local time and UTC.
I do believe that mongoid is already converting the time for you as can be seen by the T value within the iso date being 7 hours ahead:
2013-01-14T19:50:00+07:00
Merely if you were to print the date and/or time instead of the full output with the offset included I have no doubt you will get the real date.
I believe mongoid most likely prints the offset even when it is applied because that offset IS there (since the time is off-setted by 7 hours from UTC) it is just not applied further.

Display DateTime data in users TimeZone in DataTable

We have a reporting application where all DateTimes are stored in UTC in the database (SQL server 2005)
The reporting application works simply be retrieving a DataTable and binding to a DataGrid displaying the information.
If we know the logged in users time zone (e.g. +3 GMT), is there any way to update the DataTables dates which are in UTC to now display in the users time zone?
We know we can spool thorugh each column/row in the DataTable and do a conversion but is there a better more efficient way?
I would modify the time as it is displayed to the user. Keep how it is stored consistent, but also store what their timezone is, and then modify the UI to show the correct local time. For example, if their time zone is Central Standard Time, then you could use something similar to the following code:
SomeDataObject someDataObject = new SomeDataObject();
someDataObject.TimeZone = -5; //UTC Timezone for Central Standard Time
someDataObject.Time = DateTime.Now;
DateTime someTime = someDataObject.Time;
someTime.Add(someDataObject.TimeZone); // Display this back to the user
If using .NET, there's a built in class that was made precisely for this purpose. TimeZoneInfo (http://msdn.microsoft.com/en-us/library/system.timezoneinfo.aspx) as taken from the article:
A TimeZoneInfo object can represent any time zone, and methods of the TimeZoneInfo class can be used to convert the time in one time zone to the corresponding time in any other time zone. The members of the TimeZoneInfo class support the following operations:
Retrieving a time zone that is already defined by the operating system.
Enumerating the time zones that are available on a system.
Converting times between different time zones.
Creating a new time zone that is not already defined by the operating system.
Serializing a time zone for later retrieval.
Hope this helps.

Resources