def _action_dtime_parse(self, full_str, length, t): """ Parses an S3-formatted time string into a datetime.datetime instance. :param str full_str: The full, un-parsed time string. :param int length: The length of the text to parse. :param list t: The grouped string to parse. :rtype: datetime.datetime :returns: The datetime.datetime equivalent of the time string with the correct (settings.TIME_ZONE) timezone set. """ # Comes in as [['22/Apr/2011:18:28:10', '+000']] but we're just # interested in the time, since strptime with a %z formatter doesn't # work as expected on all platforms. dtime_str = t[0][0] # 22/Apr/2011:18:28:10 utc = datetime.datetime.strptime(dtime_str, "%d/%b/%Y:%H:%M:%S") # The parsed time is in UTC. Make it "aware". utc = utc.replace(tzinfo=zoneinfo.gettz('UTC')) if settings.TIME_ZONE != 'UTC': # Get the user's local timezone. to_zone = zoneinfo.gettz(settings.TIME_ZONE) # Set the timezone to the configured TZ. return utc.astimezone(to_zone) else: # Already UTC, don't budge. return utc
def main(): PrintHeaders() form = cgi.FieldStorage() comicName = form.getvalue("name") rssItems = [] if comicName == None: print("Expected Parameter: 'name'") else: for rss in ComicRss.objects.filter(ComicLogId__ComicId__Name=comicName).order_by("-ComicLogId__FetchDate"): rssTitle = rss.ComicLogId.ComicId.Name utc_zone = zoneinfo.gettz('UTC') est_zone = zoneinfo.gettz('US/Eastern') utc_date = rss.ComicLogId.FetchDate.replace(tzinfo=utc_zone) est_date = utc_date.astimezone(est_zone) desc = "<a href=\"%s\" target=\"_blank\">Comic Website</a><br/> Publish Date: %s<br/><img src=\"%s\"/>" % \ (rss.ComicLogId.ComicId.Url, est_date, rss.ComicLogId.ImageUrl) rssItems.append(RSSItem(title = rss.ComicLogId.ComicId.Name, link = rss.ComicLogId.ImageUrl, description = desc, pubDate = utc_date)) c = Comic.objects.get(Name=comicName) rss = RSS2( title = rssTitle, link = c.Url, description = "%s image feed" % rssTitle, items = rssItems) print rss.to_xml()
def test_opt_in_behavior(self): # Given the addition of opt out behavior, verify if its # not configured that we don't touch an instance that # has no downtime tag i = instance(Tags=[]) i2 = instance(Tags=[{'Key': 'maid_offhours', 'Value': ''}]) i3 = instance(Tags=[{'Key': 'maid_offhours', 'Value': 'on'}]) t = datetime.datetime( year=2015, month=12, day=1, hour=19, minute=5, tzinfo=zoneinfo.gettz('America/New_York')) f = OffHour({}) with mock_datetime_now(t, datetime): self.assertEqual(f(i), False) self.assertEqual(f(i2), True) self.assertEqual(f(i3), True) t = datetime.datetime( year=2015, month=12, day=1, hour=7, minute=5, tzinfo=zoneinfo.gettz('America/New_York')) f = OnHour({}) with mock_datetime_now(t, datetime): self.assertEqual(f(i), False) self.assertEqual(f(i2), True) self.assertEqual(f(i3), True)
def main(): form = cgi.FieldStorage() comicName = form.getvalue("name") PrintHeaders(comicName) rssItems = [] rssTitle = "" print ("<ul>") desc = "" mainRssList = list(ComicRss.objects.filter(ComicLogId__ComicId__Name=comicName).order_by("-ComicLogId__FetchDate")) archiveRss = ComicRssArchive.objects.filter(ComicLogId__ComicId__Name=comicName).order_by("-ComicLogId__FetchDate") mainRssList.extend(list(archiveRss)) for rss in mainRssList: rssTitle = rss.ComicLogId.ComicId.Name utc_zone = zoneinfo.gettz('UTC') est_zone = zoneinfo.gettz('US/Eastern') utc_date = rss.ComicLogId.FetchDate.replace(tzinfo=utc_zone) est_date = utc_date.astimezone(est_zone) desc += """ <li> <a href=\"{0}\" target=\"_blank\">Comic Website</a><br/> Publish Date: {1}<br/><img src=\"{2}\"/> </li>""".format(rss.ComicLogId.ComicId.Url, est_date, rss.ComicLogId.ImageUrl) c = Comic.objects.get(Name=comicName) print("%s</ul>" % desc) PrintFooter()
def main(): PrintHeaders() form = cgi.FieldStorage() comicName = form.getvalue("name") rssItems = [] rssTitle = "" for rss in ComicRss.objects.filter(ComicLogId__ComicId__Name=comicName).order_by("-ComicLogId__FetchDate")[:10]: rssTitle = rss.ComicLogId.ComicId.Name utc_zone = zoneinfo.gettz("UTC") est_zone = zoneinfo.gettz("US/Eastern") utc_date = rss.ComicLogId.FetchDate.replace(tzinfo=utc_zone) est_date = utc_date.astimezone(est_zone) desc = '<a href="{0}" target="_blank">Comic Website</a> - '.format(rss.ComicLogId.ComicId.Url) desc += '<a href="http://jamesralexander.com/comics/{0}/all" \ target="_blank">Comic Archive</a><br/>'.format( rss.ComicLogId.ComicId.Name ) desc += "<br/>Publish Date: {0}".format(est_date) desc += '<br/><img src="{0}"/>'.format(rss.ComicLogId.ImageUrl) rssItems.append( RSSItem(title=rss.ComicLogId.ComicId.Name, link=rss.ComicLogId.ImageUrl, description=desc, pubDate=utc_date) ) c = Comic.objects.get(Name=comicName) rss = RSS2(title=rssTitle, link=c.Url, description="%s image feed" % rssTitle, items=rssItems) print rss.to_xml()
def test_tz_long_form_resolve(self): pacific = zoneinfo.gettz("America/Los_Angeles") nzt = zoneinfo.gettz("Pacific/Auckland") self.assertEqual( OnHour({}).get_tz('america/los_angeles'), pacific) self.assertEqual( OnHour({}).get_tz('pacific/auckland'), nzt)
def testZoneInfoOffsetSignal(self): utc = zoneinfo.gettz("UTC") nyc = zoneinfo.gettz("America/New_York") self.assertNotEqual(utc, None, MISSING_TARBALL) self.assertNotEqual(nyc, None) t0 = datetime(2007, 11, 4, 0, 30, tzinfo=nyc) t1 = t0.astimezone(utc) t2 = t1.astimezone(nyc) self.assertEqual(t0, t2) self.assertEqual(nyc.dst(t0), timedelta(hours=1))
def test_opt_out_behavior(self): # Some users want to match based on policy filters to # a resource subset with default opt out behavior t = datetime.datetime( year=2015, month=12, day=1, hour=19, minute=5, tzinfo=zoneinfo.gettz('America/New_York')) i = instance(Tags=[]) f = OffHour({'opt-out': True}) with mock_datetime_now(t, datetime): self.assertEqual(f(i), True) t = datetime.datetime( year=2015, month=12, day=1, hour=7, minute=5, tzinfo=zoneinfo.gettz('America/New_York')) f = OnHour({})
def test_asg_mark_for_op_hours(self): session_factory = self.replay_flight_data('test_asg_mark_for_op_hours') session = session_factory(region='us-east-1') asg = session.client('autoscaling') localtz = zoneinfo.gettz('America/New_York') dt = datetime.now(localtz) dt = dt.replace(year=2018, month=2, day=20, hour=8, minute=42) policy = self.load_policy({ 'name': 'asg-mark-for-op-hours', 'resource': 'asg', 'filters': [ {'tag:Service': 'absent'} ], 'actions': [ {'type': 'mark-for-op', 'op': 'delete', 'hours': 1} ], }, session_factory=session_factory) resources = policy.run() self.assertEqual(len(resources), 1) describe_auto_scaling_groups = asg.describe_auto_scaling_groups( AutoScalingGroupNames=['marked'] ) resource=describe_auto_scaling_groups['AutoScalingGroups'][0] tags = [ t['Value'] for t in resource['Tags'] if t['Key'] == 'maid_status'] result = datetime.strptime( tags[0].strip().split('@', 1)[-1], '%Y/%m/%d %H%M %Z').replace( tzinfo=localtz) self.assertEqual(result.date(), dt.date())
def test_custom_onhours(self): t = datetime.datetime.now(zoneinfo.gettz("America/New_York")) t = t.replace(year=2016, month=5, day=26, hour=7, minute=00) results = [] with mock_datetime_now(t, datetime): for i in [ instance( Tags=[ { "Key": "maid_offhours", "Value": "off=(m-f,19);on=(m-f,7);tz=et", } ] ), instance( Tags=[ { "Key": "maid_offhours", "Value": "off=(m-f,20);on=(m-f,9);tz=et", } ] ), ]: results.append(OnHour({})(i)) self.assertEqual(results, [True, False])
def test_offhours_records(self): session_factory = self.replay_flight_data('test_offhours_records') t = datetime.datetime.now(zoneinfo.gettz('America/New_York')) t = t.replace(year=2016, month=8, day=14, hour=19, minute=00) with mock_datetime_now(t, datetime): p = self.load_policy({ 'name': 'offhours-records', 'resource': 'ec2', 'filters': [ {'State.Name': 'running'}, {'type': 'offhour', 'offhour': 19, 'tag': 'custodian_downtime', 'default_tz': 'est', 'weekends': False}] }, session_factory=session_factory) resources = p.run() self.assertEqual(resources, []) with open(os.path.join( p.options['output_dir'], 'offhours-records', 'parse_errors.json')) as fh: data = json.load(fh) self.assertEqual(len(data), 1) self.assertEqual(data[0][0], 'i-0ee3a9bc2eeed269f') self.assertEqual(data[0][1], 'off=[m-f,8];on=[n-f,5];pz=est') with open(os.path.join( p.options['output_dir'], 'offhours-records', 'opted_out.json')) as fh: data = json.load(fh) self.assertEqual(len(data), 1) self.assertEqual(data[0]['InstanceId'], 'i-0a619b58a7e704a9f')
def test_process(self): f = OffHour({"opt-out": True}) instances = [ instance(Tags=[]), instance(Tags=[{"Key": "maid_offhours", "Value": ""}]), instance(Tags=[{"Key": "maid_offhours", "Value": "on"}]), instance(Tags=[{"Key": "maid_offhours", "Value": "off"}]), instance( Tags=[ { "Key": "maid_offhours", "Value": "off=(m-f,5);zebrablue,on=(t-w,5)", } ] ), ] t = datetime.datetime( year=2015, month=12, day=1, hour=19, minute=5, tzinfo=zoneinfo.gettz("America/New_York"), ) with mock_datetime_now(t, datetime): self.assertEqual( f.process(instances), [instances[0], instances[1], instances[2]] )
def test_tz_only(self): t = datetime.datetime.now(zoneinfo.gettz('America/New_York')) t = t.replace(year=2016, month=5, day=26, hour=7, minute=00) with mock_datetime_now(t, datetime): i = instance(Tags=[{'Key': 'maid_offhours', 'Value': 'tz=est'}]) self.assertEqual(OnHour({})(i), True)
def test_ec2_mark_hours(self): localtz = zoneinfo.gettz('America/New_York') dt = datetime.now(localtz) dt = dt.replace(year=2018, month=2, day=20, hour=18, minute=00) session_factory = self.replay_flight_data('test_ec2_mark_hours') session = session_factory(region='us-east-1') ec2 = session.client('ec2') policy = self.load_policy({ 'name': 'ec2-mark-5-hours', 'resource': 'ec2', 'filters': [ {'tag:hourly-mark': 'absent'}, {'tag:CreatorName': 'joshuaroot'}], 'actions': [{ 'type': 'mark-for-op', 'tag': 'hourly-mark', 'hours': 3, 'op': 'stop'}] }, session_factory=session_factory) resources = policy.run() self.assertEqual(len(resources), 1) resource = ec2.describe_instances( InstanceIds=[resources[0]['InstanceId']])[ 'Reservations'][0]['Instances'][0] tags = [ t['Value'] for t in resource['Tags'] if t['Key'] == 'hourly-mark'] result = datetime.strptime( tags[0].strip().split('@', 1)[-1], '%Y/%m/%d %H%M %Z').replace( tzinfo=localtz) self.assertEqual(result.date(), dt.date())
def test_custom_bad_hours(self): t = datetime.datetime.now(zoneinfo.gettz('America/New_York')) t = t.replace(year=2016, month=5, day=26, hour=19, minute=00) # default error handling is to exclude the resource with mock_datetime_now(t, datetime): # This isn't considered a bad value, its basically omitted. i = instance(Tags=[{'Key': 'maid_offhours', 'Value': 'off=();tz=et'}]) self.assertEqual(OffHour({})(i), False) i = instance(Tags=[{'Key': 'maid_offhours', 'Value': 'off=(m-f,90);on=(m-f,7);tz=et'}]) # malformed value self.assertEqual(OffHour({})(i), False) t = t.replace(year=2016, month=5, day=26, hour=13, minute=00) with mock_datetime_now(t, datetime): i = instance(Tags=[{'Key': 'maid_offhours', 'Value': 'off=();tz=et'}]) # will go to default values, but not work due to default time self.assertEqual(OffHour({})(i), False) i = instance(Tags=[{'Key': 'maid_offhours', 'Value': 'off=(m-f,90);on=(m-f,7);tz=et'}]) self.assertEqual(OffHour({})(i), False)
def test_offhours(self): t = datetime.datetime(year=2015, month=12, day=1, hour=19, minute=5, tzinfo=zoneinfo.gettz('America/New_York')) with mock_datetime_now(t, datetime): i = instance(Tags=[ {'Key': 'maid_offhours', 'Value': 'tz=est'}]) self.assertEqual(OffHour({})(i), True)
def _initialize_windows_timezones(cls): def _windows_timezones (filename): """Returns mapping of Windows timezone names onto Olson names. This uses the Unicode Consortium's supplemental data file, available at <http://unicode.org/cldr/data/common/supplemental/supplementalData.xml>. @param filename the file to read @type filename a string filename path @return a mapping of Windows timezone names to Olson timezone names @rtype dict(string->string) """ import xml.dom.minidom mapping = {} d = xml.dom.minidom.parse(filename) if d: windows_sections = [x for x in d.getElementsByTagName("mapTimezones") if ( x.hasAttribute("type") and (x.getAttribute("type") == u"windows"))] for section in windows_sections: # probably only one section for node in section.getElementsByTagName("mapZone"): if (node.hasAttribute("other") and node.hasAttribute("type")): mapping[node.getAttribute("other")] = node.getAttribute("type") return mapping filepath = os.path.join(os.path.dirname(__file__), "windows-timezones.xml") for key, tzname in _windows_timezones(filepath).items(): tz = zoneinfo.gettz(tzname) if tz: vobject.icalendar.registerTzid(key, tz) note(5, "registered %s for '%s'", tz, key) cls.INITIALIZED = True
def test_asg_mark_for_op_hours(self): session_factory = self.replay_flight_data("test_asg_mark_for_op_hours") session = session_factory(region="us-east-1") asg = session.client("autoscaling") localtz = zoneinfo.gettz("America/New_York") dt = datetime.now(localtz) dt = dt.replace( year=2018, month=2, day=20, hour=12, minute=42, second=0, microsecond=0 ) policy = self.load_policy( { "name": "asg-mark-for-op-hours", "resource": "asg", "filters": [{"tag:Service": "absent"}], "actions": [{"type": "mark-for-op", "op": "delete", "hours": 1}], }, session_factory=session_factory, ) resources = policy.run() self.assertEqual(len(resources), 1) describe_auto_scaling_groups = asg.describe_auto_scaling_groups( AutoScalingGroupNames=["marked"] ) resource = describe_auto_scaling_groups["AutoScalingGroups"][0] tags = [t["Value"] for t in resource["Tags"] if t["Key"] == "maid_status"] result = datetime.strptime( tags[0].strip().split("@", 1)[-1], "%Y/%m/%d %H%M %Z" ).replace( tzinfo=localtz ) self.assertEqual(result, dt)
def testZoneInfoCopy(self): # copy.copy() called on a ZoneInfo file was returning the same instance CHI = zoneinfo.gettz('America/Chicago') CHI_COPY = copy.copy(CHI) self.assertIsNot(CHI, CHI_COPY) self.assertEqual(CHI, CHI_COPY)
def process(self, resources): self.tz = zoneinfo.gettz( Time.TZ_ALIASES.get(self.data.get('tz', 'utc'))) self.id_key = self.manager.get_model().id # Move this to policy? / no resources bypasses actions? if not len(resources): return msg_tmpl = self.data.get('msg', self.default_template) op = self.data.get('op', 'stop') tag = self.data.get('tag', DEFAULT_TAG) days = self.data.get('days', 0) hours = self.data.get('hours', 0) action_date = self.generate_timestamp(days, hours) msg = msg_tmpl.format( op=op, action_date=action_date) self.log.info("Tagging %d resources for %s on %s" % ( len(resources), op, action_date)) tags = {tag: msg} batch_size = self.data.get('batch_size', self.batch_size) _common_tag_processer( self.executor_factory, batch_size, self.concurrency, self.process_resource_set, self.id_key, resources, tags, self.log)
def test_offhours(self): t = datetime.datetime(year=2015, month=12, day=1, hour=19, minute=5, tzinfo=zoneinfo.gettz('America/New_York')) with mock.patch('datetime.datetime') as dt: dt.now.side_effect = lambda tz=None: t i = instance(Tags=[ {'Key': 'maid_offhours', 'Value': 'tz=est'}]) self.assertEqual(OffHour({})(i), True)
def test_resource_schedule_error(self): t = datetime.datetime.now(zoneinfo.gettz("America/New_York")) t = t.replace(year=2015, month=12, day=1, hour=19, minute=5) f = OffHour({}) f.process_resource_schedule = lambda: False with mock_datetime_now(t, datetime): i = instance(Tags=[{"Key": "maid_offhours", "Value": "tz=est"}]) self.assertEqual(f(i), False)
def stdtime(tz, year, month, day, hour, min, sec): """Use /usr/share/zoneinfo to interpret a time in a timezone. >>> stdtime("America/Chicago", "2007-04-02T21:53:27") '2007-04-02T21:53:27-05:00' """ return datetime(year, month, day, hour, min, sec, tzinfo=zoneinfo.gettz("%s" % (tz)) )
def calendar(zone): tz = zoneinfo.gettz(zone) c = vobject.iCalendar() v = c.add('vevent') v.add('summary').value = 'test' v.add('description').value = 'testdesc' dtstart = datetime.datetime(year=2000, month=1, day=1, tzinfo=tz) v.add('dtstart').value = dtstart return c.serialize()
def test_opt_in_behavior(self): # Given the addition of opt out behavior, verify if its # not configured that we don't touch an instance that # has no downtime tag t = datetime.datetime( year=2015, month=12, day=1, hour=19, minute=5, tzinfo=zoneinfo.gettz('America/New_York')) i = instance(Tags=[]) f = OffHour({}) with mock.patch('datetime.datetime') as dt: dt.now.side_effect = lambda tz=None: t self.assertEqual(f(i), False) t = datetime.datetime( year=2015, month=12, day=1, hour=7, minute=5, tzinfo=zoneinfo.gettz('America/New_York')) f = OnHour({}) self.assertEqual(f(i), False)
def add_meeting_to_vcal(ical, meeting, reminder=None): """ Convert a Meeting object into iCal object and add it to the provided calendar. :arg ical: the iCal calendar object to which the meetings should be added. :arg meeting: a single fedocal.model.Meeting object to convert to iCal and add to the provided calendar. :kwarg reminder: None or a datetime.timedelta instance. """ entry = ical.add('vevent') entry.add('summary').value = meeting.meeting_name if meeting.meeting_information: entry.add('description').value = meeting.meeting_information entry.add('organizer').value = ', '.join(meeting.meeting_manager) if meeting.meeting_location: entry.add('location').value = meeting.meeting_location start = entry.add('dtstart') stop = entry.add('dtend') if meeting.full_day: start.value = meeting.meeting_date stop.value = meeting.meeting_date_end entry.add('transp').value = 'TRANSPARENT' else: tz = zoneinfo.gettz(meeting.meeting_timezone) dti_start = datetime.combine( meeting.meeting_date, meeting.meeting_time_start) start.value = dti_start.replace(tzinfo=tz) dti_end = datetime.combine( meeting.meeting_date_end, meeting.meeting_time_stop) stop.value = dti_end.replace(tzinfo=tz) if meeting.recursion_frequency and meeting.recursion_ends: newrule = rrule.rruleset() freq = 1 if meeting.recursion_frequency == 14: freq = 2 newrule.rrule( rrule.rrule( freq=rrule.WEEKLY, interval=freq, dtstart=start.value, until=meeting.recursion_ends)) entry.rruleset = newrule if reminder: valarm = entry.add('valarm') valarm.add('trigger').value = reminder valarm.add('action').value = 'DISPLAY' valarm.add('description').value = ( '[{calendar_name}] [Fedocal] Reminder meeting: {meeting_name}' ).format(calendar_name=meeting.calendar_name, meeting_name=meeting.meeting_name)
def validate_log_date(self, line): delta = timedelta(hours=2) m = REG_GENERAL_ERR.match(line) if m: log_time = datetime.strptime(m.group(1), "%Y-%m-%d %H:%M:%S") log_time = log_time.replace(tzinfo=tz.tzutc()).astimezone(zoneinfo.gettz(self._GENERAL_CONFIG["TIMEZONE"])) log_time = log_time.replace(tzinfo=None) if (self._now - log_time) > delta: return False elif BEGIN_DEADLOCK in line: m = REG_DEADLOCK.match(line) log_time = datetime.strptime(m.group(1), "%Y-%m-%d %H:%M:%S") log_time = log_time.replace(tzinfo=tz.tzutc()).astimezone(zoneinfo.gettz(self._GENERAL_CONFIG["TIMEZONE"])) log_time = log_time.replace(tzinfo=None) if (self._now - log_time) > delta: return False return True
def process(self, resources, event=None): from c7n_azure.utils import now if self.current_date is None: self.current_date = now() self.tag = self.data.get('tag', DEFAULT_TAG) self.op = self.data.get('op', 'stop') self.skew = self.data.get('skew', 0) self.skew_hours = self.data.get('skew_hours', 0) self.tz = zoneinfo.gettz(Time.TZ_ALIASES.get(self.data.get('tz', 'utc'))) return super(TagActionFilter, self).process(resources, event)
def tryTz(timeZoneAsZoneInfo): tz = gettz(timeZoneAsZoneInfo) if tz: # logging.info(tz) return "gettz" tz = zoneinfo.gettz(timeZoneAsZoneInfo) if tz: # logging.warning(tz) return "zoneinfo" return False
def tryTz(timeZoneAsZoneInfo): tz = gettz(timeZoneAsZoneInfo) if tz: # logging.info(tz) return 'gettz' tz = zoneinfo.gettz(timeZoneAsZoneInfo) if tz: # logging.warning(tz) return 'zoneinfo' return False
def get_tz(cls, tz): found = cls.TZ_ALIASES.get(tz) if found: return zoneinfo.gettz(found) return zoneinfo.gettz(tz.title())
import re import logging import csv import datetime from django.core.management.base import BaseCommand from django.contrib.contenttypes.models import ContentType from dateutil import zoneinfo from mks.models import Member from committees.models import Committee from events.models import Event # NB: All dates scraped from the knesset site are assumed to be in timezone Israel. from simple.constants import KNESSET_COMMITTEES_AGENDA_PAGE isr_tz = zoneinfo.gettz('Israel') utc_tz = zoneinfo.gettz('UTC') logger = logging.getLogger("open-knesset.parse_future_committee_meetings") # spamWriter = csv.writer(open('eggs.csv', 'wb')) ParsedResult = namedtuple('ParseResult', 'name, year, month, day, hour, minute, ' + 'title, end_hour, end_minute, end_guessed') class Command(BaseCommand): args = '' help = 'Parses commitee members from the Knesset website' committee_ct = ContentType.objects.get_for_model(Committee)
def main(argv): config_file = None test_flag = False species_num = None try: opts, args = getopt.getopt(argv, "hc:ts:", ["help", "config=","test","species"]) except getopt.GetoptError: print(__doc__) sys.exit(2) for opt, arg in opts: if opt in ("-h", "--help"): print(__doc__) sys.exit(0) elif opt in ("-c", "--config"): config_file = arg elif opt in ("-t", "--test"): test_flag = True elif opt in ("-s", "--species"): species_num = arg if config_file: config = fhutils.GameConfig(config_file) else: config = fhutils.GameConfig() game = config.gameslist[0] # for now we only support a single game game_name = game['name'] game_stub = game['stub'] deadline_rule = game['deadline'] data_dir = game['datadir'] bin_dir = config.bindir players = fhutils.Game().players if not os.path.isdir(data_dir): print("Sorry data directory %s does not exist." % (data_dir)) sys.exit(2) if not os.path.isdir(bin_dir): print("Sorry bin directory %s does not exist." % (bin_dir)) sys.exit(2) turn = fhutils.run(bin_dir, "TurnNumber").strip() global message,deadline_msg, start_msg next_deadline = deadline_rule.after(datetime.now(config['zone'])) est = zoneinfo.gettz('America/New_York') pst = zoneinfo.gettz('America/Los_Angeles') poland = zoneinfo.gettz('Europe/Warsaw') day = next_deadline.strftime("%A %B %d") time = next_deadline.strftime("%H:%M (%I:%M %p) %Z") time += "\n= %s" % (next_deadline.astimezone(est).strftime("%I:%M %p %Z")) time += "\n= %s" % (next_deadline.astimezone(pst).strftime("%I:%M %p %Z")) time += "\n= %s" % (next_deadline.astimezone(poland).strftime("%H:%M %Z")) deadline_msg = deadline_msg %(day, time,game_stub, game_stub) msg = message %(game_name, deadline_msg) for player in players: if species_num != None and species_num != player['num']: print("skipping %s - %s" %(player['num'], player['name'])) continue orders = "%s/sp%s.ord" %(data_dir, player['num']) if os.path.isfile(orders): print("found orders for %s" %(player['name'])) continue subject = "FH %s Orders Reminder - %s" % (game_stub, player['name']) if not test_flag: print("Mailing reminder to %s (sp %s)" %(player['email'], player['name'])) config.send_mail(subject, player['email'], msg) else: print("Writing .test file") with open("sp%s.test"%(player['num']), "w") as f: f.write("To: %s\n" %( player['email'])) f.write("Subject: %s\n" %( subject)) f.write(msg)
def refreshLastTime(self, line): timestamp = datetime.strptime(line[8:], "%y%m%d %H:%M:%S") if self._GENERAL_CONFIG["TIMEZONE"]: timestamp = timestamp.replace(tzinfo=tz.tzutc()).astimezone( zoneinfo.gettz(self._GENERAL_CONFIG["TIMEZONE"])) self._last_time = timestamp.isoformat()
def get_tz(cls, tz): return zoneinfo.gettz(cls.TZ_ALIASES.get(tz, tz))
def gettz(name=None): tz = None if not name: try: name = os.environ["TZ"] except KeyError: pass if name is None or name == ":": for filepath in TZFILES: if not os.path.isabs(filepath): filename = filepath for path in TZPATHS: filepath = os.path.join(path, filename) if os.path.isfile(filepath): break else: continue if os.path.isfile(filepath): try: tz = tzfile(filepath) break except (IOError, OSError, ValueError): pass else: tz = tzlocal() else: if name.startswith(":"): name = name[:-1] if os.path.isabs(name): if os.path.isfile(name): tz = tzfile(name) else: tz = None else: for path in TZPATHS: filepath = os.path.join(path, name) if not os.path.isfile(filepath): filepath = filepath.replace(' ','_') if not os.path.isfile(filepath): continue try: tz = tzfile(filepath) break except (IOError, OSError, ValueError): pass else: tz = None if tzwin: try: tz = tzwin(name) except OSError: pass if not tz: from dateutil.zoneinfo import gettz tz = gettz(name) if not tz: for c in name: # name must have at least one offset to be a tzstr if c in "0123456789": try: tz = tzstr(name) except ValueError: pass break else: if name in ("GMT", "UTC"): tz = tzutc() elif name in time.tzname: tz = tzlocal() return tz
def main(argv): import getopt try: opts, args = getopt.getopt(argv, "h", ["map", "zones", "test", "help"]) except getopt.GetoptError: usage() if not opts: opts.append(('--test', '')) for opt, arg in opts: if opt == "--map": c = CountryLookup() cd = c.get_country_dict() del cd[''] prettify('', c.get_country_dict(), ",", " '': 'Unknown',\n") elif opt == "--zones": prettify("COUNTRY_ZONES = ", CountryLookup.get_zones(), "',", " 'unknown': 'UTC',\n") elif opt == "--test": resources_lib_re = re.compile(r"[^a-z]+resources[^a-z]+lib$") sys.path.insert(0, resources_lib_re.sub("", sys.path[0])) from dateutil import zoneinfo tried = {} saw_error = False for country, zone in COUNTRY_ZONES.iteritems(): if zone not in tried: x = zoneinfo.gettz(zone) if x is None: # To fix this, we'll need new data from http://www.twinsun.com/tz/tz-link.htm sys.stdout.write( "Timezone %s is not available in the dateutil zoneinfo tar file!!!\n" % zone) saw_error = True tried[zone] = 1 if saw_error: sys.stdout.write( "^-- Visit http://www.twinsun.com/tz/tz-link.htm for updated info.\n" ) else: sys.stdout.write( "All the listed timezone names were found in the zoneinfo tar file.\n" ) c = CountryLookup() saw_error = False for station, country in c.get_country_dict().iteritems(): zone = COUNTRY_ZONES.get(country.lower(), None) if zone is None: sys.stdout.write("Missing country timezone for %s\n" % country) saw_error = True if saw_error: sys.stdout.write( "^-- Run with --zones to output a new country table.\n") else: sys.stdout.write("All needed country names were found.\n") elif opt in ("-h", "--help"): usage() else: sys.exit(42) # Impossible...