Wednesday, 28 February 2018

Python timezone challenge- Day 17


  • the challenge here is to let the user choose the time zone from list of time zones and accordingly display the time
  •   
    import datetime
    import pytz
    
    available_zones ={"1":"African/Tunis",
     "2":"Asia/Kolkata",
     "3":"Australia/Adelaide",
     "4":"Europe/Brussels",
     "5":"Europe/London",
     "6":"Japan",
     "7":"Pacific/Tahiti"
    }
    print("Please Choose the timezone (or 0 to quit)")
    for place in sorted(available_zones):
        print("\t {} : {}".format(place,available_zones[place]))
    
    while True:
        choice = input()
        if choice == 0:
            break
        if choice in available_zones.keys():
            tz_to_display = pytz.timezone(available_zones[choice])
            world_time = datetime.datetime.now(tz=tz_to_display)
            print ("The time in {} is {} {} ".format(available_zones[choice],
                                 world_time.strftime("%A %x %X %z"),world_time.tzname()))
            print("LocalTime is {}".format(datetime.datetime.now().strftime("%A %x %X %z")))
            print("UTCTime is {}".format(datetime.datetime.utcnow().strftime("%A %x %X %z")))
    
     
    

    No comments:

    Post a Comment