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.