Skip to content

Instantly share code, notes, and snippets.

@seignovert
Last active February 17, 2017 10:45
Show Gist options
  • Save seignovert/b79464f5f38c61c22fd4c3bdee975cc5 to your computer and use it in GitHub Desktop.
Save seignovert/b79464f5f38c61c22fd4c3bdee975cc5 to your computer and use it in GitHub Desktop.
[Python] Convert local time to UTC

Convert local time to UTC

With Python

Example for Noumea:

python Noumea_to_UTC.py 2017/02/17 09:30:00

Output:

20170216 223000

With BASH

Install:

sh install.sh

Usage:

Noumea_to_UTC 2017/02/17 09:30:00

Output: 20170216 223000

#/bin/bash
pwd=$(pwd)
if [ -f "$pwd/Noumea_to_UTC.py" ]
then
f="/usr/local/bin/Noumea_to_UTC"
echo "#/bin/bash" > $f
echo "python $pwd/Noumea_to_UTC.py 2017/02/17 09:30:00" >> $f
chmod u+x $f
echo "Install OK"
else
echo "Install ERROR: Noumea_to_UTC.py not found"
exit 1
fi
# Convert local time in Noumea in UTC
# Author: B. Seignovert
# Date: 2017/02/17
from datetime import datetime
from dateutil import tz
import sys
sys.tracebacklimit = 0
if len(sys.argv) == 3:
time = '%s %s' % (sys.argv[1],sys.argv[2])
else:
raise ValueError('[USAGE] python %s 2017/02/17 12:34:56' % __file__)
Noumea = tz.gettz('Pacific/Noumea')
UTC = tz.gettz('UTC')
local = datetime.strptime(time, '%Y/%m/%d %H:%M:%S')
local = local.replace(tzinfo=Noumea)
print local.astimezone(UTC).strftime('%Y%m%d %H%M%S')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment