😎 κ³΅λΆ€ν•˜λŠ” μ§•μ§•μ•ŒνŒŒμΉ΄λŠ” μ²˜μŒμ΄μ§€?

[디렉터리와 μ‹œκ°„ 처리] ν˜„μž¬μ˜ μ‹œκ°„μ„ 좜λ ₯ν•˜κΈ° λ³Έλ¬Έ

πŸ‘©‍πŸ’» IoT (Embedded)/Raspberry Pi

[디렉터리와 μ‹œκ°„ 처리] ν˜„μž¬μ˜ μ‹œκ°„μ„ 좜λ ₯ν•˜κΈ°

μ§•μ§•μ•ŒνŒŒμΉ΄ 2024. 1. 8. 00:33
728x90
λ°˜μ‘ν˜•

<μ„œμ˜μ§„ λ‹˜μ˜ 사물인터넷을 μœ„ν•œ λ¦¬λˆ…μŠ€ ν”„λ‘œκ·Έλž˜λ° with λΌμ¦ˆλ² λ¦¬νŒŒμ΄ μ„œμ μ„ μ°Έκ³ ν•΄μ„œ μž‘μ„±ν•˜μ˜€μŠ΅λ‹ˆλ‹€ :-)>

 

⭐ μ‹œκ°„ ν•¨μˆ˜

time() : μ‹œκ°„ μΆœλ ₯을 μœ„ν•΄ λ¨Όμ € ν˜„μž¬ μ‹œκ°„을 κ°€μ Έμ˜¨λ‹€ (μ΄ν•΄ν•˜κΈ° νž˜λ“  ν° μˆ«μžλ‘œ λ‚˜μ˜¨λ‹€)
ctime(), asctime() : μ‚¬λžŒμ΄ μ΄ν•΄ν•  μˆ˜ μžˆλŠ” ν˜•νƒœλ‘œ λ³€ν™˜ν•œλ‹€

μ‹œκ°„κ³Ό κ΄€λ ¨λœ κ΅¬μ‘°μ²΄λŠ” tm κ΅¬μ‘°μ²΄
gmtime() : UTC μ‹œκ°„을 λ°˜ν™˜
localtime() : ν˜„μž¬ λ‘œμ»¬μ— λ§žλŠ” μ‹œκ°„ μ‚¬μš©
strftime() : μ›ν•˜λŠ” μ„œμ‹μ— λ§žλ„둝 μΆœλ ₯

ν˜„μž¬ μΆœλ ₯λ˜λŠ” μ‹œκ°„λŒ€λ₯Ό λ°”κΎΈκ³  μ‹Άλ‹€λ©΄ ν™˜κ²½ λ³€μˆ˜ μ‘°μ •
putenv() : ν˜„μž¬ ν™˜κ²½μ„ μ„€μ •
"TZ" : μ‹œκ°„λŒ€μ™€ κ΄€λ ¨λœ ν™˜κ²½ λ³€μˆ˜
tzset() : λ³€κ²½λœ ν™˜κ²½ λ³€μˆ˜
setlocale() : λ‘œμΌ€μΌ λ³€κ²½

#include <stdio.h>
#include <time.h>
#include <sys/time.h>       // gettimeofday() ν•¨μˆ˜
#include <stdlib.h>

int main(int argc, char **argv) {
    int i, j;
    time_t rawtime;
    struct tm *tm;
    char buf[BUFSIZ];
    struct timeval mytime;

    // ν˜„μž¬μ˜ μ‹œκ°„ κ΅¬ν•˜κΈ°
    time(&rawtime);
    // ν˜„μž¬μ˜ μ‹œκ°„μ„ 화면에 좜λ ₯
    printf("time : %u\n", (unsigned)rawtime);

    // ν˜„μž¬μ˜ μ‹œκ°„ κ΅¬ν•˜κΈ°
    gettimeofday(&mytime, NULL);
    printf("gettimeofday : %ld/%d\n", mytime.tv_sec, mytime.tv_usec);

    // ν˜„μž¬μ˜ μ‹œκ°„μ„ λ¬Έμžμ—΄λ‘œ λ°”κΏ”μ„œ 좜λ ₯
    printf("ctime : %s", ctime(&rawtime));

    // ν™˜κ²½ λ³€μˆ˜λ₯Ό μ„€μ •ν•œλ‹€
    putenv("TZ=PST3PDT");
    // TZ λ³€μˆ˜ μ„€μ •ν•œλ‹€
    tzset();
    tm = localtime(&rawtime);
    // ν˜„μž¬μ˜ μ‹œκ°„μ„ tm ꡬ쑰체λ₯Ό μ΄μš©ν•΄μ„œ 좜λ ₯
    printf("asctime : %s", asctime(tm));

    // μ‚¬μš©μž μ •μ˜ λ¬Έμžμ—΄ μ €μž₯
    strftime(buf, sizeof(buf), "%a %b %e %H:%M:%S %Y", tm);
    printf("strftime : %s\n", buf);

    return 0;
}

/*
time() : μ‹œκ°„ 좜λ ₯을 μœ„ν•΄ λ¨Όμ € ν˜„μž¬ μ‹œκ°„μ„ κ°€μ Έμ˜¨λ‹€ (μ΄ν•΄ν•˜κΈ° νž˜λ“  큰 숫자둜 λ‚˜μ˜¨λ‹€)
ctime(), asctime() : μ‚¬λžŒμ΄ 이해할 수 μžˆλŠ” ν˜•νƒœλ‘œ λ³€ν™˜ν•œλ‹€

μ‹œκ°„κ³Ό κ΄€λ ¨λœ κ΅¬μ‘°μ²΄λŠ” tm ꡬ쑰체
gmtime() : UTC μ‹œκ°„μ„ λ°˜ν™˜
localtime() : ν˜„μž¬ λ‘œμ»¬μ— λ§žλŠ” μ‹œκ°„ μ‚¬μš©
strftime() : μ›ν•˜λŠ” μ„œμ‹μ— λ§žλ„λ‘ 좜λ ₯

ν˜„μž¬ 좜λ ₯λ˜λŠ” μ‹œκ°„λŒ€λ₯Ό λ°”κΎΈκ³  μ‹Άλ‹€λ©΄ ν™˜κ²½ λ³€μˆ˜ μ‘°μ •
putenv() : ν˜„μž¬ ν™˜κ²½μ„ μ„€μ •
"TZ" : μ‹œκ°„λŒ€μ™€ κ΄€λ ¨λœ ν™˜κ²½ λ³€μˆ˜
tzset() : λ³€κ²½λœ ν™˜κ²½ λ³€μˆ˜
setlocale() : λ‘œμΌ€μΌ λ³€κ²½

gani@gani:~/raspi/File $ gcc -o time time.c
gani@gani:~/raspi/File $ ./time
*/
gcc -o time time.c
./time

728x90
λ°˜μ‘ν˜•
Comments