Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Datas e Fusos em Python

Datas e Fusos em Python

Fusos horários com Python

40fa6431238df7db2ba245186beb9d64?s=128

Nilo Ney Coutinho Menezes

February 06, 2016
Tweet

Transcript

  1. Datas de Fusos em Python PyNorte - Nilo Menezes

  2. Por que datas e fusos? • Na região Norte temos

    3 fusos horários • Se considerarmos o resto do país, teremos de 4 a 5 fusos dependendo do horário de verão • A Internet faz a manipulação de datas se tornar um problema frequente e normal
  3. Fusos

  4. E Python? • Módulo datetime • date • time •

    datetime • timedelta • pytz
  5. Detalhes • Para trabalhar com fuso horários, você precisa de

    datetime. Uma data sozinha não tem fuso. A hora sozinha também não
  6. Primeiros passos • Nomes de zona: >>> import pytz >>>

    from datetime import * >>> pytz.country_timezones["br"] ['America/Noronha', 'America/Belem', 'America/Fortaleza', 'America/Recife', 'America/Araguaina', 'America/Maceio', 'America/Bahia', 'America/Sao_Paulo', 'America/Campo_Grande', 'America/Cuiaba', 'America/Santarem', 'America/Porto_Velho', 'America/Boa_Vista', 'America/Manaus', 'America/Eirunepe', ‘America/Rio_Branco’] >>> manaus=pytz.timezone(“America/Manaus”) >>> datetime.now(tz=manaus) datetime.datetime(2016, 2, 6, 12, 42, 9, 901063, tzinfo=<DstTzInfo 'America/Manaus' AMT-1 day, 20:00:00 STD>) >>> [k for k in pytz.all_timezones if k.startswith("Br")] ['Brazil/Acre', 'Brazil/DeNoronha', 'Brazil/East', 'Brazil/West']
  7. Convertendo • Hora sem informação de zona >>> datetime.now() datetime.datetime(2016,

    2, 6, 17, 42, 15, 818676) • Adiciona informação de zona >>> datetime.now(tz=pytz.utc) datetime.datetime(2016, 2, 6, 16, 42, 30, 365507, tzinfo=<UTC>)
  8. Criando as zonas >>> manaus = pytz.timezone(“America/Manaus”) >>> belem =

    pytz.timezone(“America/Belem”) >>> datetime.now(tz=belem) datetime.datetime(2016, 2, 6, 13, 45, 20, 764109, tzinfo=<DstTzInfo 'America/Belem' BRT-1 day, 21:00:00 STD>) datetime.now(tz=manaus) datetime.now(tz=pytz.utc) datetime.now()
  9. Convertendo >>> agora = datetime.now(tz=pytz.utc) datetime.datetime(2016, 2, 6, 16, 46,

    29, 808149, tzinfo=<UTC>) >>> agora.astimezone(manaus) datetime.datetime(2016, 2, 6, 12, 46, 29, 808149, tzinfo=<DstTzInfo 'America/Manaus' AMT-1 day, 20:00:00 STD>) >>> agora.astimezone(belem) datetime.datetime(2016, 2, 6, 13, 46, 29, 808149, tzinfo=<DstTzInfo 'America/Belem' BRT-1 day, 21:00:00 STD>) >>> manaus.normalize(agora) datetime.datetime(2016, 2, 6, 12, 46, 29, 808149, tzinfo=<DstTzInfo 'America/Manaus' AMT-1 day, 20:00:00 STD>) >>> belem.normalize(agora) datetime.datetime(2016, 2, 6, 13, 46, 29, 808149, tzinfo=<DstTzInfo 'America/Belem' BRT-1 day, 21:00:00 STD>)
  10. Operações timedelta(minutes=5) >>> agora = datetime.now(tz=manaus) datetime.datetime(2016, 2, 6, 12,

    49, 16, 798101, tzinfo=<DstTzInfo 'America/Manaus' AMT-1 day, 20:00:00 STD>) >>> agora + timedelta(minutes=10) datetime.datetime(2016, 2, 6, 12, 59, 16, 798101, tzinfo=<DstTzInfo 'America/Manaus' AMT-1 day, 20:00:00 STD>) >>> agora2 = datetime.now(tz=belem) datetime.datetime(2016, 2, 6, 13, 50, 9, 78355, tzinfo=<DstTzInfo 'America/Belem' BRT-1 day, 21:00:00 STD>) >>> agora2 - agora datetime.timedelta(0, 52, 280254)
  11. python-dateutil >>> from dateutil.relativedelta import relativedelta >>> datetime.now(tz=pytz.utc) + relativedelta(months=1)

    datetime.datetime(2016, 3, 6, 16, 51, 55, 811080, tzinfo=<UTC>) >> datetime.now(tz=pytz.utc) + relativedelta(day=31, months=2) datetime.datetime(2016, 4, 30, 16, 52, 31, 801024, tzinfo=<UTC>)
  12. Próximos hangouts >>> from dateutil.rrule import * >>> from datetime

    import datetime >>> list(rrule(MONTHLY, count=4, byweekday=(SA(+1)), dtstart=datetime.now())) [datetime.datetime(2016, 2, 6, 18, 4, 52), datetime.datetime(2016, 3, 5, 18, 4, 52), datetime.datetime(2016, 4, 2, 18, 4, 52), datetime.datetime(2016, 5, 7, 18, 4, 52)] • python-dateutil: • Autor: Gustavo Niemeyer • https://labix.org/python-dateutil
  13. Obrigado ! próximo hangout: 05/03/2016