js 取得 Unix时间戳
Unix时间戳(Unix timestamp),或称Unix时间(Unix time)、POSIX时间(POSIX time),是一种时间表示方式,定义为从格林威治时间1970年01月01日00时00分00秒起至现在的总秒数。Unix时间戳不仅被使用在Unix 系统、类Unix系统中,也在许多其他操作系统中被广告采用。
目前相当一部分操作系统使用32位二进制数字表示时间。此类系统的Unix时间戳最多可以使用到格林威治时间2038年01月19日03时14分07秒(二进制:01111111 11111111 11111111 11111111)。其后一秒,二进制数字会变为10000000 00000000 00000000 00000000, 发生溢出错误,造成系统将时间误解为1901年12月13日20时45分52秒。这很可能会引起软件故障,甚至是系统瘫痪。使用64位二进制数字表示时间 的系统(最多可以使用到格林威治时间292,277,026,596年12月04日15时30分08秒)则基本不会遇到这类溢出问题。
如何在不同编程语言中获取现在的Unix时间戳(Unix timestamp)?
| Java | time |
| JavaScript | Math.round(new Date().getTime()/1000) getTime()返回数值的单位是毫秒 |
| Microsoft .NET / C# | epoch = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000 |
| MySQL | SELECT unix_timestamp(now()) |
| Perl | time |
| PHP | time() |
| PostgreSQL | SELECT extract(epoch FROM now()) |
| Python | 先 import time 然后 time.time() |
| Ruby | 获取Unix时间戳:Time.now 或 Time.new 显示Unix时间戳:Time.now.to_i |
| SQL Server | SELECT DATEDIFF(s, '1970-01-01 00:00:00', GETUTCDATE()) |
| Unix / Linux | date +%s |
| VBScript / ASP | DateDiff("s", "01/01/1970 00:00:00", Now()) |
| 其他操作系统 (如果Perl被安装在系统中) | 命令行状态:perl -e "print time" |
如何在不同编程语言中实现Unix时间戳(Unix timestamp) → 普通时间?
| Java | String date = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new java.util.Date(Unix timestamp * 1000)) |
| JavaScript | 先 var unixTimestamp = new Date(Unix timestamp * 1000) 然后 commonTime = unixTimestamp.toLocaleString() |
| Linux | date -d @Unix timestamp |
| MySQL | from_unixtime(Unix timestamp) |
| Perl | 先 my $time = Unix timestamp 然后 my ($sec, $min, $hour, $day, $month, $year) = (localtime($time))[0,1,2,3,4,5,6] |
| PHP | date('r', Unix timestamp) |
| PostgreSQL | SELECT TIMESTAMP WITH TIME ZONE 'epoch' + Unix timestamp) * INTERVAL '1 second'; |
| Python | 先 import time 然后 time.gmtime(Unix timestamp) |
| Ruby | Time.at(Unix timestamp) |
| SQL Server | DATEADD(s, Unix timestamp, '1970-01-01 00:00:00') |
| VBScript / ASP | DateAdd("s", Unix timestamp, "01/01/1970 00:00:00") |
| 其他操作系统 (如果Perl被安装在系统中) | 命令行状态:perl -e "print scalar(localtime(Unix timestamp))" |
我们来开发一个用家用摄像头捕捉图像的小程序。程序的主要代码如下,其中步骤说明将插入到代码当中。
步骤一:
在C++ builder中新建的Form上插入:2个button,一个panel,一个ComboBox和一个SaveDialg
步骤二:编写代码
//----------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "vfw.h" //双击Form后,在代码区先加入vfw头文件。
//----------------------------------------------------------------------------
#pragma resource "*.dfm"
#pragma package(smart_init)
TForm1 *Form1; //加入自定义变量。
HWND hWndC;
CAPDRIVERCAPS CapDrvCaps;
CAPSTATUS CapStatus;
//----------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent *Owner)
: TForm(Owner)
{
}
//----------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Table1->Open();
}
//----------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender) // 加入启动camere的程序。
{
char szDeviceName[80];
char szDeviceVersion[80];
for (int wIndex=0;wIndex<10;wIndex++)
{
if (capGetDriverDescription(wIndex,szDeviceName,sizeof(szDeviceName),szDeviceVersion,sizeof(szDeviceVersion)))
{
Camera->Items->Add(szDeviceName);
}
}
if(Camera->Items->Count>0)
Camera->ItemIndex=0;
else
{
ShowMessage("There is no camera!");
Close();
}
//检查视频是否处于实时捕获状态
if(CapStatus.fLiveWindow==1)
//CapStatus.fLiveWindow=1表明当前初频已处于实时捕获状态,否则CapStatus.fLiveWindow=0
{
ShowMessage("You needn't do it again!");
return;
}
hWndC=capCreateCaptureWindow((LPSTR)"My Capture Window",WS_CHILD | WS_VISIBLE,0,0,160,120,(HWND)Panel2->Handle,Camera->ItemIndex+1);
//连接设备:
capDriverConnect(hWndC,0);
capPreviewRate(hWndC,50); // rate, in milliseconds
capPreview(hWndC,TRUE); // starts preview
//获取视频驱动相关性能
capDriverGetCaps(hWndC,&CapDrvCaps,sizeof(CAPDRIVERCAPS));
//获取捕获窗口状态
capGetStatus(hWndC,&CapStatus,sizeof(CAPSTATUS));
SetWindowPos(hWndC,NULL,0,0,CapStatus.uiImageWidth,CapStatus.uiImageHeight,SWP_NOZORDER | SWP_NOMOVE);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
if(hWndC)
{
capPreview(hWndC,FALSE);
capDriverDisconnect(hWndC);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender) //加入图像保存程序。
{
if(SavePictureDialog1->Execute())
capFileSaveDIB(hWndC,(SavePictureDialog1->FileName+".bmp").c_str());
}
//---------------------------------------------------------------------------
步骤三:
运行并测试。
步骤一:
在C++ builder中新建的Form上插入:2个button,一个panel,一个ComboBox和一个SaveDialg
步骤二:编写代码
//----------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "vfw.h" //双击Form后,在代码区先加入vfw头文件。
//----------------------------------------------------------------------------
#pragma resource "*.dfm"
#pragma package(smart_init)
TForm1 *Form1; //加入自定义变量。
HWND hWndC;
CAPDRIVERCAPS CapDrvCaps;
CAPSTATUS CapStatus;
//----------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent *Owner)
: TForm(Owner)
{
}
//----------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Table1->Open();
}
//----------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender) // 加入启动camere的程序。
{
char szDeviceName[80];
char szDeviceVersion[80];
for (int wIndex=0;wIndex<10;wIndex++)
{
if (capGetDriverDescription(wIndex,szDeviceName,sizeof(szDeviceName),szDeviceVersion,sizeof(szDeviceVersion)))
{
Camera->Items->Add(szDeviceName);
}
}
if(Camera->Items->Count>0)
Camera->ItemIndex=0;
else
{
ShowMessage("There is no camera!");
Close();
}
//检查视频是否处于实时捕获状态
if(CapStatus.fLiveWindow==1)
//CapStatus.fLiveWindow=1表明当前初频已处于实时捕获状态,否则CapStatus.fLiveWindow=0
{
ShowMessage("You needn't do it again!");
return;
}
hWndC=capCreateCaptureWindow((LPSTR)"My Capture Window",WS_CHILD | WS_VISIBLE,0,0,160,120,(HWND)Panel2->Handle,Camera->ItemIndex+1);
//连接设备:
capDriverConnect(hWndC,0);
capPreviewRate(hWndC,50); // rate, in milliseconds
capPreview(hWndC,TRUE); // starts preview
//获取视频驱动相关性能
capDriverGetCaps(hWndC,&CapDrvCaps,sizeof(CAPDRIVERCAPS));
//获取捕获窗口状态
capGetStatus(hWndC,&CapStatus,sizeof(CAPSTATUS));
SetWindowPos(hWndC,NULL,0,0,CapStatus.uiImageWidth,CapStatus.uiImageHeight,SWP_NOZORDER | SWP_NOMOVE);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
if(hWndC)
{
capPreview(hWndC,FALSE);
capDriverDisconnect(hWndC);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender) //加入图像保存程序。
{
if(SavePictureDialog1->Execute())
capFileSaveDIB(hWndC,(SavePictureDialog1->FileName+".bmp").c_str());
}
//---------------------------------------------------------------------------
步骤三:
运行并测试。
在内网论坛看到有人发帖说需要一个照片自动归档的软件,就是把图片可以按照图片的日期放到相应的文件夹中,感觉用AutoIt这种软件应该挺好实现的,就写了一个,代码如下
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Opt('MustDeclareVars', 1)
AutoPhoto()
Func AutoPhoto()
Local $font,$msg,$Button_1,$radio1,$radio2,$radio3,$radio4,$isOldPhoto,$isCreatDate
$isOldPhoto=True;
$isCreatDate=True;
GUICreate("图片自动归档程序V1.1--Made By 蓝色天空",500,200) ; will create a dialog box that when displayed is centered
GUISetState(@SW_SHOW) ; will display an empty dialog box
GUICtrlCreateLabel("这是一个图片自动归档程序,请把该软件放到图片的同一目录下。", -1, 10) ; next line
GUICtrlCreateLabel("选择是否保留原图片和归档日期的方式后,点击按钮后图片即可自动归档", -1, 30) ; next line
$radio1 = GUICtrlCreateRadio("保留原图片", 10, 60, 100, 20)
$radio2 = GUICtrlCreateRadio("不保留原图片", 150, 60, 100, 20)
GUIStartGroup()
GUICtrlSetState($radio1, $GUI_CHECKED)
$radio3 = GUICtrlCreateRadio("按创建日期归档", 10, 80, 100, 20)
$radio4 = GUICtrlCreateRadio("按修改日期归档", 150, 80, 100, 20)
GUIStartGroup()
GUICtrlSetState($radio3, $GUI_CHECKED)
$Button_1 = GUICtrlCreateButton("开始自动归档", 10, 120, 100)
; Run the GUI until the dialog is closed
While 1
$msg = GUIGetMsg()
Select
Case $msg = $GUI_EVENT_CLOSE
ExitLoop
Case $msg = $radio1 And BitAND(GUICtrlRead($radio1), $GUI_CHECKED) = $GUI_CHECKED
$isOldPhoto=True;
Case $msg = $radio2 And BitAND(GUICtrlRead($radio2), $GUI_CHECKED) = $GUI_CHECKED
$isOldPhoto=False;
Case $msg = $radio3 And BitAND(GUICtrlRead($radio3), $GUI_CHECKED) = $GUI_CHECKED
$isCreatDate=True;
Case $msg = $radio4 And BitAND(GUICtrlRead($radio4), $GUI_CHECKED) = $GUI_CHECKED
$isCreatDate=False;
Case $msg = $Button_1
doAutoPhoto($isOldPhoto,$isCreatDate); Will Run/Open Notepad
EndSelect
WEnd
GUIDelete()
EndFunc ;==>Example1
Func doAutoPhoto($isOldPhoto,$isCreatDate)
Local $search,$file,$t,$yyyymd
; 显示当前目录中所有文件的文件名
$search = FileFindFirstFile("*.jpg")
; 检查搜索是否成功
If $search = -1 Then
MsgBox(0, "错误", "该目录下已经没有jpg图片文件")
Exit
EndIf
While 1
$file = FileFindNextFile($search)
If $isCreatDate=True Then
$t = FileGetTime($file, 1)
EndIf
If $isCreatDate=False Then
$t = FileGetTime($file, 0)
EndIf
If Not @error Then
$yyyymd = $t[0] & "年" & $t[1] & "月" & $t[2] & "日"
;MsgBox(0, $file+"的创建日期:", $yyyymd)
If $isOldPhoto=True Then
FileCopy($file, @ScriptDir & "\" & $yyyymd & "\", 8)
EndIf
If $isOldPhoto=False Then
FileMove($file, @ScriptDir & "\" & $yyyymd & "\", 8)
EndIf
;MsgBox(4096, "文件:", @ScriptDir & "\" & $yyyymd & "\")
EndIf
If @error Then ExitLoop
;MsgBox(4096, "文件:", $file)
WEnd
FileClose($search)
MsgBox(64, "海蓝", "恭喜,所有图片归档完毕")
EndFunc
使用时将这个软件和你需要整理的图片放到同一个目录下,然后点击那个“开始自动归档”按钮,选择是否保留原图片,以及是按创建日期归档,还是按修改日期归档。然后点击下面的按钮就好了

原先目录下有4张图片

当运行程序后,会变为如下的样子,会按照日期进行归档

下载地址:
下载文件
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Opt('MustDeclareVars', 1)
AutoPhoto()
Func AutoPhoto()
Local $font,$msg,$Button_1,$radio1,$radio2,$radio3,$radio4,$isOldPhoto,$isCreatDate
$isOldPhoto=True;
$isCreatDate=True;
GUICreate("图片自动归档程序V1.1--Made By 蓝色天空",500,200) ; will create a dialog box that when displayed is centered
GUISetState(@SW_SHOW) ; will display an empty dialog box
GUICtrlCreateLabel("这是一个图片自动归档程序,请把该软件放到图片的同一目录下。", -1, 10) ; next line
GUICtrlCreateLabel("选择是否保留原图片和归档日期的方式后,点击按钮后图片即可自动归档", -1, 30) ; next line
$radio1 = GUICtrlCreateRadio("保留原图片", 10, 60, 100, 20)
$radio2 = GUICtrlCreateRadio("不保留原图片", 150, 60, 100, 20)
GUIStartGroup()
GUICtrlSetState($radio1, $GUI_CHECKED)
$radio3 = GUICtrlCreateRadio("按创建日期归档", 10, 80, 100, 20)
$radio4 = GUICtrlCreateRadio("按修改日期归档", 150, 80, 100, 20)
GUIStartGroup()
GUICtrlSetState($radio3, $GUI_CHECKED)
$Button_1 = GUICtrlCreateButton("开始自动归档", 10, 120, 100)
; Run the GUI until the dialog is closed
While 1
$msg = GUIGetMsg()
Select
Case $msg = $GUI_EVENT_CLOSE
ExitLoop
Case $msg = $radio1 And BitAND(GUICtrlRead($radio1), $GUI_CHECKED) = $GUI_CHECKED
$isOldPhoto=True;
Case $msg = $radio2 And BitAND(GUICtrlRead($radio2), $GUI_CHECKED) = $GUI_CHECKED
$isOldPhoto=False;
Case $msg = $radio3 And BitAND(GUICtrlRead($radio3), $GUI_CHECKED) = $GUI_CHECKED
$isCreatDate=True;
Case $msg = $radio4 And BitAND(GUICtrlRead($radio4), $GUI_CHECKED) = $GUI_CHECKED
$isCreatDate=False;
Case $msg = $Button_1
doAutoPhoto($isOldPhoto,$isCreatDate); Will Run/Open Notepad
EndSelect
WEnd
GUIDelete()
EndFunc ;==>Example1
Func doAutoPhoto($isOldPhoto,$isCreatDate)
Local $search,$file,$t,$yyyymd
; 显示当前目录中所有文件的文件名
$search = FileFindFirstFile("*.jpg")
; 检查搜索是否成功
If $search = -1 Then
MsgBox(0, "错误", "该目录下已经没有jpg图片文件")
Exit
EndIf
While 1
$file = FileFindNextFile($search)
If $isCreatDate=True Then
$t = FileGetTime($file, 1)
EndIf
If $isCreatDate=False Then
$t = FileGetTime($file, 0)
EndIf
If Not @error Then
$yyyymd = $t[0] & "年" & $t[1] & "月" & $t[2] & "日"
;MsgBox(0, $file+"的创建日期:", $yyyymd)
If $isOldPhoto=True Then
FileCopy($file, @ScriptDir & "\" & $yyyymd & "\", 8)
EndIf
If $isOldPhoto=False Then
FileMove($file, @ScriptDir & "\" & $yyyymd & "\", 8)
EndIf
;MsgBox(4096, "文件:", @ScriptDir & "\" & $yyyymd & "\")
EndIf
If @error Then ExitLoop
;MsgBox(4096, "文件:", $file)
WEnd
FileClose($search)
MsgBox(64, "海蓝", "恭喜,所有图片归档完毕")
EndFunc
使用时将这个软件和你需要整理的图片放到同一个目录下,然后点击那个“开始自动归档”按钮,选择是否保留原图片,以及是按创建日期归档,还是按修改日期归档。然后点击下面的按钮就好了
原先目录下有4张图片
当运行程序后,会变为如下的样子,会按照日期进行归档
下载地址:
下载文件 







