Access to UTC time with severs running in local time

Ask a Question related to Informix, Design and Development.

  1. #1

    Default Access to UTC time with severs running in local time

    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.


    Thanks you.
    Pablo Guest

  2. Similar Questions and Discussions

    1. utc to local time
      How can I convert a utc time stamp to a perl standard time stamp for my timezone? Paul Kraus ----------------------- PEL Supply Company...
    2. local date/time.
      Does any one know, having retrieved a utc date/time from a field, whether one may use ESQL (infx) functions to convert it to local time (that is...
    3. Access to UTC time with servers running in local time
      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...
    4. GMT time to local server time
      Yes, DATEADD() and DATEDIFF() You can calculate the delta from GMT by saying: DECLARE @delta TINYINT SELECT @delta = DATEDIFF(HOUR, GETDATE(),...
  3. #2

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

    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.
    > "Jonathan Leffler" <jleffler@earthlink.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: [email]jleffler@earthlink.net[/email], [email]jleffler@us.ibm.com[/email]
    Guardian of DBD::Informix v2003.04 -- [url]http://dbi.perl.org/[/url]

    Jonathan Leffler Guest

  4. #3

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

    "Jonathan Leffler" <jleffler@earthlink.net> wrote in message
    news:hPf2b.2252$Jh2.1424@newsread4.news.pas.earthl ink.net...
    > 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;
    ================================================== ======
    > > "Jonathan Leffler" <jleffler@earthlink.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: [email]jleffler@earthlink.net[/email], [email]jleffler@us.ibm.com[/email]
    > Guardian of DBD::Informix v2003.04 -- [url]http://dbi.perl.org/[/url]
    >

    Catfish Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139