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)
- A datetime in one of the supported formats. See lc_datetime_to_clock.
- 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):
- Testcases:
- lang_test__lc_procs, lang_test__lc_timezones
Source code: # # 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) # try { set clock_value [::lc_datetime_to_clock $time_value] } on error {errmsg} { ad_log warning "lc_time_tz_convert: invalid date '$time_value'" return "" } set time_value [clock format $clock_value -format {%Y-%m-%d %H:%M:%S}] 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} { ns_log notice "lc_time_tz_convert: '$time_value' from '$from' to '$to' via Tcl returned:" $errmsg "- use DB-based conversion" # # 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_toXQL Not present: PostgreSQL, Oracle Generic XQL file: packages/acs-lang/tcl/localization-procs.xql