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

Datas e Fusos em Python

Nilo Ney Coutinho Menezes
February 06, 2016
10

Datas e Fusos em Python

Fusos horários com Python

Nilo Ney Coutinho Menezes

February 06, 2016
Tweet

Transcript

  1. 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
  2. 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
  3. 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']
  4. 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>)
  5. 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()
  6. 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>)
  7. 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)
  8. 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>)
  9. 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