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

  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 09: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
    39,660
    > 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.

  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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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.

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, 10:59 AM
  2. System function Call - Nowait possible - linux environment?
    By mickle026 in forum C++ Programming
    Replies: 1
    Last Post: 10-17-2009, 01:55 PM
  3. DateDiff in Month & Days format
    By rweworld in forum C Programming
    Replies: 3
    Last Post: 09-26-2007, 09: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, 03:35 AM