An warware: mai tsara kwanan wata

Aiki tare da kwanakin aiki ne na gama-gari a cikin kowane tsarin muhalli na shirye-shirye. Ko kuna shiga abubuwan da suka faru ko kuna yin nazari-tsakiyar lokaci, akwai yuwuwar za ku gamu da buฦ™atun sarrafa da tsara kwanakin a Python. Mai tsara kwanan wata kayan aiki ne mai matuฦ™ar amfani don irin waษ—annan ayyuka, saboda yana ba da hanya mai sauฦ™i don canza abubuwan kwanan wata da lokaci zuwa nau'ikan nau'ikan nau'ikan nau'ikan kirtani, ta yadda za a iya baje kolin yadda za a iya nunawa ko amfani da kwanakin.

Babban ษ—akin karatu da ke da hannu wajen tsara kwanan wata a Python shine lokacin rayuwa. Wannan dakin karatu wani bangare ne na daidaitattun kayan aiki na Python, ma'ana ana iya amfani da shi ba tare da buฦ™atar shigar da kowane ษ—akin karatu na ษ“angare na uku ba kafin a shigo da shi da amfani da shi. Tare da wannan ษ—akin karatu, sarrafa ranaku, lokuta, lokutan kwanan wata, timedeltas, da ฦ™ari ne iska.

Hanyar tsara kwanan wata ta amfani da ษ—akin karatu na kwanan wata na Python za a iya raba shi zuwa matakai na farko: (1) shigo da ษ—akin karatu, (2) ฦ™irฦ™irar abun kwanan wata, da (3) yin amfani da tsarin kirtani da ake so zuwa abun kwanan wata.

Misali na misali

Da ke ฦ™asa akwai misalin wannan tsari a aikace. A cikin wannan misali, za mu ฦ™irฦ™iri wani abu na kwanan wata da ke wakiltar kwanan wata da lokaci na yanzu, sa'an nan kuma mu tsara shi zuwa wakilcin kirtani:

import datetime    # step 1: import library

now = datetime.datetime.now()    # step 2: create date object
formatted_now = now.strftime("%Y-%m-%d %H:%M:%S")    # step 3: format date

print(formatted_now)	# will print out the date in the following format: "YYYY-MM-DD HH:MM:SS"

A cikin wannan misali, an tsara abin kwanan lokaci 'yanzu' ta amfani da lokacin aiki hanya, wanda ke canza abu na kwanan wata zuwa tsarin kirtani. Tsarin da aka ba da hanyar strftime yana ฦ™ayyade yadda aka tsara kirtan sakamako: "% Y-%m-%d %H:%M:%S" yana nuna cewa ya kamata a wakilta shekara a matsayin lamba huษ—u, sannan kuma wata da rana a matsayin lambobi masu lamba biyu, da sa'o'i, mintuna, da sakan kuma a matsayin lambobi masu lamba biyu.

Fahimtar strftime

Hanyar strftime tana nufin "lokacin tsarin kirtani". Hanya ce ta kwanan wata, kwanan wata, da abubuwan lokaci, kuma tana karษ“ar kirtani mai tsari azaman ma'aunin sa. Wannan tsarin kirtani ya ฦ™unshi "lambobin tsarin", waษ—anda umarni ne na musamman waษ—anda ke canzawa zuwa wasu sassan kwanan wata ko lokaci.

Anan akwai ฦ™ananan lambobin tsarin strftime da ake yawan amfani da su:

  • %Y: Shekarar lambobi huษ—u
  • %m: Watan lambobi biyu
  • %d: kwana biyu
  • %H: Sa'a mai lamba biyu (tsarin sa'o'i 24)
  • %M: Mintuna lambobi biyu
  • %S: daฦ™iฦ™a biyu lambobi

ฦ˜arin ษ—akunan karatu na tsara kwanan wata

Yayin da ษ—akin karatu na kwanan wata wani ษ“angare ne na daidaitattun sadaukarwa na Python, akwai wasu ษ—akunan karatu da ke akwai waษ—anda ke ba da ฦ™arin fasalulluka don aiki tare da kwanan wata. Wasu daga cikin waษ—annan sun haษ—a da:

  • kwanan wata: ฦ˜ari ga tsarin kwanan wata, yana ฦ™ara wasu fasaloli masu dacewa
  • pytz: Cikakken goyon bayan yankin lokaci
  • arrow: "Kwananni da lokutan da aka sauฦ™aฦ™a", yunฦ™urin sauฦ™aฦ™awa da haษ“aka sarrafa kwanan wata da tsarawa

A taฦ™aice, tsara kwanan wata aiki ne mai mahimmanci a cikin ayyukan Python da yawa, kuma wanda ษ—imbin kayan aikin sarrafa kwanan wata da tsararrun kayan aikin Python suka sanya shi mai sauฦ™i. Ta fahimtar yadda ake amfani da waษ—annan kayan aikin, za mu iya sauฦ™aฦ™a rayuwarmu sosai idan ya zo ga ayyuka da suka haษ—a da ranaku da lokuta.

Shafi posts:

Leave a Comment