sponsor

sponsor

Slider

Recent Tube

Business

Technology

Life & style

Games

Sports

Fashion

» » Date and Time

हर एक Programming Language में Date और Time का concept होता है |
Python में भी Date और Time का concept होता है |
Date और time के लिए कुछ modules को import करना पड़ता है | वैसे ही कुछ Modules नीचे दिए गए है |
  1. time Module
  2. datetime Module
  3. calendar Module

time : time module पे बहुत सारे date और time से related functions, classes और constants होते है |
datetime : datetime module में भी constants, classes; date और time से related होते है | ये module date और time के साथ काम करने के लिए Object-Oriented Programming में बनाया गया है |
calendar : Calendar module में calendar से related कुछ functios और classes होते है |

Ticks/Timestamp in Python

12.00am January 1, 1970 से अबतक के बीते हुए seconds को 'Ticks' कहते है |
Python में 12.00am January 1, 1970 से अबतक के बीते हुए seconds को return करने के लिए time module में 'time()' ये function होता है | ये time() function बीते हुए seconds कोfloating-point number में return करता है |
For Example,
import time
print("Timestamp/ticks :",time.time())

#Output : 
#Timestamp : 1497628217.2675047


What is epoch in Python?

epoch ये हमेशा 12.00am January 1, 1970 होता है | Python में time module के 'gmtime()' इस function से epoch को return किया जाता है |
import time
print("epoch :",time.gmtime(0))

Output :
epoch : 
time.struct_time(
tm_year=1970, 
tm_mon=1, 
tm_mday=1, 
tm_hour=0, 
tm_min=0, 
tm_sec=0, 
tm_wday=3, 
tm_yday=1, 
tm_isdst=0)


'time' Module in Python

time module में date और time से related कुछ functions और constants होते है | time module में कुछ classes C या C++ में बने है | निचे दिए हुए example में gmtime() function से C या C++ में बने हुए 'struct_time' इस object को return किया गया है |
Source Code :
import time
print(time.localtime())
Output :
time.struct_time(
tm_year=2017, 
tm_mon=6, 
tm_mday=16, 
tm_hour=17, 
tm_min=4, 
tm_sec=32, 
tm_wday=4, 
tm_yday=167, 
tm_isdst=0)


struct_time structure में कुछ attributes होते है |
IndexAttributeValues
0tm_yearYear in 4 Digit(for eg, 2017)
1tm_mon1 to 12(Months)
2tm_mday1 to 28,29,30,31(Days)
3tm_hour0 to 23(Hours)
4tm_min0 to 59(Minutes)
5tm_sec0 to 61 (60 or 61 are leap-seconds)
6tm_wday0 to 6(Weekday)
7tm_yday1 to 366(Yearday)
8tm_isdstDST(Daylight Saving Time)
0, if not DST
1, if DST,
-1, if not known about DST

How to Get Current Date and Time using time Module ?

Example पर localtime() द्वारा current time को display किया गया है लेकिन इस return हुए time को आसानी से पढ़ा नहीं जा सकता है |
Source Code :
import time
print("Current Time :",time.localtime())
Output :
Current Time : time.struct_time(tm_year=2017, tm_mon=6, tm_mday=17, tm_hour=23, tm_min=16, tm_sec=7, tm_wday=5, tm_yday=168, tm_isdst=0)

Getting Current Date and Time in readable Format using time Module

Readable Format में current time को return करना हो तो 'asctime()' function का इस्तेमाल किया जाता है |
Source Code :
import time
print(time.asctime())
Output :
Sat Jun 17 23:38:53 2017

Important Attributes of 'time' Module

'time' Module AttributeDescription
altzoneये local DST timezone का offset; seconds में return करता है |
daylightअगर DST timezone defined होता है तो यहाँ पर Nonzero value होती है |
timezonelocal(non-DST) timezone का offset seconds में return करता है |
tznamelocal(non-DST) timezone और local DST timezone के साथ या उसके बिना tuple में return होते है |

Important Functions of 'time' Module

'time' Module FunctionDescription
asctime()दिए हुए time tuple को या gmtime() या localtime() से return होनेवाले struct_time को string में convert करके return करता है |
ctime()epoch local time(Jan 1 05:30:00 1970) और दिए गए seconds उसमे add करके उस time को string में return किया जाता है |
gmtime()epoch GMT time(Jan 1 00:00:00 1970) और दिए गए seconds उसमे add करके उस time को struct_time में return किया जाता है |
localtime()epoch local time(Jan 1 05:30:00 1970) और दिए गए seconds उसमे add करके उस time को struct_time में return किया जाता है |
mktime()दिए हुए time-tuple या struct_time को floating-point value को seconds में return करता है |
sleep()इस function का इस्तेमाल calling thread को suspend करने के लिए किया जाता है |
strftime()time-tuple या 'gmtime() या localtime()' से return होनेवाले 'struct_time' और दिए हुए format से 'time' को return किया जाता है |
strptime()दिए हुए format के अनुसार string को parse करके 'struct_time' में return किया जाता है |
time()epoch GMT time(Jan 1, 1970, 00:00:00) से लेकर बीते हुए time को seconds(floating-point number) मे return करता है |

«
Next
Newer Post
»
Previous
Older Post

No comments:

Leave a Reply