// WECP100/book/getdatetime.c 20240804
// description: get datetime using system call, on LINUX
// machine: laptop window11
// os: linux using wsl on windows11
//> wsl >> Welcome to Ubuntu 22.04.2 LTS //on power shell
//$ cat /etc/os-release >> PRETTY_NAME="Ubuntu 22.04.2 LTS"
//$ uname -a >> inux LAPTOP-2E5CQCBT 6.6.36.3-microsoft-standard-WSL2 #1
// >> Sat Jun 29 07:01:04 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
// language: gcc --version >> gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
//$ cd /mnt/c/Users/miura/Documents/62WECP100/book
//$ gcc getdatetime.c -o getdatetime
//$ ./getdatetime
// 2024/08/04T16:02:53.268314
// 2024/08/04T16:02:53
// 2024/08/04
// 16:02:53
// 2024/08/04T16:02:53
// 2024/08/04T16:02:54
#include< sys/time.h> // gettimeofday get high-resolution time.
#include< time.h> // localtime() strftime()
#include< stdio.h> // perror() printf()
#include< unistd.h> // sleep
int main(){
struct timeval tv; // date time format
char buf[256]; //
// if (gettimeofday(&tv, NULL) < 0) { //
// perror("gettimeofday"); // handing error
// return 1; // error return
// }
gettimeofday(&tv, NULL);
strftime(buf, sizeof buf, "%Y/%m/%dT%H:%M:%S", localtime(&tv.tv_sec));
printf("%s.%06ld\n", buf, tv.tv_usec);
printf("%s\n", buf);
strftime(buf, sizeof buf, "%Y/%m/%d", localtime(&tv.tv_sec));
printf("%s\n", buf);
strftime(buf, sizeof buf, "%H:%M:%S", localtime(&tv.tv_sec));
printf("%s\n", buf);
while(1){
gettimeofday(&tv, NULL) ; // now time
strftime(buf, sizeof buf, "{\"datetime\":\"%Y/%m/%dT%H:%M:%S\"}", localtime(&tv.tv_sec));
printf("%s\n", buf);
sleep(1);
}
}
//--------------------------------------------------