本帖最后由 xms77 于 2013-3-29 12:44 编辑
这个是公司的排班表,我们公司是4班2运转,4个班分别是:Team1,Team2,Team3,Team4, 每4个星期白晚班会轮换一次,比如说:Team1
第1周(FW1)是从星期天到星期二上白班,星期三到星期六休息
第2周(FW2)是从星期天到星期三上白班,星期四到星期六休息
第3周(FW3)是从星期天到星期二上白班,星期三到星期六休息
第4周(FW4)是从星期天到星期三上白班,星期四到星期六休息
第5周(FW5)倒班了,是从星期天到星期二上晚班班,星期三到星期六休息
第6周(FW6)是从星期天到星期三上晚班班,星期四到星期六休息
。。。
。。。
我想要的结果就是可以从某个日期和时间来推断是哪个班在上班?
刚刚开始思考,脑子里面很乱,没有什么头绪,不知道大家有没有好的idea?
几乎废了一夜,总算完成了自己的代码,代码很冗长,不如5楼大师的算法简洁。
#include <date.au3>
#include <array.au3>
Local $return = _Date2WeekYearTeamDayNight("2013-02-13 20:32:29")
If IsArray($return) Then _ArrayDisplay($return)
;偶数周,Team1,3是日,一,二,三上班; Team2,4是四,五,六上班
;奇数周,Team1,3是日,一,二上班; Team2,4是三,四,五,六上班
;Weeks4Turn的值是偶数:Team1夜班,Team3白班, Team2夜班,Team4白班
;Weeks4Turn的值是奇数:Team1白班,Team3夜班,Team2白班,Team4夜班
Func _Date2WeekYearTeamDayNight($iDateTime) ;日期时间格式:'2013-03-17 08:32:29'
Local $AllInfo[3]
$iDateTime = StringStripWS($iDateTime,3)
if Not StringRegExp($iDateTime,"\d{4}[-/]\d{1,2}[-/]\d{1,2} \d{1,2}:\d{2}:\d{2}",0) Then Return SetError(-1) ;返回日期格式错误
Local $temp = StringRegExp($iDateTime,'(\d{4}[-/]\d{1,2}[-/]\d{1,2})',1)
Local $date = $temp[0]
Local $temp = StringRegExp($iDateTime,'(\d{1,2}:\d{2}:\d{2})',1)
Local $time = $temp[0]
Local $DayNight = _JudgeDayNightShift($time)
If @error Then Return SetError(-1) ;返回日期格式错误
If $DayNight = "N-" Then ;把日期转换为前一天的晚上
$iDateTime = _DatetimeBack8Hours($iDateTime)
MsgBox(0,"new datetime is ",$iDateTime)
$temp = StringRegExp($iDateTime,'(\d{4}[-/]\d{1,2}[-/]\d{1,2})',1)
$date = $temp[0]
$temp = StringRegExp($iDateTime,'(\d{1,2}:\d{2}:\d{2})',1)
$time = $temp[0]
EndIf
$DayNight = _JudgeDayNightShift($time)
$FiscalYear = _Date2FiscalYear($date)
$WeekName = _Date2WeekName($date)
$weeks = _Data2Weeks($date)
$Weeks4Turn = _DateCheck4WeeksTurn($date)
$AllInfo[0] = "WW" & $weeks & $FiscalYear
Local $DayNight = _JudgeDayNightShift($time)
If Not @error Then
Select
Case $DayNight = "D"
$AllInfo[2] = "D"
Select
Case $Weeks4Turn = 0 ;偶数
If $WeekName = 7 Or $WeekName = 1 Or $WeekName = 2 Or $WeekName = 4 Then ;如果是星期日,一,二,三
$AllInfo[1] = "T3"
Else
$AllInfo[1] = "T4"
EndIf
Case $Weeks4Turn = 1 ;奇数
If $WeekName = 7 Or $WeekName = 1 Or $WeekName = 2 Then ;如果是星期日,一,二,三
$AllInfo[1] = "T1"
Else
$AllInfo[1] = "T2"
EndIf
EndSelect
Case $DayNight = "N"
$AllInfo[2] = "N"
Select
Case $Weeks4Turn = 0 ;偶数周
If $WeekName = 7 Or $WeekName = 1 Or $WeekName = 2 Or $WeekName = 4 Then ;如果是星期日,一,二,三
$AllInfo[1] = "T1"
Else
$AllInfo[1] = "T2"
EndIf
Case $Weeks4Turn = 1 ;奇数周
If $WeekName = 7 Or $WeekName = 1 Or $WeekName = 2 Then ;如果是星期日,一,二,三
$AllInfo[1] = "T3"
Else
$AllInfo[1] = "T4"
EndIf
EndSelect
EndSelect
EndIf
Return $AllInfo
EndFunc
Func _JudgeDayNightShift($itime)
$itime = StringStripWS($itime,3)
If Not StringRegExp($itime,'\d{1,2}:\d{2}:\d{2}',0) Then Return SetError(-1)
Local $splitime = StringSplit($itime,':')
Select
Case Int($splitime[1]) >= 7 And Int($splitime[1]) < 19
Return "D"
Case Int($splitime[1]) >= 19
Return "N"
Case Int($splitime[1]) < 7
Return "N-"
EndSelect
EndFunc
Func _Date2WeekName($idate)
$idate= StringStripWS($idate,3)
if Not StringRegExp($idate,"\d{4}[-/]\d{1,2}[-/]\d{1,2}",0) Then Return SetError(-1) ;返回日期格式错误
Local $splitdate = StringSplit($idate,"-")
Local $WeekName = _DateToDayOfWeekISO($splitdate[1],$splitdate[2],$splitdate[3])
If $WeekName = 0 Then Return SetError(-2) ;转换日期出错
Return $WeekName
EndFunc
Func _Date2FiscalYear($idate)
$idate= StringStripWS($idate,3)
if Not StringRegExp($idate,"\d{4}[-/]\d{1,2}[-/]\d{1,2}",0) Then Return SetError(-1) ;返回日期格式错误
Local $splitdate = StringSplit($idate,"-")
Local $year = Int(StringRight($splitdate[1],2))
if Int($splitdate[2]) < 7 then
Return "FY"& $year
Else
Return "FY"& $year +1
EndIf
EndFunc
Func _Data2Weeks($idate)
$idate= StringStripWS($idate,3)
if Not StringRegExp($idate,"\d{4}[-/]\d{1,2}[-/]\d{1,2}",0) Then Return SetError(-1) ;返回日期格式错误
Local $iDateCalc = _DateDiff('d',"2012-6-30", $idate)
Local $Weeks = Mod(Int($iDateCalc/7)+1,52)
If $Weeks = 0 Then $Weeks = 52
Return $Weeks
EndFunc
Func _DateCheck4WeeksTurn($idate)
$idate= StringStripWS($idate,3)
if Not StringRegExp($idate,"\d{4}[-/]\d{1,2}[-/]\d{1,2}",0) Then Return SetError(-1) ;返回日期格式错误
Local $iDateCalc2 = _DateDiff('d',"2012-7-15", "2013-3-24")
Local $iWeeks4Turn = Mod(Int(Int($iDateCalc2/7)/4),2)
Return $iWeeks4Turn
EndFunc
Func _DatetimeBack8Hours($iDateTime)
$iDateTime = StringStripWS($iDateTime,3)
if Not StringRegExp($iDateTime,"\d{4}[-/]\d{1,2}[-/]\d{1,2} \d{1,2}:\d{2}:\d{2}",0) Then Return SetError(-1) ;返回日期格式错误
Local $temp = StringRegExp($iDateTime,'(\d{4}[-/]\d{1,2}[-/]\d{1,2})',1)
Local $date = $temp[0]
Local $temp = StringRegExp($iDateTime,'(\d{1,2}:\d{2}:\d{2})',1)
Local $time = $temp[0]
Local $newDate = _DateAdd('D',-1,$date)
Local $newTime = StringRegExpReplace($time,'\d{1,2}(:\d{2}:\d{2})','20$1')
Return $newDate & ' ' & $newTime
EndFunc
|