473,769 Members | 2,102 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Time Stuff

In Time.h there is a structure defined for time settings. I'm
building an embedded system that has a Real Time Clock but it's not PC
compatible.

My question is:

I don't some elements of the structure such as day of the week or day
of the year, but I do know year,month,date ,hour,min,sec. Does the
language support filling in the missing elements. And is there a
consistency check for that time structure.

Thanks
George
Nov 14 '05 #1
14 2498


George wrote:
In Time.h there is a structure defined for time settings. I'm
building an embedded system that has a Real Time Clock but it's not PC
compatible.

My question is:

I don't some elements of the structure such as day of the week or day
of the year, but I do know year,month,date ,hour,min,sec. Does the
language support filling in the missing elements. And is there a
consistency check for that time structure.


I guess the struct you mention is struct tm. The 6 values you do know are
enough assuming that the date is in range of available dates on the
implementation.
Put the values in the struct and use function mktime to calculate a
calendar
time. The other fields will be normalized.

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

int main(void)
{
time_t date;
struct tm t = {0};

/* July 4, 2004 1:00:15 pm */
t.tm_mon = 6; /* July */
t.tm_mday = 4;
t.tm_year = 104; /* Year 2004 */
t.tm_hour = 13; /* 1 pm */
t.tm_min = 0;
t.tm_sec = 15;

date = mktime(&t);
if(date != (time_t)-1)
printf("The date is %s"
"The %d day of the year\n",
ctime(&date), t.tm_yday);
else puts("Time is not available");
return 0;
}

Nov 14 '05 #2
Lewis Bowers <lb*******@cox. net> wrote in message news:<40******* ********@cox.ne t>...
George wrote:
In Time.h there is a structure defined for time settings. I'm
building an embedded system that has a Real Time Clock but it's not PC
compatible.

My question is:

I don't some elements of the structure such as day of the week or day
of the year, but I do know year,month,date ,hour,min,sec. Does the
language support filling in the missing elements. And is there a
consistency check for that time structure.


I guess the struct you mention is struct tm. The 6 values you do know are
enough assuming that the date is in range of available dates on the
implementation.
Put the values in the struct and use function mktime to calculate a
calendar
time. The other fields will be normalized.

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

int main(void)
{
time_t date;
struct tm t = {0};

/* July 4, 2004 1:00:15 pm */
t.tm_mon = 6; /* July */
t.tm_mday = 4;
t.tm_year = 104; /* Year 2004 */
t.tm_hour = 13; /* 1 pm */
t.tm_min = 0;
t.tm_sec = 15;

date = mktime(&t);
if(date != (time_t)-1)
printf("The date is %s"
"The %d day of the year\n",
ctime(&date), t.tm_yday);
else puts("Time is not available");
return 0;
}


Thank you so very much. I was purposely not very clear about some of
the details incase C# had a solution while C did not.

I posted this question in other news groups and all I got was babble
from holy that thou types listing the problems with my question.

Lewis you rock!!!
Nov 14 '05 #3
Greetings,

George wrote:

I posted this question in other news groups and all I got was babble
from holy that thou types listing the problems with my question.


In the other groups you failed to mention a language, architecture, or
implementation making it *very* difficult to answer your question. The
answer would be different in either BASIC or LISP.

--
Kyle A. York
Sr. Subordinate Grunt

Nov 14 '05 #4
On 6 Jul 2004 07:02:00 -0700, ge***********@a tt.net (George) wrote:
I posted this question in other news groups and all I got was babble
from holy that thou types listing the problems with my question.


In fairness, I will point out that one of those newsgroups was
comp.arch.embed ded, and the response was that George had not given
enough detail to suggest a solution. In the world of c.a.e., asking
for further information was quite reasonable (as one responder pointed
out, we don't even know if it's a hosted implementation. )

George's response to this request was " If you can't help shut up!!"

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 14 '05 #5
Lewis Bowers <lb*******@cox. net> wrote in message news:<40******* ********@cox.ne t>...
George wrote:
In Time.h there is a structure defined for time settings. I'm
building an embedded system that has a Real Time Clock but it's not PC
compatible.

My question is:

I don't some elements of the structure such as day of the week or day
of the year, but I do know year,month,date ,hour,min,sec. Does the
language support filling in the missing elements. And is there a
consistency check for that time structure.


I guess the struct you mention is struct tm. The 6 values you do know are
enough assuming that the date is in range of available dates on the
implementation.
Put the values in the struct and use function mktime to calculate a
calendar
time. The other fields will be normalized.

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

int main(void)
{
time_t date;
struct tm t = {0};

/* July 4, 2004 1:00:15 pm */
t.tm_mon = 6; /* July */
t.tm_mday = 4;
t.tm_year = 104; /* Year 2004 */
t.tm_hour = 13; /* 1 pm */
t.tm_min = 0;
t.tm_sec = 15;

date = mktime(&t);
if(date != (time_t)-1)
printf("The date is %s"
"The %d day of the year\n",
ctime(&date), t.tm_yday);
else puts("Time is not available");
return 0;
}

Yes. If I enter the month, day, year, hour, minute and second into
the tm structure and call mktime() The day of the year, day of the
week and daylight savings time fields are automatically filled in.

The next step is that I would like to know somethings about the next
date. I can take the time_t date from the above example and add
24*60*60 (sec/hr) seconds to it to get a time 24 hours from the first
time.

Next I want to convert the time_t date to a structure. If I call
localtime() I get returned pointer to a tm sturcture and in that
structure is the correct data.

But where is that structure located? If I call localtime() again will
I get a different pointer. If these routines only create one tm
structure ever then I can live with that. If however the run malloc()
to create a structure but never free(), Then I'll run out of ram.

Now I know this might not be strictly a C question but I know experts
such as yourselves have some insight as to what's happening.

Thanks In Advance
George
Nov 14 '05 #6
ge***********@a tt.net (George) wrote:
Yes. If I enter the month, day, year, hour, minute and second into
the tm structure and call mktime() The day of the year, day of the
week and daylight savings time fields are automatically filled in.

The next step is that I would like to know somethings about the next
date. I can take the time_t date from the above example and add
24*60*60 (sec/hr) seconds to it to get a time 24 hours from the first
time.
No, you can't. You don't know that time_t is measured in seconds, or
even that it is an integer. All you know is that time_t is "an
arithmetic type capable of representing times". It is allowed to be a
double indicating a fractional Julian Day Number.

What you _can_ do is take your struct tm, add 1 to its tm_day member,
and then call mktime() again. It will return a time_t, but as a side
effect it will "normalise" your struct tm, bringing its members into the
expected range. You _could_ even try to add 24*60*60 to the tm_sec
member, but that will fail on systems with 16-bit ints... so better
stick to tm_day.
Next I want to convert the time_t date to a structure. If I call
localtime() I get returned pointer to a tm sturcture and in that
structure is the correct data.

But where is that structure located?
Who knows? That's up to the implementation.
If I call localtime() again will I get a different pointer.
This is not guaranteed. According to n869.txt, 7.23.3:

# Execution of any of the functions that return a pointer to one of
# these object types may overwrite the information in any object of the
# same type pointed to by the value returned from any previous call to
# any of them.

Note: "may", not "must".
If these routines only create one tm structure ever then I can live with that.
I expect that they usually do (for starters, it's the easier option),
but...
If however the run malloc() to create a structure but never free(),
Then I'll run out of ram.


....even if they don't, I don't think they'd be used a lot if they leaked
memory left, right, and centre. mktime() is no different in this regard
from, say, strerror().

Richard
Nov 14 '05 #7


George wrote:
Lewis Bowers <lb*******@cox. net> wrote in message news:<40******* ********@cox.ne t>...
George wrote:
In Time.h there is a structure defined for time settings. I'm
building an embedded system that has a Real Time Clock but it's not PC
compatible.

My question is:

I don't some elements of the structure such as day of the week or day
of the year, but I do know year,month,date ,hour,min,sec. Does the
language support filling in the missing elements. And is there a
consistency check for that time structure.


I guess the struct you mention is struct tm. The 6 values you do know are
enough assuming that the date is in range of available dates on the
implementation.
Put the values in the struct and use function mktime to calculate a
calendar
time. The other fields will be normalized.

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

int main(void)
{
time_t date;
struct tm t = {0};

/* July 4, 2004 1:00:15 pm */
t.tm_mon = 6; /* July */
t.tm_mday = 4;
t.tm_year = 104; /* Year 2004 */
t.tm_hour = 13; /* 1 pm */
t.tm_min = 0;
t.tm_sec = 15;

date = mktime(&t);
if(date != (time_t)-1)
printf("The date is %s"
"The %d day of the year\n",
ctime(&date), t.tm_yday);
else puts("Time is not available");
return 0;
}


Yes. If I enter the month, day, year, hour, minute and second into
the tm structure and call mktime() The day of the year, day of the
week and daylight savings time fields are automatically filled in.

The next step is that I would like to know somethings about the next
date. I can take the time_t date from the above example and add
24*60*60 (sec/hr) seconds to it to get a time 24 hours from the first
time.

Next I want to convert the time_t date to a structure. If I call
localtime() I get returned pointer to a tm sturcture and in that
structure is the correct data.

But where is that structure located? If I call localtime() again will
I get a different pointer. If these routines only create one tm
structure ever then I can live with that. If however the run malloc()
to create a structure but never free(), Then I'll run out of ram.

Now I know this might not be strictly a C question but I know experts
such as yourselves have some insight as to what's happening.


I am not sure of your requirements but if it will allow you to declare a struct
and all you need is two calendar dates, ex. current and next day, then all you
need is to declare one tm struct and two time_t values. Dynamic allocations
may not be neccessary.

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

int main(void)
{
time_t date,nextdate;
struct tm t;

/* July 4, 2004 1:00:15 pm */
t.tm_mon = 6; /* July */
t.tm_mday = 4;
t.tm_year = 104; /* Year 2004 */
t.tm_hour = 13; /* 1 pm */
t.tm_min = 0;
t.tm_sec = 15;

date = mktime(&t);
if(date != (time_t)-1)
{
printf("The date is %s"
"The %d day of the year\n",
ctime(&date), t.tm_yday);
t.tm_hour += 24;
nextdate = mktime(&t);
if(nextdate != (time_t)-1)
{
printf("\nThe next day is %s"
"The %d day of the year\n",
ctime(&nextdate ), t.tm_yday);
printf("\nThere are %.0f secs difference"
" between the two dates\n",
difftime(nextda te,date));
}
else puts("No time available for the next date");
}
else puts("No time available for date");
return 0;
}

Nov 14 '05 #8
Alan Balmer wrote:
ge***********@a tt.net (George) wrote:
I posted this question in other news groups and all I got was
babble from holy that thou types listing the problems with my
question.


In fairness, I will point out that one of those newsgroups was
comp.arch.embed ded, and the response was that George had not
given enough detail to suggest a solution. In the world of
c.a.e., asking for further information was quite reasonable (as
one responder pointed out, we don't even know if it's a hosted
implementation. )

George's response to this request was " If you can't help shut
up!!"


Obviously a student of Dale Carnegies "How to Win Friends and
Influence People". He will go far, into the plonk pit.

--
Replies should be to the newsgroup
Chuck Falconer
Nov 14 '05 #9
>> I guess the struct you mention is struct tm. The 6 values you do know are
enough assuming that the date is in range of available dates on the
implementation.

I believe you are really supposed to initialize tm_isdst also before
feeding the struct tm to mktime(). 0 is normally a pretty good value, unless
you're dealing with something like logs around the time of a daylight-savings-time
transition, where you can have two occurrences of the time without the timezone
specifier being considered.

Yes. If I enter the month, day, year, hour, minute and second into
the tm structure and call mktime() The day of the year, day of the
week and daylight savings time fields are automatically filled in.

The next step is that I would like to know somethings about the next
date. I can take the time_t date from the above example and add
24*60*60 (sec/hr) seconds to it to get a time 24 hours from the first
time.
No, you are not guaranteed to be able to do any type of math on a
time_t. A time_t is not guaranteed to be of the form "number of
<time unit> since <epoch>". As a horrible example, consider the
following format:

HHmmSSMMDDYYYYY

HH = hour (0-23)
mm = minute (0-59)
SS = second (0-61)[*]
MM = month (1-12)
DD = day (1-31)
YYYYY = year (Y100K problem!)
[*] wierdness here due to leap seconds

fill in the values with decimal digits, now treat it as a decimal
integer. Try fiddling with this, and you realize that subtracting
these gives complete mush (you can't even do a comparison to figure
out which time is earlier, as 15:01 is "earlier" than 15:02 regardless
of what century it's in.)

Try subtracting 1 second (or 1000000000) from 000000010102004 and
see what you get. You SHOULD get 235959123102003 if you really
subtracted 1 second (I'm assuming this particular time did not have
any leap seconds without looking it up), but you won't.

There is no good way to portably add 24 hours to a timestamp, when
you want to talk about the PASSAGE OF TIME, not WHAT THE CLOCK
READS. These differ when you cross a daylight-savings-time transition
or a leap second. 15:00 on consecutive days may be 23 or 25 hours
apart. Adding 1 to tm_day or adding 24 to tm_hour works most of
the time. Then again, even a stopped clock reads correctly twice
a day.

MS-DOS file stamps are not too unlike this (using bit fields to
represent year, month, day, hour, minute, and seconds divided by
two within a 32-bit integer), although at least they put the
high-order time elements in the high-order part of the integer, so
you actually can compare time stamps using > or < .
Next I want to convert the time_t date to a structure. If I call
localtime() I get returned pointer to a tm sturcture and in that
structure is the correct data. But where is that structure located?
The structure returned by localtime() is static data that may be
overwritten on a subsequent call. This is specified by ANSI C and
should be in any decent documentation of localtime(). (If you write
functions like this, you'd better also carefully document this.)
When in doubt MAKE A COPY OF IT (the structure, not the pointer).
You do NOT have to allocate your copy with malloc() when an auto
variable will do.
If I call localtime() again will
I get a different pointer.
This is not specified. localtime() could play tricks with rotating
several buffers. Or, more commonly, it could return the same pointer
every time.
If these routines only create one tm
structure ever then I can live with that. If however the run malloc()
to create a structure but never free(), Then I'll run out of ram.


You must not free() the struct tm returned by localtime().

Gordon L. Burditt
Nov 14 '05 #10

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

Similar topics

28
2221
by: luke | last post by:
http://www.angrycoder.com/article.aspx?cid=1&y=2003&m=7&d=17 I was actually dragged onto the Microsoft technology treadmill in 1992 when they bought out Fox in Perrysburg, Ohio. Yep, I'm one of those XBase guys who was suddenly jerked out of the '80s (dBase, Clipper, FoxBase/FoxPro) into the Client/Server gold rush. Kidnapped from the nether worlds of Unix and Novell I kicked and screamed and finally learned to like my captors.
6
2518
by: Kenneth | last post by:
Hello, I'm having some serious problems debugging a script that I'm trying to make work. I'm working on a form where a user can type in a time (in the format of HH:MM), and another script automatically calculate how much time is inbetween. That part of it is working fine, but what ISN'T working fine is the script that validates whether or not the user has entered the time in proper syntax, and to make sure that the Time In does not...
77
4611
by: Charles Law | last post by:
Hi guys I have a time critical process, running on a worker thread. By "time critical", I mean that certain parts of the process must be completed in a specific time frame. The time when the process starts is not especially important, but it must be complete within a small number of seconds. The operations I am performing do not take a long time (hundreds of milliseconds), but as each part of the process is complete, my worker thread...
17
7685
by: Eric Lindsay | last post by:
Is learning to write CSS a better use of time than finding and using a package that produces complete web pages? I've moved to a new platform (Macintosh), taking with me about 400 personal web pages, some dating back so far I probably wrote them in vi. About 4 years ago (thanks in part to hints found in this group) I converted about 80 pages to CSS, and was fairly happy with the result, plain though they are. Since then I've forgotten...
26
55756
by: Pravesh | last post by:
Hi: is there a way to get current system time in milliseconds... which functions and headers?? thanks pravesh
1
2612
by: danielhardman | last post by:
I am implementing a component that helps a developer with som localization tasks at design-time, and provides some related feature at run-time. My problem is that in order to do the design-time stuff, I need reference to EnvDTE or EnvDTE80 (I'm using Whidbey beta 2). However, can't ship these .dlls and I don't need them at run-time anyway. If simply add a reference to envdte.dll in my project, then th design-time stuff works, but I also...
16
2603
by: TB | last post by:
Hi all: If you think that the following comments are absolute amateurish, then please bear with me, or simply skip this thread. A couple of months back I made the decision to initiate a gradual upgrade of my web programming skills from Classic ASP / VBS to ASP.NET / VB.NET. While the study of the language differences and all the new features in .NET has so far not been a traumatic experience, I am a bit shell-schocked after
5
3530
by: Ray Tomes | last post by:
Hi Folks I am an old codger who has much experience with computers in the distant past before all this object oriented stuff. Also I have loads of software in such languages as FORTRAN and BASIC, QBASIC etc that is very useful except that it really doesn't like to run on modern operating systems and has hopeless graphics resolution and lack of ease of use in some ways.
1
3448
by: ewanfisher | last post by:
Hello, I am trying to find a way to output how long a script took to run. Obviously the print would go at the end of the script, so it would be the time up till that point. I also run a PostgreSQL query inside the script and would like to separately show how long the query took to run. Is this even possible?
0
1202
by: Hendrik van Rooyen | last post by:
Lawrence D'Oliveiro wrote: The above is an endless loop, doing something every five seconds, and something else in the in between times. I don't think you are dense - its a good point, leading to the following analysis: The do_the_in_between_stuff call either takes less than, or more than, or exactly five
0
9579
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9422
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
10208
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
10038
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
9857
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...
0
8867
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7404
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...
1
3952
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
2
3558
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.