2012-06-20 32 views
4

我在寫一個批處理腳本,我需要unix時間。在linux下很容易,但我不知道如何在Windows中執行此操作。什麼是命令「日期+%s」的窗口等效

+1

也許[this](http://stackoverflow.com/questions/203090/how-to-get-current-datetime-on-windows-command-line-in-a-suitable-format-for-us)將有所幫助。 – ArtemStorozhuk

回答

11

這是本地批處理解決方案,應該可以在任何地區使用。它使用WMIC以獨立於語言環境的方式獲取當前本地時間。其他一切都是字符串解析和基本數學的「簡單」問題。

:UnixTime [ReturnVar] [TimeStamp] 
:: 
:: Computes the Unix time from the current local time as reported by the 
:: operating system. The Unix time is the number of seconds that have elapsed 
:: since midnight Coordinated Universal Time (UTC), January 1, 1970, not 
:: counting leap seconds. 
:: 
:: The result is returned in variable ReturnVar, 
:: or the result is echoed if ReturnVar is not specified 
:: 
:: If the TimeStamp is provided in the 2nd parameter, then the Unix time for 
:: the TimeStamp is computed, rather then for the current time. 
:: 
:: The TimeStamp must have the same format as used by WMIC: 
:: 
:: YYYYMMDDhhmmss.ffffffSzzz 
:: 
:: where: 
:: 
:: YYYY = gregorian year 
:: MM  = month 
:: DD  = day 
:: hh  = hour in 24 hour format 
:: mm  = minute 
:: ss  = seconds 
:: ffffff = fractional seconds (microseconds) 
:: S  = timezone sign: + or - 
:: zzz = timezone: minutes difference from GMT 
:: 
:: Each component must be zero prefixed as needed to maintain the proper width. 
:: 
:: The ReturnVar parameter must be provided in order to use the TimeStamp. 
:: A ReturnVar of "" will function the same as no ReturnVar. This enables the 
:: specification of a TimeStamp without an actual ReturnVar. 
:: 
@echo off 
setlocal 
set "ts=%~2" 
if not defined ts for /f "skip=1 delims=" %%A in ('wmic os get localdatetime') do if not defined ts set "ts=%%A" 
set /a "yy=10000%ts:~0,4% %% 10000, mm=100%ts:~4,2% %% 100, dd=100%ts:~6,2% %% 100" 
set /a "dd=dd-2472663+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4" 
set /a ss=(((1%ts:~8,2%*60)+1%ts:~10,2%)*60)+1%ts:~12,2%-366100-%ts:~21,1%((1%ts:~22,3%*60)-60000) 
set /a ss+=dd*86400 
endlocal & if "%~1" neq "" (set %~1=%ss%) else echo %ss% 
exit /b 


注意,該解決方案具有有限的壽命。當Unix時間超過32位有符號整數的最大值時,它將在2038-01-19停止工作。

EDIT - 該代碼已被編輯,以支持命令行而不是當前本地時間上的時間戳串的轉換。精確的時間範圍是1901-12-13 20:45:52.000000到2038-01-19 03:14:07.999999 GMT。 1970-01-01 00:00:00:00之前的時間將產生負值。

+0

它不工作,除非你是管理員(又名根在Windows) – pizza

+0

@pizza - 這是我的新聞。這個腳本在沒有管理權限的情況下在Windows 7上可以正常工作。我很確定它也適用於其他Windows版本。你真的測試過腳本嗎?如果是這樣,什麼Windows版本? – dbenham

+0

winxp:無法註冊mof文件。 只有管理員組成員可以使用WMIC.EXE。 原因:Win32錯誤:訪問被拒絕。 – pizza

3

如果通過「unix time」您的意思是epoch second,那麼Windows不包含生成該工具的工具。相反,您可以安裝第三方工具。例如:

  • 安裝Cygwin
  • 查找date二進制文件(在C:\Cygwin\下或者您安裝它的任何地方)
  • 在Linux中使用它。

或者,每對這個答案的真棒評論,你可以安裝GNU Coreutils其中還包括date命令。它包含了許多您可能不需要的其他工具,但Cygwin也是如此。

+0

什麼? Downvotes沒有評論? – ghoti

+0

無需安裝Cygwin即可獲取單個工具。大多數(所有?)GNU工具都可以下載和使用,無需Cygwin –

+0

是的,這就是我所說的unix時間。 – whoabackoff

3

您可以在Windows中使用vbscript,解釋器可在您的系統上使用。

'--------------------epoch.vbs----------------------- 
option explicit 
dim s,o,z 
for each o in GetObject("winmgmts:").InstancesOf ("Win32_OperatingSystem") 
z=o.CurrentTimeZone 
next 
s=DateDiff("s", "01/01/1970 00:00:00", Now())-(60*z) 
wscript.echo(s) 
wscript.quit 
+0

這不考慮時區差異。它也會受到夏令時的影響。 – dbenham

+0

更改爲處理時區。 – pizza

+0

+1,現在看起來不錯 – dbenham

相關問題