lc_time_tz_convert (public)

 lc_time_tz_convert -from from -to to -time_value time_value

Defined in packages/acs-lang/tcl/localization-procs.tcl

Converts a date from one timezone to another.

Switches:
-from
(required)
-to
(required)
-time_value
(required)
Timestamp in the 'from' timezone, in the ISO datetime format ("%Y-%m-%d %H:%M:%S" as per Tcl clock api). Some seemingly invalid dates such as "2000-00-00 00:00:00" may be accepted and normalized to a valid date, also according to he behavior of the Tcl clock api.
Returns:
Timestamp in the 'to' timezone, also in ISO datetime format, or the empty string when 'time_value' or one of the timezones are invalid, or when it is otherwise impossible to determine the right conversion.
See Also:

Partial Call Graph (max 5 caller/called nodes):
%3 test_lang_test__lc_procs lang_test__lc_procs (test acs-lang) lc_time_tz_convert lc_time_tz_convert test_lang_test__lc_procs->lc_time_tz_convert test_lang_test__lc_timezones lang_test__lc_timezones (test acs-lang) test_lang_test__lc_timezones->lc_time_tz_convert ad_log ad_log (public) lc_time_tz_convert->ad_log db_string db_string (public) lc_time_tz_convert->db_string fs::rss::datasource fs::rss::datasource (private) fs::rss::datasource->lc_time_tz_convert lc_time_conn_to_system lc_time_conn_to_system (public) lc_time_conn_to_system->lc_time_tz_convert lc_time_local_to_utc lc_time_local_to_utc (public) lc_time_local_to_utc->lc_time_tz_convert lc_time_system_to_conn lc_time_system_to_conn (public) lc_time_system_to_conn->lc_time_tz_convert lc_time_utc_to_local lc_time_utc_to_local (public) lc_time_utc_to_local->lc_time_tz_convert

Testcases:
lang_test__lc_procs, lang_test__lc_timezones
Source code:
    try {
        #
        # Here we enforce that the timestamp format is correct and
        # apply Tcl clock date normalization (e.g. 2000-00-00 00:00:00
        # -> 1999-11-30 00:00:00) so that the behavior is consistent
        # across DBMSs)
        #
        set clock_value [clock scan $time_value -format {%Y-%m-%d %H:%M:%S}]
        set time_value [clock format $clock_value -format {%Y-%m-%d %H:%M:%S}]
    } on error {errmsg} {
        ad_log warning "lc_time_tz_convert: invalid date '$time_value'"
        return ""
    }

    try {
        #
        # Tcl-based conversion
        #
        # Tcl clock api can perform timezone conversion fairly easy,
        # with the advantage that we do not have to maintain a local
        # timezones database, including daylight savings, to get a
        # correct and consistent result.
        #
        set clock_local [clock scan $time_value -format {%Y-%m-%d %H:%M:%S} -timezone $from]
        set clock_gmt [clock scan $clock_local -format %s -gmt 1]
        set date_to [clock format $clock_gmt -format {%Y-%m-%d %H:%M:%S} -timezone $to]
    } on error {errmsg} {
        ad_log notice  "lc_time_tz_convert: '$time_value' from '$from' to '$to' via Tcl returned:"  $errmsg

        #
        # DB-based conversion
        #
        # The typical Tcl installation will not deal with
        # non-canonical timezones, but we may have this
        # information in the ref-timezones datamodel. When the Tcl
        # conversion fails, we try this approach instead.
        #
        set date_to [db_string tz_convert {
            with gmt as
            (
             select cast(:time_value as timestamp) -
                    cast(r.gmt_offset || ' seconds' as interval) as time
               from timezones t, timezone_rules r
              where t.tz_id = r.tz_id
                and :time_value between r.local_start and r.local_end
                and t.tz = :from
             )
            select to_char(gmt.time + cast(r.gmt_offset || ' seconds' as interval),
                           'YYYY-MM-DD HH24:MI:SS')
              from timezones t, timezone_rules r, gmt
             where t.tz_id = r.tz_id
               and gmt.time between r.utc_start and r.utc_end
               and t.tz = :to
        } -default ""]
    }

    return $date_to
XQL Not present:
PostgreSQL, Oracle
Generic XQL file:
packages/acs-lang/tcl/localization-procs.xql

[ hide source ] | [ make this the default ]
Show another procedure: