473,789 Members | 2,289 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what's wrong with my program ?

hi, below is my program for doing exercise with library time functions,
something is not right with it, and for the time being i couldn't figure
out what's wrong and where, thanx for your help.

#include <time.h>
#include <stdio.h>

double TimeDifference( struct tm *t1,struct tm *t2)
{
return difftime(mktime (t1),mktime(t2) );
}
char *ConstructTime( struct tm *time,int month,int day,int year)
{
time->tm_mon=month-1;
time->tm_mday=day;
time->tm_year=year-1900;
mktime(time);
return asctime(time);
}

int main(int argc,char **argv)
{
struct tm now,t1,t2;
char *tmstr;
double diff;
int month,day,year;

scanf("%d%d%d", &month,&day,&ye ar);
tmstr=Construct Time(&now,month ,day,year);
printf("%s",tms tr);
ConstructTime(& t1,6,7,2004);
ConstructTime(& t2,6,9,2004);
diff=TimeDiffer ence(&t1,&t2);
printf("%f\n",d iff);
return 0;
}
Nov 14 '05 #1
6 1925
sugaray wrote:
hi, below is my program for doing exercise with library time functions,
something is not right with it, and for the time being i couldn't figure
out what's wrong and where, thanx for your help.

#include <time.h>
#include <stdio.h>

double TimeDifference( struct tm *t1,struct tm *t2)
{
return difftime(mktime (t1),mktime(t2) );
}
char *ConstructTime( struct tm *time,int month,int day,int year)
{
time->tm_mon=month-1;
time->tm_mday=day;
time->tm_year=year-1900;
At this point, all of the members of time except the 3 you just
set contain garbage. You need to set them to some rational
values before going on.
mktime(time);
return asctime(time);
}

--
Thomas M. Sommers -- tm*@nj.net -- AB2SB

Nov 14 '05 #2

"sugaray" <ru****@sohu.co m> wrote in message news:ad******** *************** ***@posting.goo gle.com...
hi, below is my program for doing exercise with library time functions,
something is not right with it, and for the time being i couldn't figure
out what's wrong and where, thanx for your help.

#include <time.h>
#include <stdio.h>

double TimeDifference( struct tm *t1,struct tm *t2)
{
return difftime(mktime (t1),mktime(t2) );
}
char *ConstructTime( struct tm *time,int month,int day,int year)
{
time->tm_mon=month-1;
time->tm_mday=day;
time->tm_year=year-1900;
mktime(time);
return asctime(time);
}

int main(int argc,char **argv)
{
struct tm now,t1,t2;
char *tmstr;
double diff;
int month,day,year;

/* adding these removed the misbehaviour */
memset ( &now, 0, sizeof now );
memset ( &t1, 0, sizeof t1 );
memset ( &t2, 0, sizeof t2 );

scanf("%d%d%d", &month,&day,&ye ar);
tmstr=Construct Time(&now,month ,day,year);
printf("%s",tms tr);
ConstructTime(& t1,6,7,2004);
ConstructTime(& t2,6,9,2004);
diff=TimeDiffer ence(&t1,&t2);
printf("%f\n",d iff);
return 0;
}

Nov 14 '05 #3
"Vijay Kumar R Zanvar" <vi*****@global edgesoft.com> wrote:
"sugaray" <ru****@sohu.co m> wrote in message news:ad******** *************** ***@posting.goo gle.com...
char *ConstructTime( struct tm *time,int month,int day,int year)
{
time->tm_mon=month-1;
time->tm_mday=day;
time->tm_year=year-1900;
mktime(time);
return asctime(time);
}

int main(int argc,char **argv)
{
struct tm now,t1,t2;
char *tmstr;
double diff;
int month,day,year;


/* adding these removed the misbehaviour */
memset ( &now, 0, sizeof now );
memset ( &t1, 0, sizeof t1 );
memset ( &t2, 0, sizeof t2 );


Not a good solution. First of all, the problem really is in
ConstructTime() . With a name like that, one can expect it to construct a
complete time struct, so it should do so. Second, if you use this
solution, you need to use it for every single time you create, not just
once in the function itself; forget one, and the problem comes back.
Third, this sets the structs to all-bits-zero, not to sensible initial
members; in particular, it does not set tm_isdst to something negative,
which is what I would want it to be.
Oh, and another small bug: it is never a good idea to use the same name
for one of your structs as for a closely related Library function...
A better solution is this:

char *ConstructTime( struct tm *the_time, int month,int day,int year)
{
the_time->tm_mon=month-1;
the_time->tm_mday=day;
the_time->tm_year=year-1900;
the_time->tm_sec=the_tim e->tm_min=the_tim e->tm_hour=0;
the_time->tm_wday=the_ti me->tm_yday=0;
the_time->tm_isdst=-1;
mktime(the_time );
return asctime(the_tim e);
}

Note also that asctime() returns a pointer to a static array, so if you
want to use two of those strings, copy the first _string_, not the first
pointer, before you get the second one. The second call will over-write
the string written by the first.

Richard
Nov 14 '05 #4


sugaray wrote:
hi, below is my program for doing exercise with library time functions,
something is not right with it, and for the time being i couldn't figure
out what's wrong and where, thanx for your help.

#include <time.h>
#include <stdio.h>

double TimeDifference( struct tm *t1,struct tm *t2)
{
return difftime(mktime (t1),mktime(t2) );
}

Function mktime will return (time_t)-1 should it
fail to convert a calendar time. Therefore the
function arguments must be of the form that will
ensure a conversion. Or, you must modify the function.

char *ConstructTime( struct tm *time,int month,int day,int year)
{
time->tm_mon=month-1;
time->tm_mday=day;
time->tm_year=year-1900;
mktime(time);
return asctime(time);
}


I see two problems in the use of function ConstructTime.

First you only set three members of the struct. All the
other members are indeterminant.

Second, if function mktime is unable to determine a
calendar time, components are not set to represent the
specified calendar time, nor their values forced
to the normal ranges. Using this illegitmate struct
as argument to function asctime may be unpleasant.

A possible fix:

#include <time.h>
#include <stdio.h>

double TimeDifference( struct tm *t1,struct tm *t2)
{
return difftime(mktime (t1),mktime(t2) );
}
char *ConstructTime( struct tm *time,int month,int day,int year)
{
struct tm tmp = {0};

*time = tmp;
time->tm_mon=month-1;
time->tm_mday=day;
time->tm_year=year-1900;
time->tm_isdst = -1;
return mktime(time)==( time_t)-1?NULL:asctime( time);
}

int main(int argc,char **argv)
{
struct tm now,t1,t2;
char *tmstr1, *tmstr2;
double diff;
int month,day,year;

printf("Enter the date (MM DD YYYY?): ");
fflush(stdout);
scanf("%d%d%d", &month,&day,&ye ar);
tmstr1=Construc tTime(&now,mont h,day,year);
if(tmstr1) printf("%s\n",t mstr1);
tmstr1 = ConstructTime(& t1,6,7,2004);
if(tmstr1) printf("Time 1: %s",tmstr1);
tmstr2 = ConstructTime(& t2,6,8,2004);
if(tmstr2) printf("Time 2: %s", tmstr2 );
if(tmstr1 && tmstr2)
{
diff=TimeDiffer ence(&t2,&t1);
printf("Differe nce is %.f secconds\n",dif f);
}
return 0;
}

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapi dsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #5
sugaray wrote:
hi, below is my program for doing exercise with library time functions,
something is not right with it, and for the time being i couldn't figure
out what's wrong and where, thanx for your help.

#include <time.h>
#include <stdio.h>

double TimeDifference( struct tm *t1,struct tm *t2)
{
return difftime(mktime (t1),mktime(t2) );
}
char *ConstructTime( struct tm *time,int month,int day,int year)
{
time->tm_mon=month-1;
time->tm_mday=day;
time->tm_year=year-1900;
mktime(time);
return asctime(time);
}


You need to initialize the elements of *time (which should be spelt some
way other that "time"). Here's one approach a modern C compiler would like:

char *ConstructTime( struct tm *t, int month, int day, int year)
{
*t = (struct tm) {
.tm_mon = month - 1,.tm_mday = day,.tm_year =
year - 1900,.tm_isdst = -1};
mktime(t);
return asctime(t);
}

An approach that doesn't need the anonymous struct is to declare a local
struct tm t_tmp, with the proper base values, and copy it: *t = t_tmp;
Nov 14 '05 #6
Martin Ambuhl <ma*****@earthl ink.net> wrote in message news:<2i******* *****@uni-berlin.de>...
char *ConstructTime( struct tm *t, int month, int day, int year)
{
*t = (struct tm) {
.tm_mon = month - 1,.tm_mday = day,.tm_year =>
year - 1900,.tm_isdst = -1};
mktime(t);
return asctime(t);
} An approach that doesn't need the anonymous struct is to declare a local
struct tm t_tmp, with the proper base values, and copy it: *t = t_tmp;

so, why use a temp variable rather set the values directly to *t ?
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

137
7195
by: Philippe C. Martin | last post by:
I apologize in advance for launching this post but I might get enlightment somehow (PS: I am _very_ agnostic ;-). - 1) I do not consider my intelligence/education above average - 2) I am very pragmatic - 3) I usually move forward when I get the gut feeling I am correct - 4) Most likely because of 1), I usually do not manage to fully explain 3) when it comes true. - 5) I have developed for many years (>18) in many different environments,...
125
14858
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
5
2838
by: titan0111 | last post by:
#include<iostream> #include<iomanip> #include<cstring> #include<fstream> using namespace std; class snowfall { private: int ft;
56
4344
by: Cherrish Vaidiyan | last post by:
Frinds, Hope everyone is doing fine.i feel pointers to be the most toughest part in C. i have just completed learning pointers & arrays related portions. I need to attend technical interview on C. wat type of questions should be expected? Which part of C language do the staff give more concern? The interviewers have just mentioned that .. i will have interview on C. Also can anyone can help me with sites where i can go thru sample
11
2044
by: Alfonso Morra | last post by:
Hi, I am at the end of my tether now - after spending several days trying to figure how to do this. I have finally written a simple "proof of concept" program to test serializing a structure containing pointers into a "flattened" bit stream. Here is my code (it dosen't work). I would be grateful for any feedback that helps fix this. My intention
46
4263
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do believe MSFT should do to improve C#, however. I know that in the "Whidbey" release of VS.NET currently
669
26234
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
20
2821
by: Daniel.C | last post by:
Hello. I just copied this code from my book with no modification : #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0;
12
2116
by: broli | last post by:
#include<stdio.h> #include<stdlib.h> struct point { double x, y, z;
4
1967
by: hirsh.dan | last post by:
i have a functions that writes information to a file. inside that function i have a line in which i call another function. if this line is executed, nothing is written to the file, but if i remark that line, it is written. i wanna understand what's wrong and fix it, but can't find what is wrong. also this line is mandatory in my code. the code attached is in the link, minimized to the needed info, a bit different then the way it's...
0
9661
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9506
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10403
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10193
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9978
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7524
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6755
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5414
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4087
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.