473,480 Members | 5,031 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Am I asking too much from datetime?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I'm trying to use the datetime module. The following seems to work
any day except the first day of the month:
import datetime
today = datetime.date.today()
print today 2004-06-01 yesterday = datetime.date(today.year, today.month, today.day-1)

Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: day is out of range for month

In other languages (specifically Java, but I'm sure these are others),
this would come out as "2004-05-31".

Is this is a bug, or outside of the functionality of datetime?
- --
Mike Hostetler
th*****@binary.net
http://www.binary.net/thehaas
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFAvGe4aP33v4T41CURAkQGAKChO0yxPDxHwXHTsNOb0e O92zECGgCdHKZ2
Irj6f33KpdXvwxsIhkM+Bo8=
=IRWr
-----END PGP SIGNATURE-----
Jul 18 '05 #1
4 1566
th*****@binary.net wrote:
I'm trying to use the datetime module. The following seems to work
any day except the first day of the month:

import datetime
today = datetime.date.today()
print today
2004-06-01
yesterday = datetime.date(today.year, today.month, today.day-1)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: day is out of range for month

In other languages (specifically Java, but I'm sure these are others),
this would come out as "2004-05-31".

Is this is a bug, or outside of the functionality of datetime?
--
Mike Hostetler
th*****@binary.net
http://www.binary.net/thehaas


Of course it does not work !
0 is not a valid day...
You should do something like:
import datetime
today = datetime.date.today()
print today 2004-06-01 oneDay = datetime.timedelta(days=1)
print oneDay 1 day, 0:00:00 print today-oneDay

2004-05-31
--
Yermat

Jul 18 '05 #2
>>>>yesterday = datetime.date(today.year, today.month, today.day-1)
Is this is a bug, or outside of the functionality of datetime?


Try writing it like this:
import datetime
today = datetime.date.today()
print today 2004-06-01 yesterday = today - datetime.timedelta(days=1)
print yesterday

2004-05-31

Cheers,
Brian

Jul 18 '05 #3
Yermat <lo**@fejoz.net> wrote:
Of course it does not work !
0 is not a valid day...
You should do something like:


Except that it *does* work in other languages . . . that was part of
my point. Here's a sample from some working Java code:

// this works in January!
Calendar now = Calendar.getInstance();
Calendar lastmonth = Calendar.getInstance();
lastmonth.set(now.get(Calendar.YEAR),
now.get(Calendar.MONTH)-1,
now.get(Calendar.DATE));
>>> import datetime
>>> today = datetime.date.today()
>>> print today 2004-06-01 >>> oneDay = datetime.timedelta(days=1)
>>> print oneDay 1 day, 0:00:00 >>> print today-oneDay

2004-05-31


Got it. Cool -- thanks.
--
Mike Hostetler
th*****@binary.net
http://www.binary.net/thehaas
Jul 18 '05 #4
th*****@binary.net wrote:
Yermat <lo**@fejoz.net> wrote:
[...]

Except that it *does* work in other languages . . . that was part of
my point. Here's a sample from some working Java code:
[...]


I understand your felling but I really believe it SHOULD not go that way
in other language... And so understand the python library point of view
to be the better one.

Anyway that's another debate ;-)

--
Yermat

Jul 18 '05 #5

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

Similar topics

4
2507
by: Max M | last post by:
# -*- coding: latin-1 -*- """ I am currently using the datetime package, but I find that the design is oddly asymmetric. I would like to know why. Or perhaps I have misunderstood how it...
15
12201
by: Ingmar | last post by:
Simple comparison tests we have performed show that System.Math functions in C# are much slower than corresponding functions in C++. Extreme examples are System.Math.Exp() and System.Math.Tan(),...
5
1741
by: Droopy | last post by:
Hi, I am writing a C# program that is calling C++ legacy code. I wrote a C++ managed wrapper for this legacy code. It seems to work well. The legacy code is handling serial (RS232 or RS422)...
14
2338
by: Roy Gourgi | last post by:
Hi, How much is C# slower than C++? TIA Roy
6
8960
by: Ante Perkovic | last post by:
Hi, How to declare datetime object and set it to my birthday, first or last day of this month or any other date. I can't find any examples in VS.NET help! BTW, what is the difference...
9
4893
by: Phil B | last post by:
I am having a problem with a datetime from a web services provider The provider is sending the following SOAP response <?xml version="1.0" encoding="utf-8"?> <soap:Envelope...
12
1831
by: jimocz | last post by:
Did I do something wrong? I cross posted this on the dotnet development group -- sorry if it is a double posting but we are seriously considering going to c# and this could be a show stopper. ...
0
16473
yasirmturk
by: yasirmturk | last post by:
Standard Date and Time Functions The essential date and time functions that every SQL Server database should have to ensure that you can easily manipulate dates and times without the need for any...
34
3487
by: raylopez99 | last post by:
StringBuilder better and faster than string for adding many strings. Look at the below. It's amazing how much faster StringBuilder is than string. The last loop below is telling: for adding...
0
7040
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
6905
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
7041
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
6908
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4772
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4478
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2994
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
561
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.