Python Tutorial

Showing posts with label time and date. Show all posts
Showing posts with label time and date. Show all posts

Wednesday, February 20, 2013

date compare - code snippet

import datetime

a = datetime.date(2012,1,31) # YYYY, MM, DD
b = datetime.date(2009,10,31) # YYYY, MM, DD

print a == b
print a > b
print a < b


Output:
False
True
False

Sunday, November 18, 2012

python dateutil : date operation is fun


Here some example code of operate date with dateutil. It very easy, reduce lots of pain & code. I think from now you like date operation much.
All source code available on github

 
from dateutil.parser import parse
from dateutil.relativedelta import *

from datetime import *
print "parse example"
print parse('Mon, 11 Jul 2011 10:01:56 +0200 (CEST)')
s = "Today is 25 of September of 2003, exactly at 10:49:41 with timezone -03:00."
print parse(s, fuzzy=True)


today = datetime.now()
print 20*"=="
print today # today
print today+relativedelta(months=+1) # Next month
print today+relativedelta(years=+1) # Next year
print today+relativedelta(months=+1, weeks=+1) # Next month, plus one week
print today+relativedelta(months=+1, weeks=+1, hour=10) # Next month, plus one week, at 10am
print today+relativedelta(years=+1,months=-1) # One month before one year

print 20*"++"
print today+relativedelta(weekday=FR) # Next friday
print today+relativedelta(days=+1,weekday=SU(+1)) # Next sunday, but not today
print today+relativedelta(day=31, weekday=FR(-1)) # Last friday in this month


print "Calculate Age"
birthday = datetime(1971, 4, 5, 12, 0)
age = relativedelta(today, birthday)  # calculate age
print age
print "years: ",age.years," months: ",age.months," day: ",age.days




Output:
parse example
2011-07-11 10:01:56+02:00
2003-09-25 10:49:41-03:00
========================================
2012-11-18 23:35:31.191000
2012-12-18 23:35:31.191000
2013-11-18 23:35:31.191000
2012-12-25 23:35:31.191000
2012-12-25 10:35:31.191000
2013-10-18 23:35:31.191000
++++++++++++++++++++++++++++++++++++++++
2012-11-23 23:35:31.191000
2012-11-25 23:35:31.191000
2012-11-30 23:35:31.191000
Calculate Age
relativedelta(years=+41, months=+7, days=+13, hours=+11, minutes=+35, seconds=+31, microseconds=+191000)
years:  41  months:  7  day:  13

python dateutil


Python dateutil module provides powerful extensions to the standard datetime module. It reduce pain of handling date. Here main tutorial page.

Install:
pip install python-dateutil

Thursday, October 18, 2012

Python datetime to timestamp two way conversion


It is very nice to having datetime to timestamp two way conversion. Python simply give us this feature. Lets have a look

 
from datetime import datetime
import time

date= datetime(2012,10,16) # You set date as datetime.now()
print date
print time.mktime(date.timetuple())
print datetime.fromtimestamp(time.mktime(date.timetuple()))


Output:
2012-10-16 00:00:00
1350324000.0
2012-10-16 00:00:00

Thursday, July 21, 2011

Date time

Python date time comparison:
For python date time comparison you can directly compare between two date and also make some mathematical operation
import datetime
import time
before=datetime.datetime.now()
print "before "+str(before)
time.sleep(5)
after=datetime.datetime.now()
print "after "+str(after)
difference=after-before
print "difference "+str(difference)



before 2011-07-21 15:38:13.330000
after 2011-07-21 15:38:18.347000
difference 0:00:05.017000

Thursday, August 19, 2010

python : time

'''
   This code use python time class syntax
'''
import time

'''
   return current date and time
'''
print "time.ctime() ",time.ctime()
'''
   Return current CPU time
'''
print "time.clock() ",time.clock()





Output:
time.ctime()  Thu Aug 19 14:46:02 2010
time.clock()  107.072719637