cmd - 有没有办法在 Windows 命令提示符下以秒为单位获取当前时间?
问题描述
我的目标是创建并命名一个以秒为单位的当前时间作为名称的文件。在 IOS 中有touch "$(date +%s)_my_migration_name.up.sql"
我使用的,但我正在为 Windows 寻找类似的解决方案。
解决方案
可以获取自 1970-01-01 00:00:00 以来分配给环境变量的秒数,例如Seconds
使用子例程GetSeconds
,如以下批处理文件代码所示:
@echo off
call :GetSeconds
set "FileName=%Seconds%_my_migration_name.up.sql"
set FileName
pause
goto :EOF
:GetSeconds
setlocal EnableExtensions DisableDelayedExpansion
rem Get current date/time region (country) independent.
if exist %SystemRoot%\System32\robocopy.exe for /F "tokens=1-6 delims=/: " %%I in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do set "Year=%%I" & set "Month=%%J" & set "Day=%%K" & set "Hour=%%L" & set "Minute=%%M" & set "Second=%%N" & goto ProcessDateTime
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "DateTime=%%I"
set "Year=%DateTime:~0,4%" & set "Month=%DateTime:~4,2%" & set "Day=%DateTime:~6,2%" & set "Hour=%DateTime:~8,2%" & set "Minute=%DateTime:~10,2%" & set "Second=%DateTime:~12,2%"
:ProcessDateTime
rem echo Current date/time is: %Year%-%Month%-%Day% %Hour%:%Minute%:%Second%
rem Remove leading zeros from the date/time values or calculation could be wrong.
if %Month:~0,1% == 0 set "Month=%Month:~1%"
if %Day:~0,1% == 0 set "Day=%Day:~1%"
if %Hour:~0,1% == 0 set "Hour=%Hour:~1%"
if %Minute:~0,1% == 0 set "Minute=%Minute:~1%"
if %Second:~0,1% == 0 set "Second=%Second:~1%"
set /A "Index1=Year-1979"
set /A "Index2=Index1-30"
if %Index1% LEQ 30 (
rem Get number of days to year for the years 1980 to 2009.
for /F "tokens=%Index1% delims= " %%Y in ("3652 4018 4383 4748 5113 5479 5844 6209 6574 6940 7305 7670 8035 8401 8766 9131 9496 9862 10227 10592 10957 11323 11688 12053 12418 12784 13149 13514 13879 14245") do set "Days=%%Y"
for /F "tokens=%Index1% delims= " %%L in ("Y N N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N") do set "LeapYear=%%L"
) else (
rem Get number of days to year for the years 2010 to 2038.
for /F "tokens=%Index2% delims= " %%Y in ("14610 14975 15340 15706 16071 16436 16801 17167 17532 17897 18262 18628 18993 19358 19723 20089 20454 20819 21184 21550 21915 22280 22645 23011 23376 23741 24106 24472 24837") do set "Days=%%Y"
for /F "tokens=%Index2% delims= " %%L in ("N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N N") do set "LeapYear=%%L"
)
rem Add the days to month in year.
if "%LeapYear%" == "N" (
for /F "tokens=%Month% delims= " %%M in ("0 31 59 90 120 151 181 212 243 273 304 334") do set /A "Days+=%%M"
) else (
for /F "tokens=%Month% delims= " %%M in ("0 31 60 91 121 152 182 213 244 274 305 335") do set /A "Days+=%%M"
)
rem Add the complete days in month of year.
set /A "Days+=Day-1"
rem Calculate the seconds which is easy now.
set /A "Seconds=Days*86400+Hour*3600+Minute*60+Second"
endlocal & set "Seconds=%Seconds%"
rem echo Seconds since 1970: %Seconds%
rem Exit this subroutine.
goto :EOF
有关解释,请参见以下答案:
该子例程GetSeconds
在环境变量中返回Seconds
从 1970-01-01 00:00:00 到日期/时间范围 1980-01-01 00:00:00 到 2038-01-19 03:14:07 的秒数。所以必须在 2038 年更改代码。
注释(备注)可以全部从代码中删除,以便更快地处理批处理文件代码。
推荐阅读
- docker - 如何解决长期停留在“新”状态的 docker swarm 服务?[通过重新启动 swarm 集群解决短期问题]
- node.js - 为什么在咨询 mongo 时会出现服务器错误?
- android - Android 上的 Crashlytics 和 React Native
- javascript - 这种情况下 React useRef 和全局变量的区别
- c - gprof 以 0% 显示每个函数(甚至是摘要)
- python - 使用 SymPy 求解 ODE
- c++ - 运算符方法中的调用方法怪异行为
- swift - 更新解码的 JSON 并将其附加到结构中?
- flutter - 我对提供程序和 pageView 以及在 TextFormField 中过滤列表和验证器有一个大问题
- python - 如何使用 python 在特定索引处对字符串进行切片?