وظائف الوقت في بايثون | اضبط 1 (الوقت ()، ctime ()، النوم () ...)

لقد حددت بايثون أ وحدة "الوقت" الذي يسمح لنا بمعالجة العمليات المختلفة المتعلقة بالوقت وتحويلاته وتمثيلاته التي تجد استخدامها في مختلف التطبيقات في الحياة. بداية الزمن بدأ القياس من 1 يناير 12:00 صباحًا 1970 وهذا الوقت بالذات يسمى " عصر " في بايثون.

العمليات في الوقت المحدد في بيثون 

وظيفة بايثون time.time()

وقت بايثون () تستخدم الدالة لحساب عدد ثواني انقضت منذ تلك الحقبة

Python3
   # Python code to demonstrate the working of   # time()   # importing 'time' module for time operations   import   time   # using time() to display time since epoch   print   (  'Seconds elapsed since the epoch are : '    end  =  ''  )   print   (  time  .  time  ())   

وظيفة بايثون time.gmtime()

بايثون جمتايم () ترجع الدالة أ هيكل مع 9 القيم يمثل كل منها سمة زمنية بالتسلسل. إنه يحول ثواني في سمات الوقت (أيام، سنوات، أشهر، الخ) حتى ثواني محددة من العصر. إذا لم يتم ذكر أي ثانية يتم احتساب الوقت حتى الوقت الحاضر. ويرد أدناه جدول سمات الهيكل. 

Index Attributes Values 0 tm_year 2008 1 tm_mon 1 to 12 2 tm_mday 1 to 31 3 tm_hour 0 to 23 4 tm_min 0 to 59 5 tm_sec 0 to 61 (60 or 61 are leap-seconds) 6 tm_wday 0 to 6 7 tm_yday 1 to 366 8 tm_isdst -1 0 1 where -1 means Library determines DST 
Python3
   # Python code to demonstrate the working of gmtime()   import   time   # using gmtime() to return the time attribute structure   print   (  'Time calculated acc. to given seconds is : '  )   print   (  time  .  gmtime  ())   

الإخراج: 

Time calculated acc. to given seconds is : time.struct_time(tm_year=2016 tm_mon=8 tm_mday=2 tm_hour=7 tm_min=12 tm_sec=31 tm_wday=1 tm_yday=215 tm_isdst=0) 

وظيفة بايثون time.asctime() وtime.ctime()

بايثون time.asctime() تأخذ الوظيفة سلسلة منسوبة للوقت تم إنتاجها بواسطة جمتايم () ويعود أ سلسلة مكونة من 24 حرفًا تشير إلى الوقت . بايثون time.ctime() ترجع الدالة أ سلسلة زمنية مكونة من 24 حرفًا ولكن يأخذ ثواني كوسيطة و يحسب الوقت حتى الثواني المذكورة . إذا لم يتم تمرير أي وسيطة يتم احتساب الوقت حتى الوقت الحاضر.

Python3
   # Python code to demonstrate the working of   # asctime() and ctime()   # importing 'time' module for time operations   import   time   # initializing time using gmtime()   ti   =   time  .  gmtime  ()   # using asctime() to display time acc. to time mentioned   print   (  'Time calculated using asctime() is : '    end  =  ''  )   print   (  time  .  asctime  (  ti  ))   # using ctime() to display time string using seconds    print   (  'Time calculated using ctime() is : '     end  =  ''  )   print   (  time  .  ctime  ())   

الإخراج: 

Time calculated using asctime() is : Tue Aug 2 07:47:02 2016 Time calculated using ctime() is : Tue Aug 2 07:47:02 2016 

وظيفة بايثون time.sleep()

تستخدم هذه الطريقة ل وقف تنفيذ البرنامج للوقت المحدد في الحجج.

Python3
   # Python code to demonstrate the working of   # sleep()   # importing 'time' module for time operations   import   time   # using ctime() to show present time   print   (  'Start Execution : '    end  =  ''  )   print   (  time  .  ctime  ())   # using sleep() to hault execution   time  .  sleep  (  4  )   # using ctime() to show present time   print   (  'Stop Execution : '    end  =  ''  )   print   (  time  .  ctime  ())   

الإخراج: 

Start Execution : Tue Aug 2 07:59:03 2016 Stop Execution : Tue Aug 2 07:59:07 2016 

وظيفة بايثون time.mktime()

في هذا المثال قمنا بإنشاء struct_time كائن يحتوي على مجموعة من القيم لكل حقل من حقوله، ثم قمنا بتمرير الكائن إلى الوقت.mktime() لتحويله إلى رقم الفاصلة العائمة الذي يمثل عدد الثواني منذ عصر يونكس.

Python3
   import   time   # Create a struct_time object representing a date and time   my_time   =   time  .  strptime  (  '2023-05-10 14:30:00'     '%Y-%m-  %d   %H:%M:%S'  )   # Convert the struct_time object to a floating-point number   seconds_since_epoch   =   time  .  mktime  (  my_time  )   print  (  'Seconds since epoch:'     seconds_since_epoch  )   

الإخراج:

Seconds since epoch: 1683709200.0 

وظيفة بايثون time.localtime()

في هذا المثال نسمي الوقت.الوقت المحلي () بدون وسيطة للحصول على التوقيت المحلي الحالي باعتباره struct_time.

Python3
   import   time   current_time   =   time  .  localtime  ()   print  (  current_time  )   

الإخراج:

time.struct_time(tm_year=2023 tm_mon=5 tm_mday=10 tm_hour=12 tm_min=42 tm_sec=51 tm_wday=2 tm_yday=130 tm_isdst=0) 

وظيفة بايثون time.strftime()

يأخذ سلسلة تنسيق كوسيطة أولى تحدد التنسيق المطلوب لسلسلة الإخراج.

Python3
   import   time   now   =   time  .  localtime  ()   formatted_time   =   time  .  strftime  (  '%Y-%m-  %d   %H:%M:%S'     now  )   print  (  formatted_time  )   

الإخراج:

2023-05-10 13:42:04