[Question] Is there a Linux system call to get number of days for a given month?

This is a discussion on [Question] Is there a Linux system call to get number of days for a given month? within the Linux Programming forums, part of the Platform Specific Boards category; Hello guys, I am not asking how to write a C/C++ program to get total number of days in a ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    16

    Question [Question] Is there a Linux system call to get number of days for a given month?

    Hello guys,

    I am not asking how to write a C/C++ program to get total number of days in a given month.

    I was asked to use the Linux system call to get the number. The only thing I have found so far is the:
    Code:
    int daysInMonth () const // in  qdatetime.h
    And, that actually belongs to Qt. not Linux/posix, etc.

    Does someone happen to know ....

    Thanks!

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    No, there is no such system call, and there really shouldn't be one either. That type of code belongs in user space, not in the kernel.
    Linux Syscall Reference

    But you can (ab)use mktime in the c(++) standard library. Give it day 0 of a month and it will return the last day of the previous month.

    Code:
    #include <iostream>
    #include <ctime>
    
    int main()
    {
    	int year, month;
    	std::cout << "Enter year: ";
    	std::cin >> year;
    	std::cout << "Enter month: ";
    	std::cin >> month;
    	
    	tm dayofmonth= {0};
    	dayofmonth.tm_year = year - 1900;
    	dayofmonth.tm_mon = month; // months are 0-11 so this is actually the month following the one we want.
    	dayofmonth.tm_mday = 0;
    	
    	mktime(&dayofmonth);
    	
    	std::cout << year << "/" << month << " has " << dayofmonth.tm_mday << " days" << std::endl;
    	
    	return 0;
    }
    Last edited by _Mike; 08-29-2011 at 10:51 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,830
    > I am not asking how to write a C/C++ program to get total number of days in a given month.
    Considering that the answer is 11 known constants, plus one leap year rule for February, what exactly is the difficulty here?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    16
    Quote Originally Posted by _Mike View Post
    No, there is no such system call, and there really shouldn't be one either. That type of code belongs in user space, not in the kernel.
    Linux Syscall Reference

    But you can (ab)use mktime in the c(++) standard library. Give it day 0 of a month and it will return the last day of the previous month.

    Code:
    #include <iostream>
    #include <ctime>
    
    int main()
    {
    	int year, month;
    	std::cout << "Enter year: ";
    	std::cin >> year;
    	std::cout << "Enter month: ";
    	std::cin >> month;
    	
    	tm dayofmonth= {0};
    	dayofmonth.tm_year = year - 1900;
    	dayofmonth.tm_mon = month; // months are 0-11 so this is actually the month following the one we want.
    	dayofmonth.tm_mday = 0;
    	
    	mktime(&dayofmonth);
    	
    	std::cout << year << "/" << month << " has " << dayofmonth.tm_mday << " days" << std::endl;
    	
    	return 0;
    }
    Hi Mike,
    Thank you very much for the information and explanation! It's perfect!

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    16
    Quote Originally Posted by Salem View Post
    > I am not asking how to write a C/C++ program to get total number of days in a given month.
    Considering that the answer is 11 known constants, plus one leap year rule for February, what exactly is the difficulty here?
    I think the exact difficulty is being asked to replace the simple solution as you described above with a "Linux system call". And, such a "Linux system call" does not actually exist.

  6. #6
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    Quote Originally Posted by learn View Post
    I think the exact difficulty is being asked to replace the simple solution as you described above with a "Linux system call". And, such a "Linux system call" does not actually exist.
    Then you could get a timeval structure with gettimeofday() ..which is a syscall and then use an equivalent of the above procedure.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making a Linux system call
    By ShamWow in forum Linux Programming
    Replies: 5
    Last Post: 03-29-2010, 11:59 AM
  2. System function Call - Nowait possible - linux environment?
    By mickle026 in forum C++ Programming
    Replies: 1
    Last Post: 10-17-2009, 02:55 PM
  3. DateDiff in Month & Days format
    By rweworld in forum C Programming
    Replies: 3
    Last Post: 09-26-2007, 10:17 AM
  4. printing days for a particular month
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-07-2006, 06:37 AM
  5. getting number for Month
    By Jasonymk in forum C# Programming
    Replies: 2
    Last Post: 08-09-2005, 04:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21