Showing posts with label Setting up TIME ZONE. Show all posts
Showing posts with label Setting up TIME ZONE. Show all posts

Saturday, 13 April 2013

Teradata Timestamp - Part 6 - Time Zone Normalization , AT LOCAL


Inserting in a TIMESTAMP with TIME ZONE column:

When we insert row in the table with TIMESTAMP WITH TIME ZONE column, it is not necessary to specify the time zone as the part of timestamp. The current time zone for the session will be stored with the timestamp with default (thus Time Zone is stored Implicitly).


Say we create a table as below :

CREATE VOLATILE TABLE TEST
 (
 tstamp TIMESTAMP WITH TIME ZONE
 ) ON COMMIT PRESERVE ROWS;

INSERT INTO TEST VALUES (TIMESTAMP '2013-01-01 12:34:56.123456'); --> note that we did not specify the time zone when inserting the timestamp. It will pick up the Timezone from the current setting which is system level 00:00

SET TIME ZONE INTERVAL '05:00' HOUR TO MINUTE ;/* explicit session level setting */

INSERT INTO TEST VALUES (TIMESTAMP '2013-01-01 12:34:56.123457'); --> Here again we don’t specify the time zone when inserting the timestamp. It will pick up the time zone from the current setting which is the 05:00 setting made explicitly for this session

select CAST(tstamp AS CHAR(35)) from test ;

tstamp
2013-01-01 12:34:56.123456+00:00  
2013-01-01 12:34:56.123457+05:00  

From the result we can see that zone is defaulted from the current time zone setting.

When your system uses a TIME ZONE of 00:00 we can think as if your system is located at GMT.
When we change the TIME ZONE we can think that your system moved to a different location which follows the new time zone.


Casting TIMESTAMP with TIME ZONE TO TIMESTAMP column (Time Zone Normalization):

We can cast TIMESTAMP WITH TIME ZONE to TIMESTAMP.

This conversion does not drop the TIME ZONE, instead it incorporates the time zone in the timestamp to give us an absolute value. This process is called as Zone Normalization.

The result of normalization is always relative to the current TimeZone setting. (The current setting could come from System level setting Or user level setting OR session level setting)

Example:

SET TIME ZONE LOCAL; --> Set the time Zone to 00:00

select CAST(tstamp AS timestamp(6)) from test  --> 1/1/2013 12:34:56.123457

Relative to GMT the time is 12:34

SET TIME ZONE INTERVAL '05:00' HOUR TO MINUTE ;/* explicit session level setting */
Here now we change the current time zone to Pakistan's time zone

select CAST(tstamp AS timestamp(6)) from test --> 1/1/2013 17:34:56.123457

Now here we get the relative to Pakistan's timezone. We can see that 5 hours are added to 12:34.

What we are seeing here is that 12:34 in GMT is actually 17:34 is Pakistan.

This is how normalization works.-- Relative to current time zone setting.




Inserting records with explicit time zone.

Earlier we saw that we can insert timestamp fields without writing the time zone explicitly. The system will populate the time zone value from the current time zone setting. The current time zone setting can come from system level setting / user level setting or the session level setting.

However we can also insert records with explicit time zone as shown below.

insert into test values (TIMESTAMP'2013-01-01 12:11:11+06:00');

The Time zone +6:00 is followed by Bangladesh.
We are actually inserting time 12:11:11 in Bangladesh.

Now when we normalize we get the following result:

select CAST(tstamp AS timestamp(6)) from test --> 1/1/2013 06:11:11.000000

We see the time as 06:11:11. What we are seeing currently is the time at GMT ( because my system level setting is 00:00 .i.e GMT) when the time in Bangladesh is 12:11:11

Now I change my session level time zone setting to US CST

SET TIME ZONE INTERVAL -'06:00' HOUR TO MINUTE ;/* explicit session level setting */

And when I see the normalized time zone following is what we get

select CAST(tstamp AS TIMESTAMP(6)) from test ; --> 1/1/2013 00:11:11.000000

Note that the time we are seeing is actually the time in Texas when time in bangladesh is 12:11:11 of the same day.

Now I change the TIME ZONE to Hawaii.

SET TIME ZONE INTERVAL -'11:00' HOUR TO MINUTE ;/* explicit session level setting */

 select CAST(tstamp AS TIMESTAMP(6)) from test ; --> 12/31/2012 19:11:11.000000

Note that when its 12:11:11 of 1st Jan 2013 , its 19:11:11 of 31st Dec 2012 in hawaii.
Note that it actually put us on the previous day.


AT LOCAL option:

AT LOCAL is another technique of normalizing the time zones.
Using AT LOCAL has the same effect as casting we performed earlier.

SET TIME ZONE LOCAL; --> Sets the Time Zone to system level setting which is 00:00 on my system.

select CAST(tstamp AS TIMESTAMP(6)) from test ; --> 1/1/2013 06:11:11.000000

SET TIME ZONE INTERVAL -'11:00' HOUR TO MINUTE ;/* explicit session level setting to hawaii*/

Select tstamp AT LOCAL FROM test; -->12/31/2012 19:11:11.000000
select CAST(tstamp AS TIMESTAMP(6)) from test ; --> 12/31/2012 19:11:11.000000

We can see the result of casting and using AT LOCAL is exactly same.

Teradata Timestamp - Part 5 - Setting up Time Zone


How to set up a time zone?

Time zone can be set up at multiple places:

  1. While setting up the user:

The TIME ZONE can be set while setting the user. This setting is at the user level.

CREATE USER sukul
   TIME ZONE = LOCAL  /* Use the system level setting */
                     = NULL /* No user level default. Either use the system level or the session level setting*/
                     ='08:00' /* explicit setting for time zone . This setting is at system level*/
                     =-':6:00'


  1. Setting at system level

For system level setting we use the DBSCONTROL record in DB window.

MODIFY GENERAL 16=n /* Hour, n=12 to +13*/
MODIFY GENERAL 17=n /* Minutes, n=-59 to +59*/


  1. Setting at session level

SET TIME ZONE LOCAL /* Use system level setting */

SET TIME ZONE USER /* Use user level setting */

SET TIME ZONE INTERVAL '05:00' HOUR TO MINUTE /* explicit session level setting */

Note that when setting the time  zone explicitly we make use of INTERVAL HOUR TO MINUTE.


Example:

select cast(current_timestamp as CHAR(35));
Result :  2013-04-13 05:47:23.470000+00:00  

SET TIME ZONE INTERVAL '05:00' HOUR TO MINUTE ;/* explicit session level setting */

select cast(current_timestamp as CHAR(35));
Result : 2013-04-13 05:47:24.870000+05:00  

SET TIME ZONE LOCAL; /* coming back to local time zone */

Teradata timestamp - Part 4 - TIMESTAMP/TIME with Time Zones:


TIMESTAMP/TIME with Time Zones:


Time Zones are always relative to standard accepted base time zone.
Universally the Greenwich mean time is assigned 00:00 and all time zones are expressed relative to this GMT.

We can see the current zone using the inbuilt functions CURRENT_TIME and CURRENT_TIMESTAMP.

On my system when I execute the query I get following result:

SELECT CURRENT_TIME,CURRENT_TIMESTAMP;

Current Time(0)
        Current TimeStamp(6)
03:00:02
        4/13/2013 03:00:02.026000

I don’t see the Time zone in my result.(this may be specific to my system)  However when I cast the result to characters I get to see the Time Zone

SELECT CAST(CURRENT_TIME AS CHAR(20)),CAST(CURRENT_TIMESTAMP AS CHAR(35))

Current Time(0)
        Current TimeStamp(6)
03:03:03+00:00
              2013-04-13 03:03:03.820000+00:00  






When we want time zone we should use the data type TIME WITH ZONE and TIMESTAMP WITH ZONE .

Both TIME WITH ZONE and TIMESTAMP WITH ZONE may be represented as literals using conventions we have seen previously.

The addition of the time zone adds two bytes of storage to both of these data types,
  • one byte for the hour and
  • one byte for the minute representation of the time zone.

Following is how we represent TIME WITH ZONE and TIMESTAMP WITH ZONE literals.

Literal
Interpreted data type
TIME '12:12:12-08:11'
select TYPE(TIME '12:12:12-08:11' ) --> TIME(0) WITH TIME ZONE
TIME '12:12:12.11121+02:30'
select TYPE(TIME '12:12:12.11121+02:30' ) --> TIME(5) WITH TIME ZONE
TIME '12:12:12.111216+02:30'
select TYPE(TIME '12:12:12.111216+02:30' ) --> TIME(6) WITH TIME ZONE

Note that when we specify TIME WITH TIMEZONE literal we don’t write the word 'WITH TIMEZONE'

Also there is no space on the with side of + or - sign.

Literal
Interpreted data type
TIMESTAMP '2001-01-01 12:12:11+04:30'
select TYPE(TIMESTAMP  '2001-01-01 12:12:11+04:30')--> TIMESTAMP(0) WITH TIME ZONE
TIMESTAMP '2001-01-01 12:12:11.123555+04:30'
select TYPE(TIMESTAMP '2001-01-01 12:12:11.123555+04:30') --> TIMESTAMP(6) WITH TIME ZONE




Following is the number of bytes taken for internal storage by these data types:

TIME(n) WITH TIME ZONE
6 bytes for TIME + 2 bytes for TIMEZONE
TIME --> 6 bytes
TIMEZONE_HOUR --> ByteInt , -12 to 13
TIMEZONE_MINUTE --> ByteInt , 00 to 59
TIMESTAMP(n) WITH TIME ZONE
12 Bytes for TIMESTAMP + 2 bytes for TIMEZONE
TIMESTAMP --> 10 bytes
TIMEZONE_HOUR --> ByteInt , -12 to 13
TIMEZONE_MINUTE --> ByteInt , 00 to 59

The valid range of time zones is from -12:59 to 13:00.