📅生成ID和获取时间

生成各类ID和获取时间的内容封装成一个info模块。

当前进度

.---+- include -+- tools -+- color.h
    |                     +- hint.h
    |                     +- info.h <---
    | 
    +-   src   -+- tools -+- color.c
                |         +- hint.c
                |         +- info.c <---
                |
                +- data  -+- id.txt <---
                |
                +- main.c

生成ID

在我们的项目中有三个主体需要生成唯一的ID——用户、商品、订单。

我们约定这三种ID分别以U(User), G(Good), O(Order)开头加上五位数字,从类型上讲是一个字符串。

接口设计

// info.h

/**
 * @brief 生成一个给定类型ID
 * 
 * @param dest 储存ID的目标字符串
 * @param type ID类型,U-用户,G-商品,O-订单
 */
void genID(char* dest, char type);

实现

// info.c
#include "tools/hint.h"

#include <stdio.h>
#include <string.h>

static const char* filePath = "src/data/id.txt";

void genID(char* dest, char type) {
    int uid = 0, gid = 0, oid = 0; // user, good, order
    FILE* pf = fopen(filePath, "r");
    if (pf) {
        fscanf(pf, "%d%d%d", &uid, &gid, &oid);
        fclose(pf);
    }
    int id = 0;
    switch (type) {
        case 'U': id = uid++; break;
        case 'G': id = gid++; break;
        case 'O': id = oid++; break;
    }
    sprintf(dest, "%c%05d", type, id);
    pf = fopen(filePath, "w");
    fprintf(pf, "%d %d %d\n", uid, gid, oid);
    fclose(pf);
}

这里为了让程序结束后再次打开时也能够接着之前的ID生成唯一的新ID,用了一个文件src/data/id.txt来储存当前ID的最大值。

这里的ID仅仅和产生时间有关,哪怕之前有某个用户或者商品被删除了,其他的用户或者商品、订单的ID并不会因此而变化,新生成的ID也不会填补之前的ID。

因为其实商品的删除仅仅是商品的状态从销售中变成已下架,而不是将这个商品的信息彻底从系统中删除。

获取时间

在商品的信息中,我们要求提供商品的上架日期,格式比如2022-1-14,本质上是一个字符串,所以我们需要提供一个可以获取字符串格式的日期的函数。

接口设计

/**
 * @brief 获取当前日期
 * 
 * @param dest 储存日期的目标字符串
 */
void getDate(char* dest);

实现

// info.c
#include "tools/hint.h"

#include <time.h>
#include <string.h>

void getDate(char* dest) {
    time_t rawtime;
    time(&rawtime);
    struct tm* timeinfo = localtime(&rawtime);
    sprintf(dest, "%d-%d-%d", timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, timeinfo->tm_mday);
}

最后更新于

这有帮助吗?