什么是Windows相当于命令&QUOT的,日期+%S" [英] what is the windows equivalent of the command "date+%s"

查看:230
本文介绍了什么是Windows相当于命令&QUOT的,日期+%S"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个批处理脚本,我需要的Unix时间。这是linux下容易,但我无法弄清楚如何在Windows做到这一点。

I'm writing a batch script and I need the unix time. It's easy under linux, but I can't figure out how to do this in windows.

推荐答案

下面是土生土长的一批解决方案应该在任何语言环境中工作。它使用WMIC来得到当前本地时间区域设置独立的方式。一切是字符串分析和基本数学的一个简单的事情。

Here is a native batch solution that should work in any locale. It uses WMIC to get the current local time in a locale independent manner. Everything else is a "simple" matter of string parsing and basic math.

: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年1月19日的工作。

修改 - 在code已被修改,以支持命令行,而不是当前当地时间时间戳字符串转换。支持倍precise范围是1901年12月13日20:45:52.000000通过2038年1月19日03:14:07.999999 GMT。次之前1970-01-01 00:00:00.000000将产生负值。

EDIT - The code has been edited to support conversion of a timestamp string on the command line instead of the current local time. The precise range of times supported is 1901-12-13 20:45:52.000000 through 2038-01-19 03:14:07.999999 GMT. Times prior to 1970-01-01 00:00:00.000000 will yield negative values.

这篇关于什么是Windows相当于命令&QUOT的,日期+%S"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆