Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #94806 > unrolled thread

Re: How to re-write this bash script in Python?

Started byChris Angelico <rosuav@gmail.com>
First post2015-07-31 18:26 +1000
Last post2015-08-01 03:39 +0300
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: How to re-write this bash script in Python? Chris Angelico <rosuav@gmail.com> - 2015-07-31 18:26 +1000
    Convert between timezones (was: How to re-write this bash script in Python?) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-31 12:35 +0200
      Re: Convert between timezones Akira Li <4kir4.1i@gmail.com> - 2015-08-01 03:39 +0300

#94806 — Re: How to re-write this bash script in Python?

FromChris Angelico <rosuav@gmail.com>
Date2015-07-31 18:26 +1000
SubjectRe: How to re-write this bash script in Python?
Message-ID<mailman.1112.1438331201.3674.python-list@python.org>
On Fri, Jul 31, 2015 at 6:15 PM, Cameron Simpson <cs@zip.com.au> wrote:
>> For example, bash lacks
>> decent timezone support, so I can well believe random832's guess that
>> your five-hour offset is a simulation of that; but Python can do much
>> better work with timezones, so you can get that actually correct.
>
>
> 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. But anyway, still justification to rewrite from
original spec rather than reimplementing the five-hour hack.

ChrisA

[toc] | [next] | [standalone]


#94811 — Convert between timezones (was: How to re-write this bash script in Python?)

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-07-31 12:35 +0200
SubjectConvert between timezones (was: How to re-write this bash script in Python?)
Message-ID<2716432.pTP2q8pAs5@PointedEars.de>
In reply to#94806
[X-Post & F'up2 comp.unix.shell]

Chris Angelico wrote:

> On Fri, Jul 31, 2015 at 6:15 PM, Cameron Simpson <cs@zip.com.au> 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.

<http://pubs.opengroup.org/onlinepubs/9699919799/utilities/date.html>

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
[…]

:)

-- 
PointedEars

Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#94820 — Re: Convert between timezones

FromAkira Li <4kir4.1i@gmail.com>
Date2015-08-01 03:39 +0300
SubjectRe: Convert between timezones
Message-ID<mailman.1117.1438389636.3674.python-list@python.org>
In reply to#94811
Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:

> [X-Post & F'up2 comp.unix.shell]
>
> Chris Angelico wrote:
>
>> On Fri, Jul 31, 2015 at 6:15 PM, Cameron Simpson <cs@zip.com.au> 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.
>
> <http://pubs.opengroup.org/onlinepubs/9699919799/utilities/date.html>
>
> 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




[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web