base lib 1 ----Timestamp

本文详细介绍了如何将时间戳转换为结构化的tm类型,并提供了跨平台打印int64_t值的方法。

Timestamp 时间戳


        计算机从1970-01-01 开始时间计算 , 但目前的经过的时间秒 假设等于microSecondsSinceEpoch_ , 把microSecondsSinceEpoch_ 转为time_t (大整数),在有time_t

转为tm就可得到准确的时间格式

struct tm {

int tm_sec; /* 秒–取值区间为[0,59] */

int tm_min; /* 分 - 取值区间为[0,59] */

int tm_hour; /* 时 - 取值区间为[0,23] */

int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */

int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */

int tm_year; /* 年份,其值从1900开始 */

int tm_wday; /* 星期–取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */

int tm_yday; /* 从每年的1月1日开始的天数–取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */

int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/

long int tm_gmtoff; /*指定了日期变更线东面时区中UTC东部时区正秒数或UTC西部时区的负秒数*/

const char *tm_zone; /*当前时区的名字(与环境变量TZ有关)*/

};



跨平台PRId64 的使用

int64_t用来表示64位整数,在32位系统中是long long int,在64位系统中是long int,所以打印int64_t的格式化方法是:
printf(“%ld”, value);  // 64bit OS
printf("%lld", value); // 32bit OS

跨平台的做法:
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#undef __STDC_FORMAT_MACROS 
printf("%" PRId64 "\n", value);  

根据上次提问的修改如下 define Package/base-files/install $(CP) ./files/* $(1)/ $(Package/base-files/install-key) $(Package/base-files/nand-support) $(Package/base-files/legacy-sdcard-support) $(Package/base-files/emmc-support) if [ -d $(GENERIC_PLATFORM_DIR)/base-files/. ]; then \ $(CP) $(GENERIC_PLATFORM_DIR)/base-files/* $(1)/; \ fi if [ -d $(PLATFORM_DIR)/base-files/. ]; then \ $(CP) $(PLATFORM_DIR)/base-files/* $(1)/; \ fi $(if $(filter-out $(PLATFORM_DIR),$(PLATFORM_SUBDIR)), \ if [ -d $(PLATFORM_SUBDIR)/base-files/. ]; then \ $(CP) $(PLATFORM_SUBDIR)/base-files/* $(1)/; \ fi; \ ) $(VERSION_SED_SCRIPT) \ $(1)/etc/banner \ $(1)/etc/device_info \ $(1)/etc/openwrt_release \ $(1)/etc/openwrt_version \ $(1)/usr/lib/os-release $(SED) "s#%PATH%#$(TARGET_INIT_PATH)#g" \ $(1)/sbin/hotplug-call \ $(1)/etc/preinit \ $(1)/etc/pr mkdir -p \ $(1)/CONTROL \ $(1)/dev \ $(1)/etc/config \ $(1)/etc/crontabs \ $(1)/etc/rc.d \ $(1)/overlay \ $(1)/lib/firmware \ $(1)/mnt \ $(1)/proc \ $(1)/tmp \ $(1)/usr/lib \ $(1)/usr/bin \ $(1)/sys \ $(1)/www mkdir -p -m 750 \ $(1)/root #new add @starry $(INSTALL_DIR) $(1)/etc/init.d $(INSTALL_BIN) ./files/etc/init.d/set_system_time $(1)/etc/init.d/ echo "2023-11-15 12:00:00" > $(1)/etc/build_timestamp #add end $(LN) /proc/mounts $(1)/etc/mtab $(if $(LIB_SUFFIX),-$(LN) lib $(1)/lib$(LIB_SUFFIX)) $(if $(LIB_SUFFIX),-$(LN) lib $(1)/usr/lib$(LIB_SUFFIX)) 此时编译出现了报错 make[3]: Leaving directory '/home/starry/openwrt/package/base-files' time: package/base-files/compile#5.19#0.51#5.84 ERROR: package/base-files failed to build. make[2]: *** [package/Makefile:129: package/base-files/compile] Error 1 make[2]: Leaving directory '/home/starry/openwrt' make[1]: *** [package/Makefile:123: /home/starry/openwrt/staging_dir/target-mipsel_24kc_musl/stamp/.package_compile] Error 2 分析一下
最新发布
07-18
<think>根据提供的Makefile片段,我们在`define Package/base-files/install`中添加了以下内容: ```makefile #new add @starry $(INSTALL_DIR) $(1)/etc/init.d $(INSTALL_BIN) ./files/etc/init.d/set_system_time $(1)/etc/init.d/ echo "2023-11-15 12:00:00" > $(1)/etc/build_timestamp #add end ``` 但是编译报错。错误信息为: ``` make[3]: Leaving directory '/home/starry/openwrt/package/base-files' time: package/base-files/compile#5.19#0.51#5.84 ERROR: package/base-files failed to build. make[2]: *** [package/Makefile:129: package/base-files/compile] Error 1 ``` ### 错误分析 1. **Makefile语法错误**:在Makefile中,命令必须以Tab开头,但这里我们使用的是空格?注意,我们添加的内容中,`$(INSTALL_DIR)`等命令前面是8个空格,而Makefile要求命令部分必须以Tab开头(除非前面有特殊规则)。在OpenWrt的Makefile中,通常`define Package/base-files/install`内的命令使用缩进,但实际执行时,这些命令会被展开到规则中,因此每行命令前应该是Tab字符(或者由变量定义的命令)。 2. **命令执行环境**:我们添加的`echo`命令是shell命令,但在Makefile中,每一行命令都是在一个独立的shell中执行的(除非使用`\`连接多行)。另外,我们添加的命令可能因为缺少Tab缩进而被当作非命令部分。 3. **路径问题**:`./files/etc/init.d/set_system_time`这个路径可能不存在。因为我们在Makefile中是通过相对路径来引用,而当前工作目录可能是构建目录,不一定在`package/base-files`目录下。 ### 修正步骤 #### 1. 确保使用正确的缩进 在Makefile中,命令部分(即需要被shell执行的)必须以Tab开头。观察原有代码,都是使用Tab缩进的。因此我们添加的代码也应该使用Tab缩进,而不是空格。 #### 2. 确保文件存在 在`$(INSTALL_BIN)`之前,确保`./files/etc/init.d/set_system_time`文件确实存在。如果不存在,需要先创建。 #### 3. 使用正确的路径 在Makefile中,可以使用`$(PKG_BUILD_DIR)`来引用当前包的构建目录,但在这个上下文中,我们是在`package/base-files`目录下,所以相对路径`./files`是相对于`package/base-files`目录的。但为了安全,我们可以使用`$(TOPDIR)/package/base-files/files`来指定绝对路径。 然而,在`define Package/base-files/install`中,我们通常使用相对路径,因为构建系统已经将当前目录设置为`package/base-files`。 #### 4. 使用正确的命令 `echo`命令在shell中执行,但是直接这样写可能会因为路径问题导致失败。我们可以将输出重定向的命令写在一个shell命令中,并确保目录存在。 ### 修正后的Makefile片段 ```makefile define Package/base-files/install ... [原有代码] ... #new add @starry $(INSTALL_DIR) $(1)/etc/init.d $(INSTALL_BIN) ./files/etc/init.d/set_system_time $(1)/etc/init.d/ mkdir -p $(1)/etc # 确保目录存在(虽然前面有创建,但这里再确保一下) echo "2023-11-15 12:00:00" > $(1)/etc/build_timestamp #add end ... [后续原有代码] ... $(LN) /proc/mounts $(1)/etc/mtab ... [省略] ... endef ``` ### 常见错误排查 1. **检查文件是否存在**:在`package/base-files/files/etc/init.d/set_system_time`路径下,确保`set_system_time`文件已经存在,并且有可执行权限(因为我们要用`$(INSTALL_BIN)`安装)。 2. **检查Makefile的缩进**:确保添加的每一行命令都是以Tab开头(不是空格)。在编辑Makefile时,务必使用Tab字符。 3. **查看详细错误日志**:编译时,可以执行`make package/base-files/compile V=sc`来获取详细的错误信息(V=sc表示显示完整命令和错误)。 ### 如果问题仍然存在 如果按照上述修正后仍然报错,请检查编译日志中关于`package/base-files`的具体错误。可以在OpenWrt根目录下执行: ```bash make -j1 V=sc package/base-files/compile 2>&1 | tee build.log ``` 然后查看`build.log`中关于base-files的错误输出。 ### 可能的错误原因 - **文件不存在**:`./files/etc/init.d/set_system_time`文件不存在,导致`$(INSTALL_BIN)`失败。 - **权限问题**:当前用户对相关文件没有读写权限。 - **Makefile语法错误**:在添加的代码中,可能包含了不符合Makefile语法的字符。 ### 修正后的完整示例 假设我们按照以下结构创建文件: ``` package/base-files/ ├── Makefile └── files └── etc └── init.d └── set_system_time ``` 然后修改`package/base-files/Makefile`,在`define Package/base-files/install`的适当位置(比如在创建目录之后)添加: ```makefile # 设置系统时间脚本 $(INSTALL_DIR) $(1)/etc/init.d $(INSTALL_BIN) ./files/etc/init.d/set_system_time $(1)/etc/init.d/ echo "2023-11-15 12:00:00" > $(1)/etc/build_timestamp ``` 注意:确保`echo`命令前面是一个Tab(不是空格)。 ### 如果不想修改Makefile 另一种方法是避免直接修改`base-files`的Makefile,而是通过创建自定义包来实现。这样可以避免修改OpenWrt的标准包,便于维护。 §§相关问题§§: 1. 如何在OpenWrt中创建自定义软件包 2. Makefile中INSTALL_DIR和INSTALL_BIN命令的具体含义 3. OpenWrt编译过程中package的编译流程是怎样的 4. 如何调试OpenWrt的Makefile错误</think>根据您提供的 Makefile 片段和错误信息,我来分析编译失败的原因并提供解决方案。 ### 错误分析 1. **语法错误**: ```makefile echo "2023-11-15 12:00:00" > $(1)/etc/build_timestamp ``` 在 Makefile 中,`echo` 是 shell 命令,但 Makefile 要求所有 shell 命令前必须使用 `$(shell ...)` 包装或使用 `; \` 连接多行命令 2. **路径问题**: `./files/etc/init.d/set_system_time` 路径可能不正确,因为 Makefile 执行时的当前工作目录可能不是 `package/base-files/` 3. **权限问题**: `$(INSTALL_BIN)` 要求目标文件存在且可执行,但您可能尚未创建该文件 4. **依赖关系**: 添加的代码可能需要在特定目录创建之后执行 ### 修正后的 Makefile 片段 ```makefile define Package/base-files/install $(CP) ./files/* $(1)/ $(Package/base-files/install-key) $(Package/base-files/nand-support) $(Package/base-files/legacy-sdcard-support) $(Package/base-files/emmc-support) if [ -d $(GENERIC_PLATFORM_DIR)/base-files/. ]; then \ $(CP) $(GENERIC_PLATFORM_DIR)/base-files/* $(1)/; \ fi if [ -d $(PLATFORM_DIR)/base-files/. ]; then \ $(CP) $(PLATFORM_DIR)/base-files/* $(1)/; \ fi $(if $(filter-out $(PLATFORM_DIR),$(PLATFORM_SUBDIR)), \ if [ -d $(PLATFORM_SUBDIR)/base-files/. ]; then \ $(CP) $(PLATFORM_SUBDIR)/base-files/* $(1)/; \ fi; \ ) $(VERSION_SED_SCRIPT) \ $(1)/etc/banner \ $(1)/etc/device_info \ $(1)/etc/openwrt_release \ $(1)/etc/openwrt_version \ $(1)/usr/lib/os-release $(SED) "s#%PATH%#$(TARGET_INIT_PATH)#g" \ $(1)/sbin/hotplug-call \ $(1)/etc/preinit \ $(1)/etc/pr mkdir -p \ $(1)/CONTROL \ $(1)/dev \ $(1)/etc/config \ $(1)/etc/crontabs \ $(1)/etc/rc.d \ $(1)/overlay \ $(1)/lib/firmware \ $(1)/mnt \ $(1)/proc \ $(1)/tmp \ $(1)/usr/lib \ $(1)/usr/bin \ $(1)/sys \ $(1)/www mkdir -p -m 750 \ $(1)/root # ===== 修正后的代码开始 ===== # 确保目录存在 mkdir -p $(1)/etc/init.d # 使用绝对路径安装脚本 $(INSTALL_BIN) $(TOPDIR)/package/base-files/files/etc/init.d/set_system_time $(1)/etc/init.d/ # 使用正确的shell命令语法 (echo "2023-11-15 12:00:00") > $(1)/etc/build_timestamp # ===== 修正后的代码结束 ===== $(LN) /proc/mounts $(1)/etc/mtab $(if $(LIB_SUFFIX),-$(LN) lib $(1)/lib$(LIB_SUFFIX)) $(if $(LIB_SUFFIX),-$(LN) lib $(1)/usr/lib$(LIB_SUFFIX)) endef ``` ### 关键修正点解释 1. **使用绝对路径**: ```makefile $(INSTALL_BIN) $(TOPDIR)/package/base-files/files/etc/init.d/set_system_time $(1)/etc/init.d/ ``` `$(TOPDIR)` 是 OpenWrt 构建系统的根目录变量,确保能找到正确的文件 2. **正确的 shell 命令语法**: ```makefile (echo "2023-11-15 12:00:00") > $(1)/etc/build_timestamp ``` 使用括号包裹命令确保在子 shell 中执行,避免语法错误 3. **确保目录存在**: ```makefile mkdir -p $(1)/etc/init.d ``` 在安装文件前确保目标目录存在 ### 额外需要检查的事项 1. **确保脚本文件存在**: 确认以下文件存在且具有可执行权限: ``` package/base-files/files/etc/init.d/set_system_time ``` 如果没有,请创建: ```bash mkdir -p package/base-files/files/etc/init.d nano package/base-files/files/etc/init.d/set_system_time chmod +x package/base-files/files/etc/init.d/set_system_time ``` 2. **脚本内容示例**: ```bash #!/bin/sh /etc/rc.common START=10 boot() { [ -f /etc/build_timestamp ] && { build_time=$(cat /etc/build_timestamp) date -s "$build_time" # 写入硬件时钟(如果支持) [ -x /sbin/hwclock ] && hwclock -w } } ``` ### 调试建议 如果仍然失败,请尝试: 1. 获取详细错误信息: ```bash make package/base-files/compile V=s ``` 2. 检查编译日志: ```bash tail -n 50 logs/package.base-files.log ``` 3. 单独编译 base-files: ```bash make package/base-files/{clean,compile} V=s ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值