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.


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);


2013-03-31 10:40:12


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.
  • [shell]
  • 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.

Coreutils Installation

  1. Download and install Xcode. Requires an App Store or Apple Developer account to download.

  2. After installation of Xcode, go to Xcode > Preferences > Downloads and download and install the Command Line Tools.

  3. 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.

  4. 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.

  5. (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.)

  6. 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.

    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.
        // default for Linux OSs
        #gdate = '/bin/date'; // set to your command for GNU date.
  7. Try using your local demo.

More Information

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.

Written by Steve Piercy in Lasso on Sun, May 12, 2013.
Last modified: Sun, Dec 28, 2014
Tags: tz_convert, date, time, convert, time zone, Lasso


comments powered by Disqus