ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
With Christmas in about 2 months, I have decided to modify my game, The Crimson Swarm, for a more christmassy version. Every day, the player will be given a new starter item, however I need to find the day. I have got it to print the time like;
Code:
Tue Oct 25 16:34:10 2011
However I want it to get 'Oct' and '25' from that char. I have searched the web for something which works like:
Code:
strcpy(c,function_name(time_string,4,7));
Which would set c to be Oct, and so on.
Are there any functions like that?
Also, is there a way to convert a char * to a int, for easier comparison.
Also, can anyone give me any pointers on how to save things to files, I know how to output them, but how could I make a folder in the home/user directory named '.christmasswarm' which has the files in?
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Rep:
If you do something along these lines
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main (void)
{
time_t now;
struct tm *tm;
(void) time (&now);
tm = localtime (&now);
/* show a two-digit year */
(void) fprintf (stdout, "Today is %02d/%02d/%02d\n",
tm->tm_mon+1, tm->tm_mday, tm->tm_year % 100);
/* show a four-digit year */
(void) fprintf (stdout, "Today is %02d/%02d/%d\n",
tm->tm_mon+1, tm->tm_mday, tm->tm_year + 1900);
exit (EXIT_SUCCESS);
}
See how the tm structure elements are used? And, of course, you can use them in a if.
time.h has these structure elements:
Code:
struct tm
{
int tm_sec; /* Seconds. [0-60] (1 leap second) */
int tm_min; /* Minutes. [0-59] */
int tm_hour; /* Hours. [0-23] */
int tm_mday; /* Day. [1-31] */
int tm_mon; /* Month. [0-11] */
int tm_year; /* Year - 1900. */
int tm_wday; /* Day of week. [0-6] */
int tm_yday; /* Days in year.[0-365] */
int tm_isdst; /* DST. [-1/0/1]*/
};
So, if you follow the above example, you are going to get numeric values you can use for your purposes.
Hmm... I tried setting two variables to be the date, however it errors:
Code:
void twelvedays(void)
{
time_t now;
struct tm *tm;
time (&now);
tm = localtime (&now);
month = tm->tm_mon+1;
day = tm->tm_mday;
Is the code, and lines 161, 162, and 163:
Code:
tm = localtime (&now);
month = tm->tm_mon+1;
day = tm->tm_mday
error with:
Code:
main.c:161:12: warning: assignment makes pointer from integer without a cast [enabled by default]
main.c:162:12: error: dereferencing pointer to incomplete type
main.c:163:10: error: dereferencing pointer to incomplete type
Which probably means I need something like 'int' or 'char', yet the example you gave did not have that, yet I can see no other reasons for it to fail. Any ideas?
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Rep:
You might be going at it kind of sorta not quite what you want.
The tm structure elements are filled in by the call to localtime (&now) where now is the number of seconds from the epoch (00:00:00 01 Jan 1970). You only need to invoke tm = time (&now); once in your program (you're working with days, not seconds). So,
Code:
time_t now;
struct tm *tm;
/* get the system time in seconds from the epoch */
(void) time (&now);
/* convert that to local time */
tm = localtime (&now);
You'd only do that one time in a running program.
So, I think what you're trying to do is add a day (then another and another and so on) and use the tm structure?
OK, so here's a trick. A day is 86,400 seconds. Tomorrow is the value of now + 86,400. So
Code:
/* what's tomorrow? */
now += (time_t) 86400;
/* fill the tm structure with tomorrow */
localtime (&now);
and then use those values as you see fit (cute, eh?). The localtime() utility takes care of all the calendar changes (so, if somebody was running on 31 October and we add one day the tm structure will contain values for 01 November. Similarly, if you added five days (86400*5), you'd get a correct rollover at the end of a month, irrespective of what month it may be.
I didn't answer part of your question about writing to a file in the user's home directory.
All you'd need to do is a quick-and-dirty write of the value of now in a file, say a "dot" file so it's not readily visible to the user, something like .timestamp, something like this
Code:
time_t now, oldtime;
struct tm *tm;
FILE *out;
/* get the current system time */
(void) time (&now);
/* fill the tm structure elements */
tm = localtime (&now);
/* initialize old timestamp */
oldtime = (time_t) 0;
/* try to read an existing file */
if ((out = fopen (".timestamp", "r")) != (FILE *) NULL) {
/* it exists, read the value in it */
(void) fscanf (out, "%ld\n", &oldtime);
(void) fclose (out);
}
/*
now, simply open it for writing (which clears the
existing content) and put the current time stamp into it
*/
if ((out = fopen (".timestamp", "w")) == (FILE *) NULL) {
/* just in case there's a problem */
(void) fprintf (stderr,
"%s:\tcan't create %s\n",
argv [0], ".timestamp");
exit (EXIT_FAILURE);
}
/* write the current time to the file */
(void) fprintf (out, "%ld\n", now);
(void) fclsoe (out);
/*
* you can do whatever arithmetic you need to on the
* values of oldtime and now, just keep in mind that
* the difference between the two is what you need to
* work with and that oldtime will possibly be a few
* minutes ago, yesterday, a week ago or whatever and
* code appropriately to deal with that
*/
Thanks for the tip. Now I have got it working (Or I think so, it counts 25 days and I have only tested it today. I am going to have November as testing month so I have 5 days now to add stuff, and 5 days after before release).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.