So i made another Code in C in which i am making a program to stop working when the Licence has ended.
The Program Runs but lets say the date its suppose to end on June 20, 2012 so it triggers the Stop of use of the program.
This is just and example of what i am trying to say or get to:

if(SYSTEM DATE == MY DATE LIKE JUNE 20, 2012) printf("Sorry, Program needs to be renewed\N")
    if(SYSTEM DATE > MY DATE LIKE JUNE 20, 2012)  printf(" PROGRAMMED EXPIRED\n");/
    if(SYSTEM DATE < MY DATE LIKE JUNE 20, 2012) printf("You still have time left for use");

for example, Where it says Is there a way i can put something or a code in which is read the system time?
like for example, lets say Variable "Date" is the string of code:

time_t t = time(0);
    struct tm * now = localtime( &t );
    printf("%d-%d-%d\n",           
           (now->tm_year + 1900), 
           (now->tm_mon + 1), 
           now->tm_mday)

so if i put "date" in if (date == my date like june 20, 2012) it really means the entire code above in which its being read and compared to June 20.
in which it would terminate the program.

i really suck in timing and time.h functions, a little help??

Recommended Answers

All 2 Replies

You can use mktime() after populating a tm object with the suitable date. Then the comparison can be done with difftime():

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

time_t get_today(struct tm** date, int is_gmt)
{
    time_t now = time(NULL);

    if (date)
    {
        *date = is_gmt ? gmtime(&now) : localtime(&now);
    }

    return now;
}

time_t get_date(struct tm** date, int is_gmt, int year, int month, int day)
{
    struct tm* working_date;
    time_t ret = get_today(&working_date, is_gmt);

    working_date->tm_year = year - 1900;
    working_date->tm_mon = month - 1;
    working_date->tm_mday = day;

    if (date)
    {
        *date = working_date;
    }

    return ret = mktime(working_date);
}

int main(void)
{
    int year = 2012, month = 6, day = 20;
    time_t today_time = get_today(NULL, 1);
    time_t target_time = get_date(NULL, 1, year, month, day);

    if (target_time == -1)
    {
        fprintf(stderr, "Invalid date: '%d-%d-%d'\n", year, month, day);
        return EXIT_FAILURE;
    }

    if (difftime(target_time, today_time) < 0)
    {
        puts("Your trial period has expired");
    }
    else
    {
        puts("You are using a trial version of this software");
    }

    return EXIT_SUCCESS;
}

deceptikon, you saved My back here. it works wonders man, i didnt know i missed a lot of stuff onto my code in which it read current date. thnx soo much, You are Awesome!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.