Help needed on time date

View: New views
3 Messages — Rating Filter:   Alert me  

Help needed on time date

by tonyaim83 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi
 I'm using Date time library for the first time so no idea how best make use of it.
 My primary target is to check if the first timestamp and second timestamp entered by the user are in a span of one hour.
Format of  input string is as follows YYYYDDMM-HH:MM:SS(20071015-17:47:10) Now if the second string is also in the an hour span it should return true otherwise false.
I m thinking of doing this like
std::string to_iso_string(date d1)
ptime p1(d1, time_duration("17:21:23"));
std::string to_iso_string(date d2)
ptime p2(d2, time_duration("17:22:23"));

ptime p3=p2-p1;
if (p3>1)
{
  return false;
}
I could also find a function which has a separator T
std::string to_iso_extended_string(ptime);
How can i make use of this function as in my case the separtor of date- time is "-" Or a better way to perform the same task.
 

Re: Help needed on time date

by Larry-40 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Perhaps something like where p1 and p2 are as you defined them

time_duration onehour(1,0,0);

if (p2 > (p1 + onehour)) {
      return false;
}

Larry
----- Original Message -----
From: "tonyaim83" <abhishek.v@...>
Newsgroups: gmane.comp.lib.boost.user
To: <boost-users@...>
Sent: Monday, December 24, 2007 6:29 AM
Subject: Help needed on time date


>
> Hi
> I'm using Date time library for the first time so no idea how best make
> use
> of it.
> My primary target is to check if the first timestamp and second timestamp
> entered by the user are in a span of one hour.
> Format of  input string is as follows YYYYDDMM-HH:MM:SS(20071015-17:47:10)
> Now if the second string is also in the an hour span it should return true
> otherwise false.
> I m thinking of doing this like
> std::string to_iso_string(date d1)
> ptime p1(d1, time_duration("17:21:23"));
> std::string to_iso_string(date d2)
> ptime p2(d2, time_duration("17:22:23"));
>
> ptime p3=p2-p1;
> if (p3>1)
> {
>  return false;
> }
> I could also find a function which has a separator T
> std::string to_iso_extended_string(ptime);
> How can i make use of this function as in my case the separtor of date-
> time
> is "-" Or a better way to perform the same task.
>
> --
> View this message in context:
> http://www.nabble.com/Help-needed-on-time-date-tp14486644p14486644.html
> Sent from the Boost - Users mailing list archive at Nabble.com.


_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: Help needed on time date

by Jeff Garland :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

tonyaim83 wrote:

> Hi
>  I'm using Date time library for the first time so no idea how best make use
> of it.
>  My primary target is to check if the first timestamp and second timestamp
> entered by the user are in a span of one hour.
> Format of  input string is as follows YYYYDDMM-HH:MM:SS(20071015-17:47:10)
> Now if the second string is also in the an hour span it should return true
> otherwise false.
> I m thinking of doing this like
> std::string to_iso_string(date d1)
> ptime p1(d1, time_duration("17:21:23"));
> std::string to_iso_string(date d2)
> ptime p2(d2, time_duration("17:22:23"));
>
> ptime p3=p2-p1;
> if (p3>1)
> {
>   return false;
> }
> I could also find a function which has a separator T
> std::string to_iso_extended_string(ptime);
> How can i make use of this function as in my case the separtor of date- time
> is "-" Or a better way to perform the same task.
>  

I suggest you use the facet i/o.  The program below prints "less than one
hour".  The time_input_facet defines the parsing format:

     time_input_facet* timefacet = new time_input_facet("%Y%m%d-%H:%M:%S");

Rest of the program should be pretty obvious.

*****************************
#include "boost/date_time.hpp"
#include <iostream>

using namespace boost::posix_time;

ptime
parse_time(const std::string& s)
{

   ptime t; //not-a-date-time
   try {
     std::stringstream ss(s.c_str());
     time_input_facet* timefacet = new time_input_facet("%Y%m%d-%H:%M:%S");
     ss.imbue(std::locale(std::locale::classic(), timefacet));
     ss.exceptions(std::ios_base::failbit); // turn on exceptions
     ss >> t;
   }
   catch(std::exception& e) {
     std::cout << "Incorrect date format entered" << std::endl;
     std::cout << e.what() << std::endl;
   }
   return t;

}


int main()
{

   hours one_hour(1);
   ptime t1 = parse_time("20071015-17:47:10");
   ptime t2 = parse_time("20071015-17:47:45");
   if (!(t1.is_not_a_date_time() || t2.is_not_a_date_time())) {
     if (t2-t1 < one_hour) {
       std::cout << "less than one hour" << std::endl;
     }
   }

}

_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users