473,545 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
+ 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.t oday()
print today 2004-06-01 yesterday = datetime.date(t oday.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)

iD8DBQFAvGe4aP3 3v4T41CURAkQGAK ChO0yxPDxHwXHTs NOb0eO92zECGgCd HKZ2
Irj6f33KpdXvwxs IhkM+Bo8=
=IRWr
-----END PGP SIGNATURE-----
Jul 18 '05 #1
4 1570
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.t oday()
print today
2004-06-01
yesterday = datetime.date(t oday.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.t oday()
print today 2004-06-01 oneDay = datetime.timede lta(days=1)
print oneDay 1 day, 0:00:00 print today-oneDay

2004-05-31
--
Yermat

Jul 18 '05 #2
>>>>yesterday = datetime.date(t oday.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.t oday()
print today 2004-06-01 yesterday = today - datetime.timede lta(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.getIns tance();
Calendar lastmonth = Calendar.getIns tance();
lastmonth.set(n ow.get(Calendar .YEAR),
now.get(Calenda r.MONTH)-1,
now.get(Calenda r.DATE));
>>> import datetime
>>> today = datetime.date.t oday()
>>> print today 2004-06-01 >>> oneDay = datetime.timede lta(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
2516
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 should be used? I can make a datetime easily enough
15
12225
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(), which both seem to average 50 times slower than their C++ counterparts. (Tests are done using optimized Release configurations outside the Visual...
5
1745
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) lines. To keep a serial line "alive", i have to call regularly a function. A c# thread is calling about every 25 ms this function (based on "WaitOne...
14
2341
by: Roy Gourgi | last post by:
Hi, How much is C# slower than C++? TIA Roy
6
8969
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 between date and datetime classes? Please, help
9
4903
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 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"...
12
1838
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. I ran the following C# program and it ran in 9 seconds (give or take 1 sec) using System; using System.Collections.Generic; using System.Text;
0
16480
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 formatting considerations at all. They are simple, easy, and brief and you should use them any time you need to incorporate any date literals or...
34
3497
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 200000 strings of 8 char each, string took over 25 minutes while StringBuilder took 40 milliseconds! Can anybody explain such a radical...
0
7425
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7682
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7935
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7449
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7780
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5069
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1911
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 we have to send another system
1
1037
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.