import datetime import calendar def day_in_1970_from_month_and_week(month, week): if month < 1 or month > 12 or week < 1 or week > 5: raise ValueError("invalid time: month=%d, week=%d" % (month, week)) oneday = datetime.timedelta(days=1) oneweek = datetime.timedelta(days=7) first_sunday = datetime.date(1970, month, 1) while first_sunday.weekday() != calendar.SUNDAY: first_sunday += oneday while True: day = first_sunday + (oneweek * (week - 1)) if day.month != month: week -= 1 else: break return day.day def format_tz_time_string(month, week, hour): day = day_in_1970_from_month_and_week(month, week) return "1970%02i%02iT%02i0000" % (month, day, hour)