Skip to content

Commit 5109fbc

Browse files
committed
Merge branch 'master' into dev-version
2 parents 8466be8 + 37b0a73 commit 5109fbc

File tree

6 files changed

+56
-20
lines changed

6 files changed

+56
-20
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ VObject is licensed under the [Apache 2.0 license](http://www.apache.org/license
2121
Useful scripts included with VObject:
2222

2323
* [ics_diff](https://github.com/py-vobject/vobject/blob/master/vobject/ics_diff.py): order is irrelevant in iCalendar files, return a diff of meaningful changes between icalendar files
24-
* [change_tz](https://github.com/py-vobject/vobject/blob/master/vobject/change_tz.py): Take an iCalendar file with events in the wrong timezone, change all events or just UTC events into one of the timezones PyICU supports. Requires [PyICU](https://pypi.python.org/pypi/PyICU/).
24+
* [change_tz](https://github.com/py-vobject/vobject/blob/master/vobject/change_tz.py): Take an iCalendar file with events in the wrong timezone, change all events or just UTC events into one of the timezones **pytz** supports. Requires [pytz](https://pypi.python.org/pypi/pytz/).
2525

2626
# History
2727
VObject was originally developed in concert with the Open Source Application

requirements-dev.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1+
-r requirements.txt
2+
13
coverage
2-
python-dateutil >= 2.4.0
34
setuptools
45
wheel

requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
python-dateutil >= 2.4.0
1+
python-dateutil >= 2.5.0; python_version < '3.10'
2+
python-dateutil >= 2.7.0; python_version >= '3.10'
3+
pytz>=2019.1

setup.py

100755100644
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@
4747
]
4848
},
4949
include_package_data = True,
50-
install_requires = ['python-dateutil >= 2.4.0', 'six'],
50+
install_requires=["python-dateutil >= 2.5.0; python_version < '3.10'",
51+
"python-dateutil >= 2.7.0; python_version >= '3.10'",
52+
"pytz", 'six'],
5153
platforms = ["any"],
5254
packages = find_packages(),
5355
description = "A full-featured Python package for parsing and creating "

vobject/change_tz.py

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Translate an ics file's events to a different timezone."""
22

3+
<<<<<<< HEAD
34
from optparse import OptionParser
45
import vobject
56

@@ -8,7 +9,17 @@
89
except:
910
PyICU = None
1011

12+
=======
13+
>>>>>>> master
1114
from datetime import datetime
15+
from optparse import OptionParser
16+
17+
import pytz
18+
from dateutil import tz
19+
20+
from vobject import base, icalendar
21+
22+
version = "0.1"
1223

1324

1425
def change_tz(cal, new_timezone, default, utc_only=False, utc_tz=vobject.icalendar.utc):
@@ -18,11 +29,9 @@ def change_tz(cal, new_timezone, default, utc_only=False, utc_tz=vobject.icalend
1829
Args:
1930
cal (Component): the component to change
2031
new_timezone (tzinfo): the timezone to change to
21-
default (tzinfo): a timezone to assume if the dtstart or dtend in cal
22-
doesn't have an existing timezone
32+
default (tzinfo): a timezone to assume if the dtstart or dtend in cal doesn't have an existing timezone
2333
utc_only (bool): only convert dates that are in utc
24-
utc_tz (tzinfo): the tzinfo to compare to for UTC when processing
25-
utc_only=True
34+
utc_tz (tzinfo): the tzinfo to compare to for UTC when processing utc_only=True
2635
"""
2736

2837
for vevent in getattr(cal, 'vevent_list', []):
@@ -31,21 +40,41 @@ def change_tz(cal, new_timezone, default, utc_only=False, utc_tz=vobject.icalend
3140
for node in (start, end):
3241
if node:
3342
dt = node.value
34-
if (isinstance(dt, datetime) and
35-
(not utc_only or dt.tzinfo == utc_tz)):
43+
if isinstance(dt, datetime) and (not utc_only or dt.tzinfo == utc_tz):
3644
if dt.tzinfo is None:
3745
dt = dt.replace(tzinfo=default)
3846
node.value = dt.astimezone(new_timezone)
3947

4048

49+
def show_timezones():
50+
for tz_string in pytz.all_timezones:
51+
print(tz_string)
52+
53+
54+
def convert_events(utc_only, args):
55+
print("Converting {} events".format('only UTC' if utc_only else 'all'))
56+
ics_file = args[0]
57+
_tzone = args[1] if len(args) > 1 else 'UTC'
58+
59+
print("... Reading {}".format(ics_file))
60+
cal = base.readOne(open(ics_file))
61+
change_tz(cal, new_timezone=tz.gettz(_tzone), default=tz.gettz('UTC'), utc_only=utc_only)
62+
63+
out_name = "{}.converted".format(ics_file)
64+
print("... Writing {}".format(out_name))
65+
with open(out_name, 'wb') as out:
66+
cal.serialize(out)
67+
68+
print("Done")
69+
70+
4171
def main():
4272
options, args = get_options()
43-
if PyICU is None:
44-
print("Failure. change_tz requires PyICU, exiting")
45-
elif options.list:
46-
for tz_string in PyICU.TimeZone.createEnumeration():
47-
print(tz_string)
73+
74+
if options.list:
75+
show_timezones()
4876
elif args:
77+
<<<<<<< HEAD
4978
utc_only = options.utc
5079
if utc_only:
5180
which = "only UTC"
@@ -68,11 +97,13 @@ def main():
6897
cal.serialize(out)
6998

7099
print("Done")
100+
=======
101+
convert_events(utc_only=options.utc, args=args)
102+
>>>>>>> master
71103

72104

73105
def get_options():
74106
# Configuration options
75-
76107
usage = """usage: %prog [options] ics_file [timezone]"""
77108
parser = OptionParser(usage=usage, version=vobject.VERSION)
78109
parser.set_description("change_tz will convert the timezones in an ics file. ")
@@ -83,14 +114,14 @@ def get_options():
83114
default=False, help="List available timezones")
84115

85116
(cmdline_options, args) = parser.parse_args()
86-
if not args and not cmdline_options.list:
117+
if not (args or cmdline_options.list):
87118
print("error: too few arguments given")
88-
print
89119
print(parser.format_help())
90-
return False, False
120+
return cmdline_options, False
91121

92122
return cmdline_options, args
93123

124+
94125
if __name__ == "__main__":
95126
try:
96127
main()

vobject/icalendar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def pickTzid(tzinfo, allowUTC=False):
333333
if tzinfo is None or (not allowUTC and tzinfo_eq(tzinfo, utc)):
334334
# If tzinfo is UTC, we don't need a TZID
335335
return None
336-
# try PyICU's tzid key
336+
# try Pytz's tzid key
337337
if hasattr(tzinfo, 'tzid'):
338338
return toUnicode(tzinfo.tzid)
339339

0 commit comments

Comments
 (0)