Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'bash': 0.07; 'variable,': 0.07; 'invocation': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'thu': 0.09; 'worse': 0.09; '{},': 0.09; 'python': 0.10; 'python.': 0.11; '--version': 0.16; 'accordingly': 0.16; 'cdt': 0.16; 'echo': 0.16; 'posix': 0.16; 'posted:': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'simpson': 0.16; 'subject:Convert': 0.16; 'subject:between': 0.16; 'wrote:': 0.16; 'variable': 0.18; '>>>': 0.20; '2015': 0.20; '31,': 0.22; 'assuming': 0.22; 'code.': 0.23; "haven't": 0.24; 'import': 0.24; 'install': 0.25; 'header:User-Agent:1': 0.26; 'command': 0.26; 'header:X-Complaints-To:1': 0.26; 'chris': 0.26; 'skip:" 20': 0.26; 'fri,': 0.27; 'skip:# 10': 0.27; 'converting': 0.27; 'gnu': 0.27; "skip:' 10": 0.28; 'skip:( 20': 0.28; 'environment': 0.29; 'convert': 0.29; 'skip:[ 10': 0.31; 'probably': 0.31; 'skip:s 30': 0.31; 'message-id:@gmail.com': 0.34; 'skip:d 20': 0.34; 'could': 0.35; 'but': 0.36; 'url:org': 0.36; 'beginning': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:org': 0.37; 'to:addr:python.org': 0.40; 'thomas': 0.63; 'between': 0.65; 'cameron': 0.66; 'skip:$ 10': 0.67; 'apologize': 0.69; 'jul': 0.72; 'received:89': 0.80; 'actually,': 0.84; 'pip': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Akira Li <4kir4.1i@gmail.com> Subject: Re: Convert between timezones Date: Sat, 01 Aug 2015 03:39:13 +0300 References: <20150731081503.GA12474@cskk.homeip.net> <2716432.pTP2q8pAs5@PointedEars.de> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: 89.169.229.68 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) Cancel-Lock: sha1:Gpn65gXtP3d1UHoakL29JM30rFw= X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 60 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1438389636 news.xs4all.nl 2918 [2001:888:2000:d::a6]:54979 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:94820 Thomas 'PointedEars' Lahn writes: > [X-Post & F'up2 comp.unix.shell] > > Chris Angelico wrote: > >> On Fri, Jul 31, 2015 at 6:15 PM, Cameron Simpson wrote: >>> Actually, bash has no timezone support but the date command _does_, and >>> probably neither better nor worse than Python. All one has to do is set >>> the TZ environment variable, eg (untested): >>> >>> _year_gmt=$( TZ=GMT date +%Y ) >> >> That's assuming that it's converting against the current system >> timezone. I don't know how you'd use `date` to convert between two >> arbitrary timezones. […] > > With POSIX date(1), ISTM all you could do is set the system time and for an > additional invocation the TZ variable accordingly for output. > > > > With GNU date(1): > > $ (tz_source="Asia/Dubai"; time_source="$(LC_TIME=C TZ=$tz_source date -d > "today 00:00 UTC+4" -Im)"; tz_target="America/Chicago"; echo "When it was > $time_source in $tz_source, it was $(LC_TIME=C TZ=$tz_target date -d > "$time_source") in $tz_target.") > When it was 2015-07-31T00:00+0400 in Asia/Dubai, it was Thu Jul 30 15:00:00 > CDT 2015 in America/Chicago. > > $ date --version > date (GNU coreutils) 8.23 > […] > Here's a corresponding Python code. I haven't seen the beginning of the discussion. I apologize if it has been already posted: #!/usr/bin/env python from datetime import datetime import pytz # $ pip install pytz source_tz, target_tz = map(pytz.timezone, ['Asia/Dubai', 'America/Chicago']) d = datetime.now(source_tz) # the current time in source_tz timezone midnight = source_tz.localize(datetime(d.year, d.month, d.day), is_dst=None) fmt = "%Y-%m-%dT%H:%M:%S%z" print("When it was {:{fmt}} in {}, it was {:{fmt}} in {}".format( midnight, source_tz.zone, target_tz.normalize(midnight), target_tz.zone, fmt=fmt)) Output: When it was 2015-08-01T00:00:00+0400 in Asia/Dubai, it was 2015-07-31T15:00:00-0500 in America/Chicago