Bash에서 날짜 시간 문자열을 epoch로 변환
날짜 시간 문자열은 다음 형식입니다. 06/12/2012 07:21:22. UNIX 타임 스탬프 또는 에포크로 변환하려면 어떻게해야합니까?
당신이 찾고있는 것은입니다 date --date='06/12/2012 07:21:22' +"%s"
. --date
및 %s
형식 문자열은 모두 GNU 확장 이므로 GNU coreutils를 사용하고 있다고 가정합니다 . POSIX는 둘 중 하나를 지정하지 않으므로 POSIX 호환 시스템에서도 이러한 변환을 수행하는 이식 가능한 방법이 없습니다.
의 다른 버전에 대해서는 해당 매뉴얼 페이지를 참조하십시오 date
.
참고 : bash --date
및 -d
option은 날짜를 US 또는 ISO8601 형식 (예 : UK, EU 또는 기타 형식이 아닌 mm/dd/yyyy
또는) 으로 예상 yyyy-mm-dd
합니다.
Linux의 경우이 명령을 실행하십시오.
date -d '06/12/2012 07:21:22' +"%s"
Mac OSX의 경우 다음 명령을 실행하십시오.
date -j -u -f "%a %b %d %T %Z %Y" "Tue Sep 28 19:35:15 EDT 2010" "+%s"
이러한 답변 중 상당수는 지나치게 복잡하고 변수 사용 방법이 누락되었습니다. 다음은 표준 Linux 시스템에서 더 간단하게 수행하는 방법입니다 (이전에 언급했듯이 Mac 사용자의 경우 date 명령을 조정해야 함).
샘플 스크립트 :
#!/bin/bash
orig="Apr 28 07:50:01"
epoch=$(date -d "${orig}" +"%s")
epoch_to_date=$(date -d @$epoch +%Y%m%d_%H%M%S)
echo "RESULTS:"
echo "original = $orig"
echo "epoch conv = $epoch"
echo "epoch to human readable time stamp = $epoch_to_date"
결과 :
RESULTS:
original = Apr 28 07:50:01
epoch conv = 1524916201
epoch to human readable time stamp = 20180428_075001
또는 함수로 :
# -- Converts from human to epoch or epoch to human, specifically "Apr 28 07:50:01" human.
# typeset now=$(date +"%s")
# typeset now_human_date=$(convert_cron_time "human" "$now")
function convert_cron_time() {
case "${1,,}" in
epoch)
# human to epoch (eg. "Apr 28 07:50:01" to 1524916201)
echo $(date -d "${2}" +"%s")
;;
human)
# epoch to human (eg. 1524916201 to "Apr 28 07:50:01")
echo $(date -d "@${2}" +"%b %d %H:%M:%S")
;;
esac
}
get_curr_date () {
# get unix time
DATE=$(date +%s)
echo "DATE_CURR : "$DATE
}
conv_utime_hread () {
# convert unix time to human readable format
DATE_HREAD=$(date -d @$DATE +%Y%m%d_%H%M%S)
echo "DATE_HREAD : "$DATE_HREAD
}
백그라운드 전용 프로세스date
로 사용 하는 효율적인 솔루션
이런 종류의 번역을 훨씬 더 빠르게 하기 위해 ...
소개
이 게시물에서는
- 다음 은 Quick Demo입니다 .
- 일부 설명 ,
- 기능 많은 가능한 취소 * X 도구 (
bc
,rot13
,sed
...).
빠른 데모
fifo=$HOME/.fifoDate-$$
mkfifo $fifo
exec 5> >(exec stdbuf -o0 date -f - +%s >$fifo 2>&1)
echo now 1>&5
exec 6< $fifo
rm $fifo
read -t 1 -u 6 now
echo $now
현재 UNIXTIME을 출력해야합니다 . 거기에서 비교할 수 있습니다
time for i in {1..5000};do echo >&5 "now" ; read -t 1 -u6 ans;done
real 0m0.298s
user 0m0.132s
sys 0m0.096s
과:
time for i in {1..5000};do ans=$(date +%s -d "now");done
real 0m6.826s
user 0m0.256s
sys 0m1.364s
From more than 6 seconds to less than a half second!!(on my host).
You could check echo $ans
, replace "now"
by "2019-25-12 20:10:00"
and so on...
Optionaly, you could, once requirement of date subprocess ended:
exec 5>&- ; exec 6<&-
Original post (detailed explanation)
Instead of running 1 fork by date to convert, run date
just 1 time and do all convertion with same process (this could become a lot quicker)!:
date -f - +%s <<eof
Apr 17 2014
May 21 2012
Mar 8 00:07
Feb 11 00:09
eof
1397685600
1337551200
1520464020
1518304140
Sample:
start1=$(LANG=C ps ho lstart 1)
start2=$(LANG=C ps ho lstart $$)
dirchg=$(LANG=C date -r .)
read -p "A date: " userdate
{ read start1 ; read start2 ; read dirchg ; read userdate ;} < <(
date -f - +%s <<<"$start1"$'\n'"$start2"$'\n'"$dirchg"$'\n'"$userdate" )
Then now have a look:
declare -p start1 start2 dirchg userdate
(may answer something like:
declare -- start1="1518549549" declare -- start2="1520183716" declare -- dirchg="1520601919" declare -- userdate="1397685600"
This was done in one execution!
Using long running subprocess
We just need one fifo:
mkfifo /tmp/myDateFifo
exec 7> >(exec stdbuf -o0 /bin/date -f - +%s >/tmp/myDateFifo)
exec 8</tmp/myDateFifo
rm /tmp/myDateFifo
(Note: As process is running and all descriptors are opened, we could safely remove fifo's filesystem entry.)
Then now:
LANG=C ps ho lstart 1 $$ >&7
read -u 8 start1
read -u 8 start2
LANG=C date -r . >&7
read -u 8 dirchg
read -p "Some date: " userdate
echo >&7 $userdate
read -u 8 userdate
We could buid a little function:
mydate() {
local var=$1;
shift;
echo >&7 $@
read -u 8 $var
}
mydate start1 $(LANG=C ps ho lstart 1)
echo $start1
Or use my newConnector
function
With functions for connecting MySQL/MariaDB
, PostgreSQL
and SQLite
...
You may find them in different version on GitHub, or on my site: download or show.
wget https://raw.githubusercontent.com/F-Hauri/Connector-bash/master/shell_connector.bash
. shell_connector.bash
newConnector /bin/date '-f - +%s' @0 0
myDate "2018-1-1 12:00" test
echo $test
1514804400
Nota: On GitHub, functions and test are separated files. On my site test are run simply if this script is not sourced.
# Exit here if script is sourced
[ "$0" = "$BASH_SOURCE" ] || { true;return 0;}
참고URL : https://stackoverflow.com/questions/10990949/convert-date-time-string-to-epoch-in-bash
'Development Tip' 카테고리의 다른 글
IP 주소로 사용자 위치 가져 오기 (0) | 2020.11.01 |
---|---|
이벤트 로그에서 앱 풀 재활용을 찾는 방법 (0) | 2020.11.01 |
다운로드 한 파일의 이름을 wget으로 바꾸는 방법은 무엇입니까? (0) | 2020.11.01 |
phpstorm 미해결 함수 또는 메서드 $ () (0) | 2020.11.01 |
수은 체인지 셋의 사용자 이름을 변경할 수 있습니까? (0) | 2020.11.01 |