How to re-write this bash script in Python?

Cameron Simpson cs at zip.com.au
Fri Jul 31 23:31:16 EDT 2015


On 31Jul2015 18:26, Chris Angelico <rosuav at gmail.com> wrote:
>On Fri, Jul 31, 2015 at 6:15 PM, Cameron Simpson <cs at 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.

If date supports it I'd think one could use its -d option:

  utc=$( TZ=src_zone date -d source-time-spec -u +%Y%m%dT%H%M%SZ )

then:

  tz2=$( TZ=dst_zone date -d "$utc" )

Untested, but seems tractable.

>But anyway, still justification to rewrite from
>original spec rather than reimplementing the five-hour hack.

Yes indeed.

Cheers,
Cameron Simpson <cs at zip.com.au>



More information about the Python-list mailing list