How to convert output timezone per query in sequel ruby? - 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.

Related

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.

Rails 3.1 - timestamps in model and delayed 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

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'

What is the best way to format a date in JSON for Mongo DB storage

I have a date with a time. I'm using ruby, but the language shouldn't matter.
d = "2010-04-01 13:00:00"
What is the best way to format this date for Mongo DB? By 'best' I mean, is there a certain format I could use where Mongo would recognize it as a date and might give me more-advanced filtering optons?
ie: If formatted correctly, could I ask Mongo to return all records whose month is '04'?
Thanks!
You don't need to format dates at all; dates are a supported data type. Each client driver should support dates through their standard date type, including the ruby one.
For advanced queries like your example, you can use a javascript expression for the find specifier:
{"$where": "this.date.getMonth() == 3"}
In ruby you should use a Time instance, which will get stored as the BSON datetime type. You could use a $where clause like Coady mentions, but will be better to do a range query with $lt and $gt - less overhead and can leverage an index.
I don't like relying on mongo Date objects. I think Mongo is slower with 'date' objects than it is with other data types (such as integers).
I tend to use integers (if you need timezone, have a tz field too, then you have localized time):
document = {:some_timestamp => Time.now.to_i}
#collection.find({'some_timestamp' => {'$gte' => Time.now.to_i}})
Sometimes I just use the timestamp built into the BSON::ObjectId's:
id = BSON::ObjectId.from_time(Time.now)
#collection.find({'_id' => {'$lte' => id}})

Storing Dates in Oracle via Hibernate

I'm storing a simple java.util.date in an Oracle XE database via hibernate.
When testing with JUnit if I can retrieve the correct value, I get an error like this:
junit.framework.AssertionFailedError:
expected:<Sun Dec 28 11:20:27 CET 2008>
but was:<2008-12-28 11:20:27.0>
The value is stored in an Oracle Date column (which should have a second-precision) which looks okay to me. Also, I'm surprised that 11:20:27 is not equal to 11:20:27.0. Or does this have to do with timezones?
Any help is welcome.
Thorsten
Okay, worked some more on it ...
Oracle Date columns only store values with an accuracy of a second.
Java Dates do contain milliseconds, but they are typically not printed. So
expected:
was actually created by a date like 11:20:27,345, which is of course not equal to 11:20:27.0
Solution:
either only use full second dates to store and retrieve
or
get hibernate to create the correct Oracle Datatype (TIMESTAMP) - this is very dependent on the dialect specified in the hibernate config (OracleDialect and Oracle10gDialect create different types).
If you compare a java.util.Date to a java.sql.Date that both represent the same instant in time, equals(Object) will return false (it considers two objects of different classes to never be equal).
Your tests need to account for that. The easiest way to do this is to convert the dates to UNIX time (e.g. java.util.Date.getTime()) and compare those values.

Resources