from machine import RTC
import network
import ntptime
import time
#Relog 
'''
station = network.WLAN(network.STA_IF)

def connect(id, pswd):
  ssid = id
  password = pswd
  if station.isconnected() == True:
    print("Already connected")
    return
  station.active(True)
  station.connect(ssid, password)
  while station.isconnected() == False:
    pass
  print("Connection successful")
  #print(station.ifconfig())
 
def disconnect():
  if station.active() == True: 
   station.active(False)
  if station.isconnected() == False:
    print("Disconnected") 
 
#connect("Wokwi-GUEST", "")# nombre de la red y contrasena
'''
'''
rtc = RTC()
ntptime.settime()
(year, month, day, weekday, hours, minutes, seconds, subseconds) = rtc.datetime()
#print ("UTC Time: ")
#print((hours, minutes, seconds))
sec = ntptime.time()

timezone_hour=2.0
timezone_sec = timezone_hour * 3600
sec = int(sec + timezone_sec)
(year, month, day, hours, minutes, seconds, weekday, yearday) = time.localtime(sec)
#print ("Hora Barcelona: ")
#print((year, month, day, hours, minutes, seconds))
#print((hours),':',(minutes),':',(seconds))
rtc.datetime((year, month, day, 0, hours, minutes, seconds, 0))
#disconnect()
'''
#otro

'''
from machine import Pin, Timer   #importing pin, and timer class
led= Pin(36, Pin.OUT)              # GPIO14 as led output

led.value(0)              #LED is off
timer=Timer(-1)
  
timer.init(period=1000, mode=Timer.ONE_SHOT, callback=lambda t:led.value(not led.value()))
#initializing the timer
'''
#menu
'''
def menu():
    print('1-Relog')
    print('2-Temporizador')
    print('3-Cronometro')
    print('4-Salir')
    opcion=int(input('ingrese una opcion: '))
    return opcion
opcion=0

def menu2():
    print('1-Bogota')
    print('2-Barcelona')
    print('3-New York')
    print('4-Salir')
    ciudad=int(input('Escoja una ciudad: '))
    return ciudad
ciudad=0

while opcion!=4:
    opcion=menu()
    if opcion==1:
        print('Seleccione una ciudad:')
        connect("Wokwi-GUEST", "")# nombre de la red y contrasena
        while opcion!=4:
            ciudad=menu2()
            if ciudad==1:
                print('La hora de Bogota es:')
                
                rtc = RTC()
                ntptime.settime()
                (year, month, day, weekday, hours, minutes, seconds, subseconds) = rtc.datetime()
                sec = ntptime.time()                
                timezone_hour=-5.0 
                timezone_sec = timezone_hour * 3600
                sec = int(sec + timezone_sec)
                (year, month, day, hours, minutes, seconds, weekday, yearday) = time.localtime(sec)
                print((hours),':',(minutes),':',(seconds))
                rtc.datetime((year, month, day, 0, hours, minutes, seconds, 0))
                   
            elif ciudad==2:
                
                print('La hora de Barcelona es')
                rtc = RTC()
                ntptime.settime()
                (year, month, day, weekday, hours, minutes, seconds, subseconds) = rtc.datetime()
                sec = ntptime.time()   
                timezone_hour = 2.0
                timezone_sec = timezone_hour * 3600
                sec = int(sec + timezone_sec)
                (year, month, day, hours, minutes, seconds, weekday, yearday) = time.localtime(sec)
                print((hours),':',(minutes),':',(seconds))
                rtc.datetime((year, month, day, 0, hours, minutes, seconds, 0))
                
            elif ciudad==3:
                print('La hora de New York es')
                rtc = RTC()
                ntptime.settime()
                (year, month, day, weekday, hours, minutes, seconds, subseconds) = rtc.datetime()
                sec = ntptime.time()   
                timezone_hour = -4.0
                timezone_sec = timezone_hour * 3600
                sec = int(sec + timezone_sec)
                (year, month, day, hours, minutes, seconds, weekday, yearday) = time.localtime(sec)
                print((hours),':',(minutes),':',(seconds))
                rtc.datetime((year, month, day, 0, hours, minutes, seconds, 0))
                
            elif ciudad==4:
                print('Volver al menu principal')
                disconnect()
                break
            continue    
            #else:
                #print('opcion no valida')  
    
    elif opcion==2:
        print('Ingrese el tiempo del temporizador')
        tm=int ( input( "tiempo en minutos"))
        ts=int ( input( "tiempo en segundos"))
        t=ts+(tm*60)
        for x in range (t,0,-1):
            s=x%60
            m=int(x/60)%60
            h=int(x/3600)
            print(f"{h:02}:{m:02}:{s:02}")
            time.sleep(1)
        print("Es hora!!!")
    elif opcion==3:
        print('Inicia el cronometro')
    elif opcion==4:
        print('Fin de la aplicacion')
    else:
        print('opcion no valida')     
'''
'''
disconnect()
'''
#limpiar pantalla
'''
print("\x1B\x5B2J", end="")
print("\x1B\x5BH", end="")

'''
#temporizador 
'''
import time

t=int ( input( "tiempo en segundos"))
for x in range (t,0,-1):
    s=x%60
    m=int(x/60)%60
    h=int(x/3600)
    print(f"{h:02}:{m:02}:{s:02}")
    time.sleep(1)
print("Es hora!!!")
'''
'''
import time
  
# Timer starts
starttime = time.time()
lasttime = starttime
lapnum = 1
value = ""
  
print("Press ENTER for each lap.\nType Q and press ENTER to stop.")
  
while value.lower() != "q":
              
    # Input for the ENTER key press
    value = input()
  
    # The current lap-time
    laptime = round((time.time() - lasttime), 2)
  
    # Total time elapsed since the timer started
    totaltime = round((time.time() - starttime), 2)
  
    # Printing the lap number, lap-time, and total time
    print("Lap No. "+str(lapnum))
    print("Total Time: "+str(totaltime))
    print("Lap Time: "+str(laptime))
            
    print("*"*20)
  
    # Updating the previous total time and lap number
    lasttime = time.time()
    lapnum += 1
  
print("Exercise complete!")
'''
'''
import time
 
seconds = time.time()
print("Time in seconds since the epoch:", seconds)
local_time = time.time(seconds)
print("Local time:", local_time)

'''
def time_convert(sec):
    mins=sec//60
    sec=sec%60
    hours=mins//60
    mins=mins%60
    print("tiempo total={0}:{1}:{2}",format(int(hours),int(mins),int(sec)))
input("enter  pa iniciar") #sw3.on()
start_time=time.time()
try:
    hours=0
    while True:
        for minutes in range(0,60):
            for seconds in range(0,60):
                
                time.sleep(1)
                print("\x1B\x5B2J", end="")
                print(hours,":",minutes,":",seconds+1)
                #print("\x1B\x5BH", end="")
except  KeyboardInterrupt:# if (sw2.on()):
    end_time=time.time()
    time_lapsed=end_time-start_time
    time_convert(time_lapsed)

tm=int ( input( "tiempo en minutos"))
ts=int ( input( "tiempo en segundos"))
t=ts+(tm*60)
for x in range (t,0,-1):
    s=x%60
    m=int(x/60)%60
    h=int(x/3600)
    print(f"{h:02}:{m:02}:{s:02}")
    time.sleep(1)
print("Es hora!!!")