tz_convert - Convert datetime types between time zones in Lasso 8
The project tz_convert and its source files are hosted on GitHub.
[tz_convert] converts datetime types from one time zone locale to another for Lasso 8. This is especially useful for converting datetimes to and from UTC. Using UTC makes performing calculations with datetime types simple because it avoids issues with time zones and daylight savings time.
This tag passes the parameters to the GNU coreutils date command via the custom tag [shell] and Lasso's [os_process]. Compatible with Mac OS X and CentOS and other Linux variants. Windows is not supported at this time. Contributions to this project for Windows are accepted, either as code or by making me an offer I cannot refuse.
tz_convert accepts five parameters.
-dt is required and must be a date type. Its value should have an explicit time specified.
-tzin is optional and must be a valid time zone according to the tz database. The value is a user-friendly time zone representation parameter, containing a country, state, or city, e.g., "America/Los_Angeles". If the time zone is not valid, then the default is UTC. Thus to avoid unexpected results, the developer should validate the time zone prior to passing it into this tag. It is the timezone from which the datetime will be converted.
-tzout is exactly the same as -tzin, except it is the timezone to which the datetime will be converted.
-format is optional. If provided, -format must be an acceptable format according to date in coreutils. Defaults to %F %T, i.e., 2013-03-31 08:00:00. See more formats.
-showcmd is optional. If true, then the command issued to coreutils date will be shown. If false, then it will not be shown. Defaults to false. Useful for debugging your logic.
1 2 3 4 5 6
local('dt' = date('2013-03-31 03:40:12')); local('tzin' = 'America/Los_Angeles'); local('tzout' = 'UTC'); local('format' = '%F %T'); local('showcmd' = true); tz_convert(-dt=#dt, -tzin=#tzin, -tzout=#tzout, -format=#format, -showcmd=#showcmd);
1 2 3 4 5
2013-03-31 10:40:12 Command: export TZ="UTC";/opt/local/bin/gdate -d"TZ=\":America/Los_Angeles\" 2013-03-31 03:40:12" +"%F %T"
Installation and Requirements¶
The repository contains both the tag [tz_convert] in a file named tz_convert.lasso and a directory tz_convert_demo containing the demo. In this directory there is a web page named index.lasso containing the tz_convert tag followed by a web form. The demo includes a jQuery plugin and jQuery itself.
The demo requires [tz_select] in order to render an HTML select menu of timezones, but is not required by [tz_convert]. For more information on [tz_select], read the article tz_select - Display an HTML select menu of time zones with Lasso.
Install and configure the following requirements.
- os_process - See Lasso Language Guide for installation and configuration.
- coreutils - Mac OS X has a useless implementation of date. GNU coreutils must be installed to provide the necessary functionality of date required by this tag.
Download and install Xcode. Requires an App Store or Apple Developer account to download.
After installation of Xcode, go to Xcode > Preferences > Downloads and download and install the Command Line Tools.
Using your favorite package manager for Mac OS X, install coreutils. As of this writing, the preferred package manager is Homebrew, but macports and fink should work just as well.
The tools provided by GNU coreutils are prefixed with the character "g" by default to distinguish them from the BSD commands. For example, date becomes gdate, cp becomes gcp, and ls becomes gls.
(Optional: if you want to use the GNU tools by default without the leading g, then add the directory to the front of your PATH environment variable according to the package manager that you used to install coreutils.)
Configure the value for the location of gdate in tz_convert.lasso. Type which gdate at a shell prompt (Terminal on Mac OS X) to determine its location.
1 2 3 4 5 6
else(#os >> 'Mac'); #gdate = '/usr/local/bin'; // set to your command for GNU date. Type 'which gdate' at a shell prompt (Terminal on Mac OS X) to determine its location. else; // default for Linux OSs #gdate = '/bin/date'; // set to your command for GNU date. /if;
Try using your local demo.
- Sources for Time Zone and Daylight Saving Time Data
- List of tz database time zones
- tz database, also called the zoneinfo database or IANA Time Zone Database
- date formats for GNU coreutils date. Or use man gdate or gdate --help.
A video presentation from PyCon 2012, What you need to know about datetimes, summarizes the complexities of date and time.
Lasso 9 provides a locale for date and other data types, so this tag is not necessary in that version.
Did you like this article? Please send me a Gratipay, as little as 25¢ per week!
Updates, suggestions and comments regarding this article may be sent to Steve Piercy, [email protected] or comment using Disqus.