Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Site Related Forums
 The Yak Corral
 function problem

Author  Topic 

ace333
Starting Member

11 Posts

Posted - 2005-09-20 : 13:48:17
Anyone know what language this function is in and what its doing....
I think its trying to reset the time inputted to zero .....

any help .... and can any1 spot any of the errors in it....

time_t normalise(time_t input_time)
{

bool finished;

// This produces a formatted time string like:
// Thu_Nov_24_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;
}



elwoos
Master Smack Fu Yak Hacker

2052 Posts

Posted - 2005-09-20 : 14:03:44
Looks like a variant of C to me!!

As for what it's doing - never did quite grasp the intricacies of C

steve

Facts are meaningless. You could use facts to prove anything that's even remotely true!
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2005-09-20 : 14:07:37
It looks like a completely crap routine to roll a time value back to midnight on Sunday-last!

Completely pointless.

ace333: tell the interviewer that you think it would be done more efficiently using a Tally table. If he doesn't know what one of those is suggest he reads some books by Joe Celko.

Kristen
Go to Top of Page

Arnold Fribble
Yak-finder General

1961 Posts

Posted - 2005-09-20 : 15:44:03
There are 7 days in a week.
19700101 was a Thursday.
Shouldn't it be something like this?

time_t normalise(time_t input_time) {
return input_time - ((input_time+345600)%604800+604800)%604800;
}

And before you ask, the 2 modulus operators are there because I can't remember whether C is allowed to return negative results for a%b where a is negative.
Also, this assumes a POSIX-compliant lack of leap-seconds!
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2005-09-20 : 16:19:08
quote:
Originally posted by Arnold Fribble

There are 7 days in a week.
19700101 was a Thursday.
Shouldn't it be something like this?

time_t normalise(time_t input_time) {
return input_time - ((input_time+345600)%604800+604800)%604800;
}

And before you ask, the 2 modulus operators are there because I can't remember whether C is allowed to return negative results for a%b where a is negative.
Also, this assumes a POSIX-compliant lack of leap-seconds!



... thinking for a couple of seconds, thoughtfully removing pipe from mouth ...
"Elementary my dear Watson"


Very nice!
The mark of a genius is simplification, not complication.
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-09-20 : 16:30:23
That is some of the worst code I've ever seen. There quite a few objects that we need definitions before -- this depends on what time_t is (a struct? a class?) and what the function format_time() returns.
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-09-20 : 18:01:12
if i remember my c++ days correctly time_t is a struct.

Go with the flow & have fun! Else fight the flow
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2005-09-20 : 19:52:15
That's just cruel.
quote:
Originally posted by Kristen

...suggest he reads some books by Joe Celko.,,





CODO ERGO SUM
Go to Top of Page

jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2005-09-20 : 23:57:49
c-language

--------------------
keeping it simple...
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-09-21 : 02:18:02
C or C Like language
This reminds me to think those college days

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -