473,396 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Expressing time.

I'm playing around times and dates. I'd like to determine the age of
particular sets of data in a user friendly matter.

I've got everything ready but dealing with time in a user friendly manner.

What would be a good way to express (in python):
time.localtime()+30 days
time.localtime()+5 minutes
time.localtime()+2 months
time.localtime()-2 years

I know I could probably just calculate out each of those in seconds but that
seems unelegant and very unfriendly (not to mention prone to error).

Is there any python facilities to make this an easier chore (aka
time.localtime()+months(3))?
(I included months as it is a special case in that you can't just
arbitrarily calculate months in seconds without being relative to a
particular month).

--
Sean
Jul 18 '05 #1
1 2296
"Sean" <nu*******@tfh.ca> writes:
I'm playing around times and dates. I'd like to determine the age of
particular sets of data in a user friendly matter. I've got everything ready but dealing with time in a user friendly manner. What would be a good way to express (in python):
time.localtime()+30 days
time.localtime()+5 minutes
time.localtime()+2 months
time.localtime()-2 years Is there any python facilities to make this an easier chore (aka
time.localtime()+months(3))?
(I included months as it is a special case in that you can't just
arbitrarily calculate months in seconds without being relative to a
particular month).


mx.DateTime makes these sort of calculations easy. I have a similarish task
in presenting users an easy way of specifying which day's log files to search
(hence in this case it was always a date in the past but the principle would
be similar). So I allow things like 'monday', 'tuesday' etc. and -n for n
days ago and several other variations. The interesting one in this instance
is:

dm_reg = re.compile (r'^(-?\d\d?)[/.](-?\d\d?)$')
this_day = today()

# dd/mm (-ve dd means count from end of month, -mm is mm months ago)
mt = re.match (dm_reg, user_input)
if mt:
dy,mon = map(int, mt.groups())
if mon < 0:
mon = this_day.month + mon
if mon <= 0:
mon = 12 + mon
dt = DateTime (this_day.year, mon, dy)
if dt > this_day:
dt = DateTime (this_day.year-1, mon, dy)
return dt

Which allows eg 1/-2 for the first of whatever month was 2 months ago or even
-1/-1 for the last day of the previous month. mx.DateTime is doing all the
hard work. You could cook up something similar to parse eg "-2m" "+2y" etc.

Possibly the new time stuff in 2.3 will do this also, I haven't looked at 2.3
yet.

Eddie
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Bart Nessux | last post by:
am I doing this wrong: print (time.time() / 60) / 60 #time.time has been running for many hours if time.time() was (21600/60) then that would equal 360/60 which would be 6, but I'm not getting...
5
by: David Stockwell | last post by:
I'm sure this has been asked before, but I wasn't able to find it. First off I know u can't change a tuple but if I wanted to increment a time tuple by one day what is the standard method to do...
76
by: Nick Coghlan | last post by:
GvR has commented that he want to get rid of the lambda keyword for Python 3.0. Getting rid of lambda seems like a worthy goal, but I'd prefer to see it dropped in favour of a different syntax,...
6
by: David Graham | last post by:
Hi I have asked this question in alt.php as the time() function as used in setcookie belongs to php - or does it belong equally in the javascript camp - bit confused about that. Anyway, can anyone...
4
by: Andrew Poulos | last post by:
How do I convert a length of time, measured in seconds, into a "point in time" type time interval or what's represented as: time (second,10,2) The format is: PS]] where: y: The number of...
8
by: peterbe | last post by:
What's the difference between time.clock() and time.time() (and please don't say clock() is the CPU clock and time() is the actual time because that doesn't help me at all :) I'm trying to...
3
by: Szabolcs Nagy | last post by:
I have to measure the time of a while loop, but with time.clock i always get 0.0s, although python manual sais: "this is the function to use for benchmarking Python or timing algorithms" So i...
6
by: Rebecca Smith | last post by:
Today’s question involves two time text boxes each set to a different time zone. Initially txtCurrentTime will be set to Pacific Time or system time. This will change with system time as we travel...
1
by: Doug1962 | last post by:
I autorun a query everyday at 3:30 pm and wish to be able to extract only the records that have been added to the database within my criteria since 3:30 pm the day before. I am unsure of how to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.