2

Specifically, what I want to know is: when it’s 10:00 (10AM) EST, what time is it in the user’s current time zone? At time of writing the answer to a user in the UK (GMT) would be 15:00 (3PM).

For my specific case I don’t even need the minutes or the day, just the hour (15 in this example).

Most questions of this nature are concerned with converting the present time (now) or from the user’s timezone. At one point I even resorted to ruby for the transformation, but making it work from anything but UTC is a bit harder. I feel like there must be way to do it with date, but macOS uses BSD’s date and most answers use GNU’s.

  • 1
    You almost certainly do want the minutes, to handle the cases where the difference in times is not an integer number of hours. – icarus Jan 5 '17 at 20:38
4

It's pretty clunky, but this ought to do it on MacOS using only the shell and the date command:

Convert 2017-01-05 10:00:00 in North American eastern time to the system's local time zone:

date -r $(TZ=America/Montreal date -j 010510002017 +%s)

To convert that to a timezone other than the local one, prefix the outer command with the desired target timezone:

TZ=Australia/Sydney date -r $(TZ=America/Montreal date -j 010510002017 +%s)

Just beware the extremely bizarre date input format: mmddHHMMYYYY

|improve this answer|||||
  • Converting to seconds and then from seconds. That’s smart. Thank you, this is exactly what I needed. Added a recommended edit to cover my specific case. – user137369 Jan 5 '17 at 23:55
1

The POSIX APIs aren't very rich in this regard. You're probably better off using something like pytz than system tools:

#!/usr/bin/python

import datetime
import pytz

eastern = pytz.timezone('America/Montreal')
britain = pytz.timezone('Europe/London')
datetime.datetime(2017, 1, 5, 10, 0, tzinfo=eastern).astimezone(britain)
|improve this answer|||||
  • To be clear, I do not want the target timezone to be the UK. I want it to be the current one from the user. Also, if the answer has to not use date, I’d rather it use ruby, since I’m more familiar with it. – user137369 Jan 5 '17 at 22:05
  • 1
    Fair enough about ruby. As for the target timezone, Europe/London is just an example. Use whatever target timezone you want instead, or pytz.reference.LocalTimezone for the local one from the environment. – Celada Jan 5 '17 at 22:23
  • Upvoted all the same since it was still a good answer. Thank you for the help! – user137369 Jan 5 '17 at 23:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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