Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I found a solution about how to convert timezone with the Linux date command: Timezone conversion by command line

$ date --date='TZ="Asia/Taipei" 18:00'
Fri Jul 16 11:00:00 BST 2010

It's working great, but I can't figure it out how to use it in bash script, when time is a variable, like:

TIME=18:00
DATE="`date --date='TZ="Asia/Taipei" $TIME' +%F\ %H:%M`"
echo $DATE;

I have problems with escaping special characters. And I totally don't understand why the date command works with timezones like BST, EET etc. and not with timezones like "Asia/Taipei", Europe/Moscow etc.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

There's a section titled QUOTING in man bash. I suggest you read it, or the Bash Reference Manual on quoting.

A correct command line would be:

DATE=$( date --date="TZ=\"Asia/Taipei\" $TIME" +%F\ %H:%M )
  • Using $( ) prevents some quoting issues that occur with backticks.
  • You need to double-quote both the time zone (apparently for formatting reasons), and double-quote the argument to allow variable substitution, so just escape the inner quotes once.

I'm not sure what you're asking in your last paragraph, but note that the mapping from continent/city to time zone isn't bijective. Given a date and time in a time zone, you won't be able to find out what city that is. Multiple files in /usr/share/zoneinfo have the same time zone information. Additionally, it depends on when you execute the command, due to daylight savings time, areas changing their entire time zone, or other date related weirdness.

share|improve this answer
    
It works just great, thank you! –  Aleksandr Jan 18 '13 at 8:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.