Validating time

This is a discussion on Validating time within the C++ Programming forums, part of the General Programming Boards category; Is this fair enough to validate time for year 2009.. Code: bool valid::validDayOfDate(const int& day) { return (day>0) && (day<=31); ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Validating time

    Is this fair enough to validate time for year 2009..

    Code:
    bool valid::validDayOfDate(const int& day)
    {
    	return (day>0) && (day<=31);
    }
    
    bool valid::validMonthOfDate(const int& month)
    {
    	return (month>0) && (month<=12);
    }
    
    bool valid::validYearOfDate(const int& year)
    {
    	return year==2009;
    }
    
    bool valid::validHourhOfDay(const int& hr)
    {
       return (hr>=0) && (hr<24);
    }
    
    bool valid::validMinuteOfDay(const int& min)
    {
    	return (min>=0) && (min<60);
    }

  2. #2
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Hi, I'm going to be honest here, I've never seen return values referenced in that way, aka:

    Code:
    return (day>0) && (day<=31);
    Instead of:

    Code:
    if (day > 0 && day <= 31)
       return 1;
    else
       return 0;
    But if that is a valid way of evaluation, and an if statement is not required, I cannot see any reason why that wouldn't work.
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,706
    Not really. You've got to deal with days in February and leap years. Plus, September, April, June and November have only thirty days. Plus, theGregorian calendar was only well-known around the world after AD 1582, so choose an appropriate epoch for the year.

    It would seem more convenient to validate 59 minutes and 59 seconds, since it wraps around to 0 anyway, but it depends on what's acceptable input I suppose. It makes perfect sense to time the microwave for 90 seconds and stuff, for example.

    Get crackin'

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by whiteflags View Post
    It would seem more convenient to validate 59 minutes and 59 seconds, since it wraps around to 0 anyway, but it depends on what's acceptable input I suppose. It makes perfect sense to time the microwave for 90 seconds and stuff, for example.

    Get crackin'
    reason i'm saying '<60' ... caters for 59 not 60

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by pobri19 View Post
    Code:
    if (x < 5)
       return 1;
    else
       return 0;
    same as

    return x < 5;

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You don't take into account that months have a different length.

    It also doesn't seem worth it to make a function that only accepts one particular year. Are you going to recompile the application next year etc?

    I have mostly avoided dealing with dates, but I'd rather use existing libraries, such as Boost.Datetime.

    Plus, theGregorian calendar was only well-known around the world after AD 1582, so choose an appropriate epoch for the year.
    In the Russian Empire (and hence in my country) this happened as lately as on February 2, 1918. (This is the reason why the 1917 communist revolution a.k.a the October Revolution has always been celebrated in November )

    In particular, since this country has been under various rules throughout the history, it also used Gregorian calendar for some time in the 17th-18th century if I'm not mistaken So dates far back in the history are relative to geographical locations.
    Last edited by anon; 01-10-2009 at 07:03 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Quote Originally Posted by csonx_p View Post
    same as

    return x < 5;
    Thanks, I'm surprised I haven't seen it used before like that :/
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It would be surprising to see it otherwise
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,307
    Quote Originally Posted by whiteflags View Post
    Not really. You've got to deal with days in February and leap years.
    Not with the only valid year being 2009 - look at the validYearOfDate() function.

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,706
    That one I will respond to. Were there ever more than 29 days in February? Certainly not in 2009.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Converting from normal date to TM structure

    I understand there' a function mktime() which can help me convert from tm to time_t, then i can calculate difference in seconds. But i have a unique situation here... A user shoul be able to enter two dates (start - end) manually in the format (month/day/year hour:minutes:seconds) ... Then is should first be able to convert from these start/end format to tm structure .. This is where my problem is...

    Here's my attempt though
    Code:
    	struct tm issueDate;
    	struct tm returnDate;
    
    	/*** 
    	 **
    	 * Convert from normal date to TM structure 
    	 **
    	 ***/
    
    	std::ostringstream issue_dymonyr, return_dymonyr;
    	std::ostringstream issue_hrminsec, return_hrminsec;
    
    	issue_dymonyr << issue_dt.getMonth() << "/" << issue_dt.getDay() << "/" << issue_dt.getYear();
    	return_dymonyr << return_dt.getMonth() << "/" << return_dt.getDay() << "/" << return_dt.getYear();
    	
    	issue_hrminsec << issue_dt.getHour() << ":" << issue_dt.getMinute() << ":" << "0";
    	return_hrminsec << return_dt.getHour() << ":" << return_dt.getMinute() << ":" << "0";
    
    	strptime(issue_dymonyr.str()+" "+issue_hrminsec.str(), "%D %T", &issueDate);
    	strptime(return_dymonyr.str()+" "+return_hrminsec.str(), "%D %T", &returnDate);
    I have an error here, says : "error C3861: 'strptime': identifier not found" .. i have #include <time.h> added

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Is there a reason you cannot use Boost?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    Is there a reason you cannot use Boost?
    Please expand? I do have boost configure, which funcition do i need from boost.. Or which header file

    thnx

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    If you would listen to feedback...
    Quote Originally Posted by anon View Post
    I have mostly avoided dealing with dates, but I'd rather use existing libraries, such as Boost.Datetime.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    Is there a reason you cannot use Boost?
    seem to be experiencing strange problems with the 'days' class from boost

    Code:
    	using namespace boost::gregorian;
    
    	std::ostringstream issue_dymonyr, return_dymonyr;
    	std::ostringstream issue_hrminsec, return_hrminsec;
    
    	issue_dymonyr << issue_dt.getMonth() << "-" << issue_dt.getDay() << "-" << issue_dt.getYear();
    	return_dymonyr << return_dt.getMonth() << "-" << return_dt.getDay() << "-" << return_dt.getYear();
    
    	try {
    	  date issuedDate(from_simple_string(issue_dymonyr.str()));
    	  date returnDate(from_simple_string(return_dymonyr.str()));
    	  days rental_days = returnDate - issuedDate; /* HERE ... Error : syntax error : missing ';' before identifier 'rental_days' */ 
                 & /* error C2065: 'rental_days' : undeclared identifier */
    	}
    	catch(...) {
    		std::cout << "Bad dates entered: " << issue_dymonyr.str() << return_dymonyr.str() << std::endl;
    	}

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Replies: 11
    Last Post: 03-29-2009, 12:27 PM
  3. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 05:00 PM
  4. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 06:17 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 09:12 PM

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