2011-08-30 19 views

उत्तर

70

कि क्या करना चाहिए यह

import time 

date_time = '29.08.2011 11:05:02' 
pattern = '%d.%m.%Y %H:%M:%S' 
epoch = int(time.mktime(time.strptime(date_time, pattern))) 
print epoch 
+7

यह आपके सर्वर के टाइमज़ोन के लिए ही काम करता है। – eumiro

+1

चूंकि mktime का उपयोग किया जा रहा है, क्या इसका मतलब है कि आपको यूटीसी में लौटे हुए युग को समायोजित करना चाहिए? – ChuckCottrill

+0

उत्तर के लिए धन्यवाद! Upvoted। –

2

उपयोग strptime समय पार्स, और उस पर time() फोन यूनिक्स टाइमस्टैम्प प्राप्त करने के लिए।

10

मान लें कि आप एक 24 घंटे समय स्वरूप का उपयोग कर रहे हैं:

import time; 
t = time.mktime(time.strptime("29.08.2011 11:05:02", "%d.%m.%Y %H:%M:%S")); 
4
import time 
def expires(): 
    '''return a UNIX style timestamp representing 5 minutes from now''' 
    return int(time.time()+300) 
9
Your code will behave strange if 'TZ' is not set properly, e.g. 'UTC' or 'Asia/Kolkata' 

So, you need to do below 

>>> import time, os 
>>> d='2014-12-11 00:00:00' 
>>> p='%Y-%m-%d %H:%M:%S' 
>>> epoch = int(time.mktime(time.strptime(d,p))) 
>>> epoch 
1418236200 
>>> os.environ['TZ']='UTC' 
>>> epoch = int(time.mktime(time.strptime(d,p))) 
>>> epoch 
1418256000 
+0

एक टाइपो, आपका कोड स्निपेट होना चाहिए: os.environ ['TZ'] = 'UTC'। ध्यान दें कि कुंजी टीजेड ' – sateesh

2

अगर आप यूटीसी gm कार्यों में से कुछ की कोशिश करना चाहते हैं:

import time 
import calendar 

date_time = '29.08.2011 11:05:02' 
pattern = '%d.%m.%Y %H:%M:%S' 
utc_epoch = calendar.timegm(time.strptime(date_time, pattern)) 
print utc_epoch 
संबंधित मुद्दे

 संबंधित मुद्दे