473,809 Members | 2,791 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

script for seconds in given month?

Hi, does anyone happen to know of a script that would return the
number of seconds in a month if I give it a month and a year?

My python is a little weak, but if anyone could offer some suggestions
I think I could handle it myself, or if anyone happens to know of a
script already written that performs this I would be extremely
grateful.

Thanks!
-Ed

Apr 16 '07 #1
19 2737
Jim
On Apr 16, 12:22 pm, "edfialk" <edfi...@gmail. comwrote:
Hi, does anyone happen to know of a script that would return the
number of seconds in a month if I give it a month and a year?

My python is a little weak, but if anyone could offer some suggestions
I think I could handle it myself, or if anyone happens to know of a
script already written that performs this I would be extremely
grateful.
Probably there are sophisticated answers, but have you tried
something like:

monthDays={'Jan ':31,'Feb':28, ..}
secs=60*60*24*m onthDays[thisMonth]
if (thisMonth=='Fe b'
and isLeap(thisYear )):
secs+=60*60*24
return secs

?

Apr 16 '07 #2
On Apr 16, 6:22 pm, "edfialk" <edfi...@gmail. comwrote:
Hi, does anyone happen to know of a script that would return the
number of seconds in a month if I give it a month and a year?
something like this might work, it should event handle DST correctly.
You could read up on mktime() if you want to make sure.

from time import mktime
def secondsInMonth( year, month):
s1 = mktime((year,mo nth,1,0,0,0,0,0 ,-1))
s2 = mktime((year,mo nth+1,1,0,0,0,0 ,0,-1))
return s2-s1

/Matt

Apr 16 '07 #3

Mattfrom time import mktime
Mattdef secondsInMonth( year, month):
Matt s1 = mktime((year,mo nth,1,0,0,0,0,0 ,-1))
Matt s2 = mktime((year,mo nth+1,1,0,0,0,0 ,0,-1))
Matt return s2-s1

Probably won't work if month==12. ;-)

Skip
Apr 16 '07 #4
On Apr 16, 11:22 am, "edfialk" <edfi...@gmail. comwrote:
Hi, does anyone happen to know of a script that would return the
number of seconds in a month if I give it a month and a year?

My python is a little weak, but if anyone could offer some suggestions
I think I could handle it myself, or if anyone happens to know of a
script already written that performs this I would be extremely
grateful.

Thanks!
-Ed
Do you need to handle leap seconds too? (not a joke)

-- Paul

Apr 16 '07 #5
Jim: I need years too, basically from 1960-2000. Don't want to
hardcode all those days :)

Matt: Thanks, I will try this out.

Paul: I don't believe we need leap seconds. Leap days definitely.

I'll let you know how Matt's code works. Any other suggestions feel
free to let me know.

Thanks all!
-Ed

Apr 16 '07 #6
On Apr 16, 10:18 am, s...@pobox.com wrote:
Mattfrom time import mktime
Mattdef secondsInMonth( year, month):
Matt s1 = mktime((year,mo nth,1,0,0,0,0,0 ,-1))
Matt s2 = mktime((year,mo nth+1,1,0,0,0,0 ,0,-1))
Matt return s2-s1

Probably won't work if month==12. ;-)

Skip


Actually, mktime as described in the C Standard does not
restrict members such as tm_mon of struct tm to the
range 0-11 (1-12 for the corresponding entry in the
time tuple in Python). So the underlying struct in
CPython may be normalized and return a perfectly valid
time even if month is 12.

--
Regards,
Steven
Apr 16 '07 #7

import datetime

def first_day_of_ne xt_month( year, month ):
"""returns the first day of the next month
>>first_day_of_ next_month(2007 ,5)
datetime.dateti me(2007, 6, 1, 0, 0)
>>first_day_of_ next_month(2007 ,12)
datetime.dateti me(2008, 1, 1, 0, 0)
"""
oneday = datetime.timede lta(days=1)
day = datetime.dateti me(year, month, 1)
if day.day == 1:
day += oneday
while day.day != 1:
day += oneday
return day
from time import mktime
def secondsInMonth( year, month):
s1 = mktime((year,mo nth,1,0,0,0,0,0 ,-1))
day = first_day_of_ne xt_month(year, month)
year = day.year
month = day.month
s2 = mktime((year,mo nth+1,1,0,0,0,0 ,0,-1))
return s2-s1
year = 2007
month = 2
print secondsInMonth( year, month)

sk**@pobox.com wrote:
Mattfrom time import mktime
Mattdef secondsInMonth( year, month):
Matt s1 = mktime((year,mo nth,1,0,0,0,0,0 ,-1))
Matt s2 = mktime((year,mo nth+1,1,0,0,0,0 ,0,-1))
Matt return s2-s1

Probably won't work if month==12. ;-)

Skip
--
Shane Geiger
IT Director
National Council on Economic Education
sg*****@ncee.ne t | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy
Apr 16 '07 #8
On Apr 16, 12:49 pm, "edfialk" <edfi...@gmail. comwrote:
Jim: I need years too, basically from 1960-2000. Don't want to
hardcode all those days :)

Matt: Thanks, I will try this out.

Paul: I don't believe we need leap seconds. Leap days definitely.

I'll let you know how Matt's code works. Any other suggestions feel
free to let me know.

Thanks all!
-Ed
I googled for "NIST leap second" and found this table online of leap
seconds, if you do in fact need them: http://tf.nist.gov/pubs/bulletin/leapsecond.htm

-- Paul

Apr 16 '07 #9
edfialk <ed*****@gmail. comwrote:
Hi, does anyone happen to know of a script that would return the
number of seconds in a month if I give it a month and a year?

My python is a little weak, but if anyone could offer some suggestions
I think I could handle it myself, or if anyone happens to know of a
script already written that performs this I would be extremely
grateful.
import calendar

def seconds_in_mont h(month, year):
nomatter, daysinmonth = calendar.monthr ange(year, month)
return daysinmonth * 24 * 60 * 60
Alex
Apr 17 '07 #10

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

Similar topics

4
2890
by: Damien Renwick | last post by:
I have a php script which simply stops midway through a while loop that processes records returned by a MySQL query. The HTML page continues trying to load the page but the php has stopped running and eventually I get a timeout. The script is running in my development environment which consists of php 4.3.4, Apache 1.3.28 and MySQL 4.0.17, installed on Windows XP with Service Pack 2. I suspect that there may be a problem with my...
0
6033
by: Will Seay | last post by:
At the end of this message I've pasted a script we're trying to modify slightly. I don't believe it is VBscript or javascript but these are the closest groups I could find with my limited programming knowledge. Basically, we are trying to add a few lines to this script that will execute a few shell commands (see comments at the very end of the code). We think this may be ActionScript2 but aren't sure. If you can offer any help, or know...
7
2210
by: Dennis Roberts | last post by:
I have a script to parse a dns querylog and generate some statistics. For a 750MB file a perl script using the same methods (splits) can parse the file in 3 minutes. My python script takes 25 minutes. It is enough of a difference that unless I can figure out what I did wrong or a better way of doing it I might not be able to use python (since most of what I do is parsing various logs). The main reason to try python is I had to look at...
3
11122
by: Stuart H | last post by:
Since bash is horrible at date statements i had to convert my bash script to perl....now i have a headache.. I had help to convert but now in modifying i have messed it up.. i'm checking a returned value from a sql query and comparing it against the date. The two possible output types $ /usr/local/bin/sqsh -Uuser -Ppassword -S sql1 -h -i test.sql
3
9252
by: kaming | last post by:
Dear all, Do any one have idea how to obtain the exactly number of seconds in a month with given month and year using DB2 given functions? The formula is as simple as "No_of_days_for_a_month" * 60 * 60 * 24 .. but It is a headache for me to find the No_of_days_for_a_month... in DB2 Thanks very much! Henry
1
1907
by: Rob | last post by:
Access Gurus, This script was used to view the reports on the web by choosing a date on or before the current day.Recently it stopped working and i am not able to figure where the problem is. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd"> <HTML lang=en xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"> <HEAD>
14
5417
by: asdf | last post by:
I have a python script whose output i want to dynamically display on a webpage which will be hosted using Apache. How do I do that? thanks
15
3258
by: Lawrence Krubner | last post by:
Does anything about this script look expensive, in terms of resources or execution time? This script dies after processing about 20 or 25 numbers, yet it leaves no errors in the error logs. This is on a server that handles a fairly demanding site. The defaults, in php.ini, have all been cranked fairly high: scripts get 180 seconds to run, and they can have as much as 256 megs of RAM. The input for this script is coming from a textarea in...
0
9601
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10635
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10378
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9198
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7653
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5550
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4332
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
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3013
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.