dbTalk Databases Forums  

Re: Access to UTC time with servers running in local time

comp.databases.informix comp.databases.informix


Discuss Re: Access to UTC time with servers running in local time in the comp.databases.informix forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Jonathan Leffler
 
Posts: n/a

Default Re: Access to UTC time with servers running in local time - 08-24-2003 , 10:58 PM






Catfish wrote:
Quote:
Why not write a stored procedure? You can run 'SYSTEM date -u' and
add your format commands or run a short script. The 'date -u'
command returns GMT. I've been doing something like this in my
procs for years as procs return the start and not current time (in
7.31). If required, have the proc return the time then you can run
it in your SQL code.
The only issue there is how do you get the value from the system
command into the database server? There is no way to retrieve the
information directly in the SP, so you would have to insert it into a
table. Since the insertion would be done by a separate process, you
can't use a temp table - it has to be a permanent table. And then you
start running into a variety of concurrency issues. How do you ensure
you get the correct time?

Yes, it can be done that way. I don't think it is all that easy,
though. OTOH, it does stay firmly on the server, so it is probably
easier than what I was outlining.

Quote:
"Jonathan Leffler" <jleffler (AT) earthlink (DOT) net> wrote:
Pablo wrote:
I am running server under Linux.

Can I get the UTC time in servers with SQL sentences ?

The 'current' and 'today' options always return local time, this
server has several databases with differents time zones and I need to
obtain UTC time, set the environment variable TZ=UTC+0 is not possible
due to I'm not DBA.

I've scratched my head on this, and I don't think it can be done
trivially. If you have a programming language (eg I4GL) and you know
your own time zone offset from UTC, then you can do it by calculating:

1. In your program, find your local current time.
2. Given that and your time zone offset, calculate the current UTC.
3. Get the server to tell you what it thinks the time is:
SELECT CURRENT YEAR TO SECOND FROM SysTables WHERE Tabid = 1;
4. Use that and the current UTC to determine the server's time zone.

With that in place, you can now get the server to calculate the UTC
for you. Remember that the machines may not be synchronized with NTP
or SNTP, so allow for drifting clocks.

It's simpler simply to know what the server's time zone is.

If you only have DB-Access, then the only way to do it, I think, is to
know what the server's time zone is -- or know what UTC is on your
client-side. Actually, that can be done pretty simply; you can
conflate steps 1 and 2 if your time zone is (temporarily) UTC; run
your program with TZ=UTC0 in the environment. Beware 'spring forward,
fall back', as they say here in the USA.

--
Jonathan Leffler #include <disclaimer.h>
Email: jleffler (AT) earthlink (DOT) net, jleffler (AT) us (DOT) ibm.com
Guardian of DBD::Informix v2003.04 -- http://dbi.perl.org/



Reply With Quote
  #2  
Old   
Catfish
 
Posts: n/a

Default Re: Access to UTC time with servers running in local time - 08-25-2003 , 08:57 PM






"Jonathan Leffler" <jleffler (AT) earthlink (DOT) net> wrote

Quote:
Catfish wrote:
Why not write a stored procedure? You can run 'SYSTEM date -u' and
add your format commands or run a short script. The 'date -u'
command returns GMT. I've been doing something like this in my
procs for years as procs return the start and not current time (in
7.31). If required, have the proc return the time then you can run
it in your SQL code.

The only issue there is how do you get the value from the system
command into the database server? There is no way to retrieve the
information directly in the SP, so you would have to insert it into a
table. Since the insertion would be done by a separate process, you
can't use a temp table - it has to be a permanent table. And then you
start running into a variety of concurrency issues. How do you ensure
you get the correct time?

Yes, it can be done that way. I don't think it is all that easy,
though. OTOH, it does stay firmly on the server, so it is probably
easier than what I was outlining.
It's not hard. Just how bad do you need this time.
You're correct about using a table to transfer the datetime.
Here's the code. However, embedding the proc in SQL is not for large reads.
For the large reads, store the datetime in a temp table.
================================================== ======
## /opt/informix/scripts/gmt_dtime.sh
#!/bin/sh
GMTdtime=`date -u "+%Y-%m-%d %H:%M:%S"`
dbaccess workdb - <<! > /dev/null 2>&1
set lock mode to wait;
update wk_gmtdtime set gmtdtime = "$GMTdtime";
!
================================================== ======
create table wk_gmtdtime (gmtdtime datetime year to second);

insert into wk_gmtdtime values (current);

create procedure sp_gmtdtime()
returning datetime year to second;
define v_gmtdtime datetime year to second;
system "/opt/informix/scripts/gmt_dtime.sh";
select gmtdtime into v_gmtdtime from wk_gmtdtime;
return v_gmtdtime;
end procedure;

select sp_gmtdtime() gmt from systables where tabid = 1;
================================================== ======
Quote:
"Jonathan Leffler" <jleffler (AT) earthlink (DOT) net> wrote:
Pablo wrote:
I am running server under Linux.

Can I get the UTC time in servers with SQL sentences ?

The 'current' and 'today' options always return local time, this
server has several databases with differents time zones and I need to
obtain UTC time, set the environment variable TZ=UTC+0 is not possible
due to I'm not DBA.

I've scratched my head on this, and I don't think it can be done
trivially. If you have a programming language (eg I4GL) and you know
your own time zone offset from UTC, then you can do it by calculating:

1. In your program, find your local current time.
2. Given that and your time zone offset, calculate the current UTC.
3. Get the server to tell you what it thinks the time is:
SELECT CURRENT YEAR TO SECOND FROM SysTables WHERE Tabid = 1;
4. Use that and the current UTC to determine the server's time zone.

With that in place, you can now get the server to calculate the UTC
for you. Remember that the machines may not be synchronized with NTP
or SNTP, so allow for drifting clocks.

It's simpler simply to know what the server's time zone is.

If you only have DB-Access, then the only way to do it, I think, is to
know what the server's time zone is -- or know what UTC is on your
client-side. Actually, that can be done pretty simply; you can
conflate steps 1 and 2 if your time zone is (temporarily) UTC; run
your program with TZ=UTC0 in the environment. Beware 'spring forward,
fall back', as they say here in the USA.


--
Jonathan Leffler #include <disclaimer.h
Email: jleffler (AT) earthlink (DOT) net, jleffler (AT) us (DOT) ibm.com
Guardian of DBD::Informix v2003.04 -- http://dbi.perl.org/




Reply With Quote
  #3  
Old   
Michael Mueller
 
Posts: n/a

Default Re: Access to UTC time with servers running in local time - 08-26-2003 , 06:58 AM




There is an undocumented way of getting the server's utc time in seconds
just like the time() system call on the server would return it:

select dbinfo( 'utc_current') from systables where tabid = 99

(There also is a dbinfo('utc_to_datetime', <some_utc_int>) to convert it
to a string. But unfortunately it uses local time).

If you want it in ascii and are willing to use esqlc, you could do this:

#include <time.h>

main()
{
struct tm *tm;

exec sql begin declare section;
int tim;
exec sql end declare section;

exec sql whenever error stop;
exec sql database sysmaster;

exec sql select dbinfo( 'utc_current') into :tim
from systables where tabid = 99;
printf("utc time %d\n", tim);
tm = gmtime((time_t *)&tim);
printf("%s\n", asctime(tm));
}


Michael



Jonathan Leffler wrote:
Quote:
Pablo wrote:


I am running server under Linux.

Can I get the UTC time in servers with SQL sentences ?

The 'current' and 'today' options always return local time, this
server has several databases with differents time zones and I need to
obtain UTC time, set the environment variable TZ=UTC+0 is not possible
due to I'm not DBA.



I've scratched my head on this, and I don't think it can be done
trivially. If you have a programming language (eg I4GL) and you know
your own time zone offset from UTC, then you can do it by calculating:

1. In your program, find your local current time.
2. Given that and your time zone offset, calculate the current UTC.
3. Get the server to tell you what it thinks the time is:
SELECT CURRENT YEAR TO SECOND FROM SysTables WHERE Tabid = 1;
4. Use that and the current UTC to determine the server's time zone.

With that in place, you can now get the server to calculate the UTC
for you. Remember that the machines may not be synchronized with NTP
or SNTP, so allow for drifting clocks.

It's simpler simply to know what the server's time zone is.

If you only have DB-Access, then the only way to do it, I think, is to
know what the server's time zone is -- or know what UTC is on your
client-side. Actually, that can be done pretty simply; you can
conflate steps 1 and 2 if your time zone is (temporarily) UTC; run
your program with TZ=UTC0 in the environment. Beware 'spring forward,
fall back', as they say here in the USA.


--

=== Michael Mueller ==================
Tel. + 49 8171 63600
Fax. + 49 8171 63615
Web: http://www.mm.kay-mueller.de
http://www.planets.kay-mueller.de
======================================

sending to informix-list


Reply With Quote
  #4  
Old   
Michael Mueller
 
Posts: n/a

Default Re: Access to UTC time with servers running in local time - 08-26-2003 , 07:11 AM




Just for the sake of completeness:

There also is a dbinfo( 'get_tz') which behaves a bit funny. If the TZ
environment variable in the client is not set it returns the server's
string value for TZ. If TZ is set in the client, it returns the client's
own value.

If your server uses TZ to set the time zone, you can get it this way.
But many operating systems have other means than TZ to set the time zone
system wide (but may also support TZ settings).

Michael

Michael Mueller wrote:
Quote:
There is an undocumented way of getting the server's utc time in seconds
just like the time() system call on the server would return it:

select dbinfo( 'utc_current') from systables where tabid = 99

(There also is a dbinfo('utc_to_datetime', <some_utc_int>) to convert it
to a string. But unfortunately it uses local time).

If you want it in ascii and are willing to use esqlc, you could do this:

#include <time.h

main()
{
struct tm *tm;

exec sql begin declare section;
int tim;
exec sql end declare section;

exec sql whenever error stop;
exec sql database sysmaster;

exec sql select dbinfo( 'utc_current') into :tim
from systables where tabid = 99;
printf("utc time %d\n", tim);
tm = gmtime((time_t *)&tim);
printf("%s\n", asctime(tm));
}


Michael



Jonathan Leffler wrote:

Pablo wrote:


I am running server under Linux.

Can I get the UTC time in servers with SQL sentences ?

The 'current' and 'today' options always return local time, this
server has several databases with differents time zones and I need to
obtain UTC time, set the environment variable TZ=UTC+0 is not possible
due to I'm not DBA.




I've scratched my head on this, and I don't think it can be done
trivially. If you have a programming language (eg I4GL) and you know
your own time zone offset from UTC, then you can do it by calculating:

1. In your program, find your local current time.
2. Given that and your time zone offset, calculate the current UTC.
3. Get the server to tell you what it thinks the time is:
SELECT CURRENT YEAR TO SECOND FROM SysTables WHERE Tabid = 1;
4. Use that and the current UTC to determine the server's time zone.

With that in place, you can now get the server to calculate the UTC
for you. Remember that the machines may not be synchronized with NTP
or SNTP, so allow for drifting clocks.

It's simpler simply to know what the server's time zone is.

If you only have DB-Access, then the only way to do it, I think, is to
know what the server's time zone is -- or know what UTC is on your
client-side. Actually, that can be done pretty simply; you can
conflate steps 1 and 2 if your time zone is (temporarily) UTC; run
your program with TZ=UTC0 in the environment. Beware 'spring forward,
fall back', as they say here in the USA.




--

=== Michael Mueller ==================
Tel. + 49 8171 63600
Fax. + 49 8171 63615
Web: http://www.mm.kay-mueller.de
http://www.planets.kay-mueller.de
======================================

sending to informix-list


Reply With Quote
  #5  
Old   
Jonathan Leffler
 
Posts: n/a

Default Re: Access to UTC time with servers running in local time - a server-onlyanswer! - 08-27-2003 , 12:34 AM



Michael Mueller wrote:

Quote:
Just for the sake of completeness:

There also is a dbinfo( 'get_tz') which behaves a bit funny. If the TZ
environment variable in the client is not set it returns the server's
string value for TZ. If TZ is set in the client, it returns the client's
own value.

If your server uses TZ to set the time zone, you can get it this way.
But many operating systems have other means than TZ to set the time zone
system wide (but may also support TZ settings).

I wasn't aware of the get_tz option - I live, I learn. I'm not sure
it is all that much use, but that's a separate discussion.

The dbinfo('utc_current') information (which I'd forgotten about)
points the way to do it in the server, though. There's an SPL
function in the IIUG Software Archive with a name like
unixtime_to_datetime. It converts an integer number of seconds since
the Epoch (1970-01-01 00:00:00 +00:00) into a datetime year to second
- in UTC. You can also call CURRENT YEAR TO SECOND to get the
server's local time. The difference - an interval DAY(n) TO SECOND -
is the time zone the server is running in; that's the information we
wanted. About the only thing to watch, IIRC, is that the value of
dbinfo('utc_current') varies during the execution of a statement, but
the value of CURRENT does not. If your statement is long running,
that could skew the difference. If you want an INTERVAL HOUR TO
MINUTE, then you need to add the subtraction to 'INTERVAL(0:0) HOUR TO
MINUTE' - and worry about rounding. The zero interval must be on the
LHS of the addition. If you want a different interval (e.g. INTERVAL
MINUTES(4) TO MINUTE), change the constant.

Thanks for the help, Michael!


Quote:
Michael Mueller wrote:
There is an undocumented way of getting the server's utc time in seconds
just like the time() system call on the server would return it:

select dbinfo( 'utc_current') from systables where tabid = 99

(There also is a dbinfo('utc_to_datetime', <some_utc_int>) to convert it
to a string. But unfortunately it uses local time).

If you want it in ascii and are willing to use esqlc, you could do this:

#include <time.h

main()
{
struct tm *tm;

exec sql begin declare section;
int tim;
exec sql end declare section;

exec sql whenever error stop;
exec sql database sysmaster;

exec sql select dbinfo( 'utc_current') into :tim
from systables where tabid = 99;
printf("utc time %d\n", tim);
tm = gmtime((time_t *)&tim);
printf("%s\n", asctime(tm));
}


Michael



Jonathan Leffler wrote:

Pablo wrote:


I am running server under Linux.

Can I get the UTC time in servers with SQL sentences ?

The 'current' and 'today' options always return local time, this
server has several databases with differents time zones and I need to
obtain UTC time, set the environment variable TZ=UTC+0 is not possible
due to I'm not DBA.




I've scratched my head on this, and I don't think it can be done
trivially. If you have a programming language (eg I4GL) and you know
your own time zone offset from UTC, then you can do it by calculating:

1. In your program, find your local current time.
2. Given that and your time zone offset, calculate the current UTC.
3. Get the server to tell you what it thinks the time is:
SELECT CURRENT YEAR TO SECOND FROM SysTables WHERE Tabid = 1;
4. Use that and the current UTC to determine the server's time zone.

With that in place, you can now get the server to calculate the UTC
for you. Remember that the machines may not be synchronized with NTP
or SNTP, so allow for drifting clocks.

It's simpler simply to know what the server's time zone is.

If you only have DB-Access, then the only way to do it, I think, is to
know what the server's time zone is -- or know what UTC is on your
client-side. Actually, that can be done pretty simply; you can
conflate steps 1 and 2 if your time zone is (temporarily) UTC; run
your program with TZ=UTC0 in the environment. Beware 'spring forward,
fall back', as they say here in the USA.






--
Jonathan Leffler #include <disclaimer.h>
Email: jleffler (AT) earthlink (DOT) net, jleffler (AT) us (DOT) ibm.com
Guardian of DBD::Informix v2003.04 -- http://dbi.perl.org/



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.