C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 7th, 2017, 11:29 AM
Nathan105 Nathan105 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2017
Posts: 1 Nathan105 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 43 m
Reputation Power: 0
Post Improving Code

Code:
unsigned long normalise(unsigned long input_time)
{
 
      bool finished;
 
      // This produces a formatted time string like:
      // Fri_Nov_25_18:22:48_1986
      string str_time = format_time( input_time );
 
      while( str_time.substr(1,3) != "Sun")
      {
            input_time -= 24*60*60;
            str_time = format_time( input_time );
      }
 
      while( str_time.substr(11,2) != "00" )
      {
            input_time -= 60*60;
            str_time = format_time( input_time );
      }
 
      while( str_time.substr(14,2) != "00")
      {
            str_time = format_time( input_time );
            input_time -= 60;
      }
 
      while( str_time.substr(17,2) != "00")
      {
            input_time -= 1;
            str_time = format_time( input_time );
      }
 
      return input_time;
}


In the following code I found out two bugs:

1) The first while loop should have substr values (0,3) and not (1,3)
2) The third while loop, output time is formatted before new time is calculated.

My Question is:
Given that input_time actually represents an integer value in seconds since the epoch - and has the value 0 at precisely “Thu Jan 1 00:00:00 1970” write a better implementation of this function.

How do I go about and approach the question? What I was thinking of doing is use only one loop and try and change the index value in every iteration. Please help me with a better implementation of this function.

Much Obliged

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Help


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.

© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap