嵌入式Linux时区设置

说明

  • 在多个平台进行时区设置,发现有多种方式,有的组合设置能生效但是有些组合不能,有些不解,特记录一下。

时区设置

早期平台(gm8126平台)

  • 之前gm8126板子上时区设置只需要将相应的时区文件覆盖掉/etc/timezone即可。
  • /etc/timezone是时区文件,里面内容是复杂不可读的时区信息。
  • 平台使用glibc。

MTK8516平台

  • MTK8516平台使用systemd系统为初始化进程(init)。
  • 设置时区需要使用以下命令:
sudo timedatectl set-timezone 'Asia/Shanghai' 
* 在/usr/share/zoneinfo/目录下需要有相应的时区文件,不然会报错
  • /etc/localtime是具体时区文件 /usr/share/zoneinfo/xxx 的软链接,设置时区成功后/etc/localtime 会自动更新。
  • /etc/timezone文件依然存在,但是内容不再是时区信息,而是简单的时区字符串,例如:‘Asia/Shanghai’。
  • 平台使用glibc。

疑惑

  • 不清楚/etc/timezone的具体作用,是否只是为了兼容老版本?

ssc323平台

  • ssc323平台使用Busybox生成的老式sysinit为初始化进程,平台使用uclibc。
  • 设置时区时,替换/etc/timezone或者创建软链接/etc/localtime链接到具体的时区文件都不生效;只能通过以下设置方法:
1. 设置 /etc/TZ 文件,在该文件中指明时区
2. 设置 TZ 环境变量

总结

  • 时区设置方法不同的根源是C标准库不同,使用glibc和uclibc的时区设置方式是不同的,可以根据代码推导和官方文档确认。
  1. uclibc
官网FAQ说明:https://www.uclibc.org/FAQ.html#timezones
Why does localtime() return times in UTC even when I have my timezone set?
The uClibc time functions get timezone information from the TZ environment variable, as described in the Single Unix Specification Version 
3. See http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html for details on valid settings of TZ. For some additional examples,   
read https://www.uClibc.org/lists/uclibc/2002-August/004010.html in the uClibc mailing list archive.  
You can store the value of TZ in the file '/etc/TZ' and uClibc will then automagically use the specified setting.
  • uclibc不支持/etc/timezone和localtime的设置方式,只支持TZ文件和环境变量,
  • 示例如下:
时区设置为 中国UTC+8
/etc/TZ 文件中写入 "CST-8"
或者 
/etc/TZ 文件中写入 "UTC-8"  //为什么是-8,个人也不清楚,测试设置为UTC-8时区才是中国UTC+8
  1. glibc
官网FAQ说明:https://sourceware.org/glibc/wiki/FAQ#How_can_I_set_the_timezone_correctly.3F
You first have to install yourself the timezone database, it is hosted at http://www.iana.org/time-zones.

Then, simply run the tzselect shell script, answer the question and use the name printed in the end by making a symlink /etc/localtime pointing to /usr/share/zoneinfo/NAME (NAME is the returned value from tzselect). That's all. You never again have to worry. Instead of the system wide setting of /etc/localtime}}, you can also set the {{{TZ environment variable.

The GNU C Library supports the extented POSIX method for setting the TZ variable, this is documented in the manual.
  • glibc既支持创建软链接/etc/localtime指向具体的时区文件,也支持设置TZ环境变量的方式。
  • 猜测:创建软链接/etc/localtime方式是替换/etc/timezone的升级方式,目的是减少一份时区文件的空间占用。
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
000 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 223 227 230 231 232 233 234 235 236 237 238 239 240 241 243 247 250 251 253 257 260 261 263 267 270 271 272 273 274 275 276 277 278 279 280 281 283 287 290 291 293 297 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 423 427 430 431 432 433 434 435 436 437 438 439 440 441 443 447 450 451 453 457 460 461 463 467 470 471 472 473 474 475 476 477 478 479 480 481 483 487 490 491 493 497 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 523 527 530 531 532 533 534 535 536 537 538 539 540 541 543 547 550 551 553 557 560 561 563 567 570 571 572 573 574 575 576 577 578 579 580 581 583 587 590 591 593 597 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 623 627 630 631 632 633 634 635 636 637 638 639 640 641 643 647 650 651 653 657 660 661 663 667 670 671 672 673 674 675 676 677 678 679 680 681 683 687 690 691 693 697 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 823 827 830 831 832 833 834 835 836 837 838 839 840 841 843 847 850 851 853 857 860 861 863 867 870 871 872 873 874 875 876 877 878 879 880 881 883 887 890 891 893 897 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 923 927 930 931 932 933 934 935 936 937 938 939 940 941 943 947 950 951 953 957 960 961 963 967 970 971 972 973 974 975 976 977 978 979 980 981 983 987 990 991 993 997

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值