【date】Linux date命令修改时间的问题

article/2025/10/6 23:06:41

Linux date命令修改时间的问题

    • 问题
    • 路径
    • 找原因
    • 解决方法

问题

Android10;高通平台
使用下面date命令修改时间日期,时分秒生效,年月日不生效
=> date -D YYYY-MM-DD hh:mm:ss

路径

\android\external\toybox\toys\posix\date.c
\android\external\toybox\lib\args.c

找原因

首先在命令行键入
=> adb shell date -D 2020-10-01 22:59:00

去代码看一下参数传的有没有什么问题
\android\external\toybox\main.c

// argc:参数的数量   argv: 参数
int main(int argc, char *argv[])
{if (!*argv) return 127;if (CFG_TOYBOX) {// Call the multiplexer, adjusting this argv[] to be its' argv[1].// (It will adjust it back before calling toy_exec().)toys.argv = argv-1;toybox_main();}...
}

argc = 4,
argv[0] = date, argv[1] = -D, argv[2] = 2020-10-01, argv[3] = 22:59:00
在这里插入图片描述
很明显这里并没有什么问题,我们接着往下看调用toybox_main()

// Multiplexer command, first argument is command to run, rest are args to that.
// If first argument starts with - output list of command install paths.
void toybox_main(void)
{static char *toy_paths[]={"usr/","bin/","sbin/",0};int i, len = 0;if (toys.argv[1]) {toy_exec(toys.argv+1);if (0<readlink(toys.argv[1], libbuf, sizeof(libbuf)))toy_exec_which(toy_find(basename(libbuf)), toys.argv);}...
}

  参数传给toys.argv,我们看一下toys.argv里面是怎么存放数据的:
  其中toys.argv[0]为空,toys.argv[1] = date, toys.argv[2] = -D, toys.argv[3] = 2020-10-08, toys.argv[4] = 22:59:00
  还是没有问题,我们接着往下看

// Lookup internal toybox command to run via argv[0]
void toy_exec(char *argv[])
{toy_exec_which(toy_find(basename(*argv)), argv);
}// Run an internal toybox command.
void toy_exec_which(struct toy_list *which, char *argv[])
{...// Run commandtoy_init(which, argv);if (toys.which) toys.which->toy_main();xexit();
}
void toy_init(struct toy_list *which, char *argv[])
{void *oldwhich = toys.which;...// Continue to portion of init needed by standalone commandstoy_singleinit(which, argv);
}static void toy_singleinit(struct toy_list *which, char *argv[])
{toys.which = which;toys.argv = argv;...if (NEED_OPTIONS && which->options) get_optflags();else {toys.optargs = argv+1;for (toys.optc = 0; toys.optargs[toys.optc]; toys.optc++);}toys.old_umask = umask(0);if (!(which->flags & TOYFLAG_UMASK)) umask(toys.old_umask);toys.signalfd--;toys.toycount = ARRAY_LEN(toy_list);
}

这里走的分支是get_optflags()
  get_optflags()是对参数的解析,通过log可以发现get_optflags的for循环只循环了gof.argc=1(-D),gof.argc=3(22:59:00),漏掉了gof.argc=2就是年月日
先看gof.argc=1的循环:
  gof.arg是字符型指针,指向toys.argv[1] = -D 的”-”,++后就指向了”D”, 所以传给gotflag()的参数是”D”

// Fill out toys.optflags, toys.optargs, and this[] from toys.argv
void get_optflags(void)
{struct getoptflagstate gof;struct opts *catch;unsigned long long saveflags;char *letters[]={"s",""};// Iterate through command line arguments, skipping argv[0]for (gof.argc=1; toys.argv[gof.argc]; gof.argc++) {gof.arg = toys.argv[gof.argc];catch = NULL;// Parse this argumentif (gof.stopearly>1) goto notflag;gof.nodash_now = 0;// Various things with dashesif (*gof.arg == '-') {// Handle -if (!gof.arg[1]) goto notflag;gof.arg++;if (*gof.arg=='-') {struct longopts *lo;gof.arg++;// Handle --if (!*gof.arg) {gof.stopearly += 2;continue;}// do we match a known --longopt?for (lo = gof.longopts; lo; lo = lo->next) {if (!strncmp(gof.arg, lo->str, lo->len)) {if (!gof.arg[lo->len]) gof.arg = 0;else if (gof.arg[lo->len] == '=' && lo->opt->type)gof.arg += lo->len;else continue;// It's a match.catch = lo->opt;break;}}// Should we handle this --longopt as a non-option argument?if (!lo && gof.noerror) {gof.arg -= 2;goto notflag;}// Long option parsed, handle option.gotflag(&gof, catch);continue;}// Handle things that don't start with a dash.} else {if (gof.nodash && (gof.nodash>1 || gof.argc == 1)) gof.nodash_now = 1;else goto notflag;}// At this point, we have the args part of -args.  Loop through// each entry (could be -abc meaning -a -b -c)saveflags = toys.optflags;while (*gof.arg) {// Identify next option char.for (catch = gof.opts; catch; catch = catch->next)if (*gof.arg == catch->c)if (!((catch->flags&4) && gof.arg[1])) break;// Handle option char (advancing past what was used)if (gotflag(&gof, catch) ) {toys.optflags = saveflags;gof.arg = toys.argv[gof.argc];goto notflag;}}continue;// Not a flag, save value in toys.optargs[]
notflag:if (gof.stopearly) gof.stopearly++;toys.optargs[toys.optc++] = toys.argv[gof.argc];}...
}

  看下为什么会把年月日跳过,gotflag()中选项(-option)解析, gotflag(&gof, catch);
传入的gof->arg 指向 ”D”, gof->arg有参数++指向” ”

// Use getoptflagstate to parse one command line option from argv
static int gotflag(struct getoptflagstate *gof, struct opts *opt)
{int type;// Set flagstoys.optflags |= opt->dex[1];gof->excludes |= opt->dex[2];if (opt->flags&2) gof->stopearly=2;...// Does this option take an argument?if (!gof->arg) {if (opt->flags & 8) return 0;gof->arg = "";} else gof->arg++; // 指向" "type = opt->type; // type = ":"...return 0;
}

char*arg = ” ”
gof->nodash_now = 0, !arg[0] = 1, opt->flags = 0
进行判断,++argc,argc由1变为2,也是导致年月日被跳过的原因
这时arg指向“2020-10-08”, opt->arg指向“2020-10-08”

解决方法

  由于args.c是解析命令行参数的共用文件,没把握最好不要修改,很容易影响到其他命令的解析。我们可以让被跳过的参数2为任意字符,如("+"可为任意字符)
=> date -D + YYYY-MM-DDhh:mm:ss
另外也可以使用
=> date MMDDhhmmYYYY.ss set
来设置时间


http://chatgpt.dhexx.cn/article/aW2CSuQg.shtml

相关文章

i2ctools工具移植到android(使用NDK方式 在某android平台测试)

前提条件 主板i2c已在设备树配置status和引脚复用正常&#xff0c;即设备的i2c总线达到正常使用条件I2C device interface假设内核已配置进去 编译工具链NDK环境搭建 下载NDK 下载地址点我解压 ~/workspace/ndk$ ls android-ndk-r22b android-ndk-r22b-linux-x86_64.zip …

高通平台 Android9 adb shell “hwclock -w“ 报错

hwclock -w 报错 文章目录 hwclock -w 报错问题现象分析1. hwclock命令分析2. /dev/rtc0驱动节点分析 修改设备树后hwclock -w报错没有了&#xff0c;但是系统会重启&#xff0c;原因未知 问题现象 sdm660_64:/ # hwclock -w hwclock: ioctl 4024700a: Invalid argument分析 …

Android top命令、ps命令、busybox命令

top命令 usage: top [-Hbq] [-k FIELD,] [-o FIELD,] [-s SORT] [-n NUMBER] [-m LINES] [-d SECONDS] [-p PID,] [-u USER,]Show process activity in real time.-H Show threads -k Fallback sort FIELDS (default -S,-%CPU,-ETIME,-PID) -o Show FIELDS (def PID,USER,PR,N…

OpenHarmony啃论文俱乐部—盘点开源鸿蒙引用的三方开源软件[1]

目录这里写自定义目录标题 OpenHarmony third_party三方库&#xff1a;学术研究和参与开源的结合third_party_openh264third_party_ninjathird_party_gnthird_party_markupsafethird_party_toyboxthird_party_gstreamerthird_party_ffmpegthird_party_mtdevthird_party_flutter…

Android缺少awk工具的几种解决方法

在日常测试中&#xff0c;我们会用到各种各样的Android平台&#xff0c;用于测试存储设备的性能。其中&#xff0c;我们依赖到Android平台自身的工具&#xff0c;通过编写shell脚本来实现测试存储设备的性能。   而awk工具(shell命令)在shell脚本中会经常用到&#xff0c;一般…

toybox 和 busybox 的作用

来自知乎&#xff1a;程序员秘书 ##前言## 我们在做android开发时&#xff0c;经常会有在板子系统里要修改文件内容对比验证问题&#xff0c;或者要操作特殊的shell命令操作看些信息&#xff0c;等等一些需求。但是往往会因为刷到板子的系统里默认没有/不支持相关的命令&…

欢乐听:一个简洁的瀑布流模式的音乐分享站

欢乐听 一个简洁的瀑布流模式的音乐分享站。

分享5个高质无损音乐网站,歌曲很丰富,爱听歌的小伙伴有耳福了

生活中很多人都离不开音乐&#xff0c;散步的时候听音乐&#xff0c;等待的时候听着音乐&#xff0c;心情不好的时候听音乐&#xff0c;不管走到哪&#xff0c;有音乐的陪伴一点也不寂寞&#xff0c;不同音乐的旋律给我们带来不同的心情&#xff0c;今天小编就给爱听音乐的小伙…

【音乐】收藏的300多首抖音神曲,MP3音乐分享,近一年的抖音歌曲

今天给大家分享N多&#xff08;300&#xff09;首抖音神曲。抖音大家都知道&#xff0c;各种火&#xff0c;各种原因&#xff0c;其中的BGM(背景音乐)更是起到了至关重要&#xff08;画龙点睛&#xff09;的作用&#xff0c;不知道是哪个大神搞的&#xff0c;去年我搞视频的时候…

搭建一个点歌QQ机器人,另外还能看美女

目录 前言具体实现1、爆照2、生日书3、获取歌词和分享音乐 完整项目下载地址&#xff08;配置了python环境&#xff09;完整项目下载地址&#xff08;电脑没有python环境&#xff09; 前言 完整项目&#xff0c;包括框架、代码和详细使用说明可以去社区下载&#xff08;下载完…

基于java的音乐网站的设计与实现

欢迎添加微信互相交流学习哦&#xff01; 项目源码&#xff1a;https://gitee.com/oklongmm/biye 基于java的音乐网站的设计与实现 摘 要 随着互联网和宽带上网的普及&#xff0c;网络传输以其特有的快速、高效、便捷的传输方式越来越被人们接受。在当今社会的影响下&…

android wifi传输音乐,让你通过WiFi分享手机上的歌曲,音乐共享软件MyStream十一发布Android版...

MyStream原来是 iOS上的音乐共享应用&#xff0c;十一期间&#xff0c;它将跨出iOS平台&#xff0c;首次推出Android版音乐共享服务。 MyStream和主流的Pandora、Spotify、Turntable.fm、Songza这些音乐分享服务并不一样。它将手机上的本地音乐通过WiFi或蓝牙和周围的设备进行音…

竞品分析:网易云音乐和QQ音乐,音乐类app的战场

文章从产品的角度分别分析两款产品的行业市场、功能、业务模式以及运营策略&#xff0c;进一步了解两款产品的差异与不同。 一、产品概况 1. 产品概述及版本 网易云音乐是一款专注于发现与分享的音乐产品&#xff0c;依托专业音乐人、DJ、好友推荐及社交功能&#xff0c;为用…

网易云音乐竞品分析

概述 1.原因 个人平时使用网易云较多&#xff0c;正好想学学竞品分析怎么写。因此&#xff0c;想通过梳理市场最新报告和数据表现&#xff0c;了解在线音乐行业的现状和市场情况&#xff1b;分析网易云音乐目前的市场地位、功能设计、UI设计等方面&#xff0c;与相关竞品进行对…

音乐平台程序源码分享

简介&#xff1a; 这是一个音乐分享平台源码&#xff0c;用户可以自行上传音乐分享&#xff0c;源码自适应手机&#xff0c;使用很方便。 安装说明&#xff1a; 演示环境&#xff1a;宝塔PHP5.5 mysql5.6 Apache 2.4.46&#xff0c;把程序上传到根目录&#xff0c;然后修改数…

分享两个音乐播放地址

下歌吧音乐下载平台 http://music.y444.cn/ 搜索时候如果没有想要的&#xff0c;可以切换线路搜索一下 MYFREEMP3 MYFREEMP3 一个音乐下载以及播放网站 下载后的音乐名称需要直接更改一下&#xff0c;可以下载歌曲的歌词等 http://tools.liumingye.cn/music/?pagesearch…

基于web的音乐分享网站的设计与实现

欢迎添加微信互相交流学习哦&#xff01; 项目源码&#xff1a;https://gitee.com/oklongmm/biye2 音乐分享网站的设计与实现 摘 要 随着社会的发展时代的前进&#xff0c;IT行业的发展也是日新月异&#xff0c;对人类的生产和生活方式产生了很大影响。网络传播以其特有的快…

把自己录制的mp3音乐分享到朋友圈

参考&#xff1a;https://www.zhihu.com/question/345647212 录了首歌&#xff0c;想上传至微信朋友圈&#xff0c;发现还没那么简单。 mp3音乐文件上传到网易云音乐后&#xff0c;无法使用分享功能&#xff0c;所以无法分享到朋友圈。 可通过以下步骤解决。 一、将mp3文件发…

#pragma comment

原因:突然看到#pragma comment,不知其意思.感觉自己是个渣渣.所以写了一篇博客. #pragma comment 简单来说就是链接了一个文件&#xff0c;它可以是compiler&#xff0c;exestr&#xff0c;lib&#xff0c;linker文件. 例如&#xff1a; #include<WinSock2.h> #pragma…

预处理 #pragma 命令详解

关注、星标公众号&#xff0c;不错过精彩内容 素材来源&#xff1a;网络 编辑整理&#xff1a;strongerHuang 预处理指令 #pragma 相信程序员都知道&#xff0c;但在所有的预处理指令中&#xff0c;#pragma 指令可能是最复杂的了&#xff0c;它的作用是设定编译器的状态或者是指…