简单设置软件有效期的方法

来源:互联网 发布:淘宝怎样关注达人 编辑:程序博客网 时间:2024/05/20 23:39

第一步  弄清Linuxgmtimelocaltime的区别

前段时间用到,写了段小测试代码,个人觉得足够清楚的表明了二者的区别了,还是不明白的话,就看看APUE里面的章节吧。

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

int main(int argc, char **argv)
{
    time_t tmpcal_ptr = {0};
    struct tm *tmp_ptr = NULL;

    tmpcal_ptr = time(NULL);//获取系统时间,单位为秒
    printf("tmpcal_ptr=%d\n", tmpcal_ptr);   

    tmp_ptr = gmtime(&tmpcal_ptr);//转换成tm类型的结构体;
    printf("after gmtime, the time is:\n%d:%d:%d", tmp_ptr->tm_hour, tmp_ptr->tm_min, tmp_ptr->tm_sec);

    tmp_ptr = localtime(&tmpcal_ptr); //转换成tm类型的结构体;
    printf("after localtime, the time is:\n%d:%d:%d", tmp_ptr->tm_hour, tmp_ptr->tm_min, tmp_ptr->tm_sec);

return 0;
}

localtime是将时区考虑在内了,转出的当前时区的时间。但是注意,有些嵌入式设备上被裁减过的系统,时区没有被设置好,导致二者转出来的时间都是0时区的。

 

第二步 tm这个结构体的定义?

/*

 * Similar to the struct tm in userspace <time.h>, but it needs to be here so

 * that the kernel source is self contained.

 */

struct tm {

/*

 * the number of seconds after the minute, normally in the range

 * 0 to 59, but can be up to 60 to allow for leap seconds

 */

int tm_sec;

/* the number of minutes after the hour, in the range 0 to 59*/

int tm_min;

/* the number of hours past midnight, in the range 0 to 23 */

int tm_hour;

/* the day of the month, in the range 1 to 31 */

int tm_mday;

/* the number of months since January, in the range 0 to 11 */ //==>我们的月数需要加1

int tm_mon;

/* the number of years since 1900 *//我们的年数是从1900年开始计算的,因此,2017年的话,需要设置为tm_year + 117

long tm_year;

/* the number of days since Sunday, in the range 0 to 6 */

int tm_wday;

/* the number of days since January 1, in the range 0 to 365 */

int tm_yday;

};

从上面这些我们可以看出,年月日我们都可以获取到,这个相当给力,给我们后面判断时间带来了很大方便.

 

第三步 两个方案

方案一 如何控制软件的有效日期方案1

简单粗暴法,该方法直接在软件里写死到哪个日期后就不可用了,当然,如果你将日期强制改为这个日期之前,也是可以用的,不过软件发布后,用户不会将系统时间设置为过去的任何日子,因此,如果B2B的话可以这样做,因为下一步B2C的时候,C用的肯定是正确的系统时间(这里只考虑移动终端哈).欢迎各位大神前来拍砖讨论.

        time_t tmpcal_ptr = {0};

        struct tm *tmp_ptr = NULL;

        tmpcal_ptr = time(NULL);

        ALOGD("The time is %d", tmpcal_ptr);

        tmp_ptr = localtime(&tmpcal_ptr);

        ALOGD("after localtime, the time is:%d:%d:%d, %d:%d:%d", tmp_ptr->tm_hour, tmp_ptr->tm_min, tmp_ptr->tm_sec, tmp_ptr->tm_mday, tmp_ptr->tm_mon + 1, tmp_ptr->tm_year + 1900);

        int year_now = tmp_ptr->tm_year + 1900;

        int month_now = tmp_ptr->tm_mon + 1;

        int day_now = tmp_ptr->tm_mday;

        ALOGD("The time is %d - %d - %d", year_now, month_now, day_now);

        ALOGD("Checking license ......");

        if(year_now > 2017 || month_now >= 8 && day_now >= 26){

            ALOGE("The license EXPIRED ");

            return;//如果逾期,我们就不再进行初始化,这样功能就无法正常使用了.

        }

        ALOGD("The TS color enhance license's state is OK.");

方案二 如何控制软件的有效日期方案2

将第一次加载此功能的时间设置为初始时间,然后将该时间写入到/sdcard/Android/下面(希望有大神能指点将这个时间备份文件放在哪里更优雅的建议),这样就完成了初始时间的保存.

每次在打开软件前,均去/sdcard/Android下面检查时间文件是否存在:如果存在,则读取首次加载此功能的时间,否则将当前时间当做初次加载该功能的时间保存到/sdcard/Android.

然后就可以将本次开启该功能的时间减去之前记录的时间,即可判断该功能是否应该正常开启.

 

其它:如果计算函数耗时,我们倒是可以使用gettimeofday()函数,它能精确到微秒级.

 

References:

1.这个blog主要是讲述gmtimelocaltime的区别

http://www.linuxidc.com/Linux/2013-04/82670.htm

2.这个blog写的大而全,没重点

http://www.cnblogs.com/sun-frederick/p/4772535.html

3.这个blog主要讲了如何将1970年以来的秒数转换为年月日

我认为应尽量使用库函数来完成,这样能够减少不稳定性,不知道这样思考是否正确,欢迎大神拍砖

http://www.cnblogs.com/lebronjames/archive/2010/09/06/1819359.html

原创粉丝点击