VARCHART XGantt用戶手冊(cè)(.NET版):如何使用日歷(下)
VARCHART XGantt是一個(gè)交互式的甘特圖控件,其模塊化的設(shè)計(jì)讓您可以創(chuàng)建滿足您和您的客戶所需求的應(yīng)用程序。相較于其他甘特圖控件,VARCHART XGantt穩(wěn)定性高,開發(fā)時(shí)間長,各大行業(yè)的知名公司都在使用它。本文主要描述了如何使用日歷教程中的如何使用日歷進(jìn)行計(jì)算的內(nèi)容,現(xiàn)在就跟著小編來了解一下吧~
點(diǎn)擊下載VARCHART XGantt免費(fèi)版
如何使用日歷進(jìn)行計(jì)算
日歷中的計(jì)算不一定在時(shí)間范圍內(nèi)可見。對(duì)象Calendar的AddDuration方法從開始日期和指定的工作時(shí)間單位數(shù)計(jì)算最終日期,同時(shí)考慮到非工作時(shí)間。傳遞負(fù)號(hào)的時(shí)間單位將導(dǎo)致從給定的結(jié)束日期開始計(jì)算開始日期。 CalcDuration方法是AddDuration方法的補(bǔ)充,它從給定的開始日期和結(jié)束日期計(jì)算工作時(shí)間單位(持續(xù)時(shí)間)數(shù)。
計(jì)算方法如何工作
請(qǐng)注意:指定為天、小時(shí)、分鐘或秒的工作時(shí)間單位必須與VcGantt對(duì)象的屬性TimeUnit定義的時(shí)間單位相對(duì)應(yīng)。
AddDuration方法可確保所計(jì)算的日期始終位于工作時(shí)間間隔內(nèi)。同時(shí),如果源值位于非工作時(shí)間內(nèi),則后向計(jì)算不一定提供與前向計(jì)算的源值相等的結(jié)果。
計(jì)算的有限可逆性
以交互方式創(chuàng)建或修改活動(dòng)時(shí),VARCHART XGantt會(huì)自動(dòng)注意活動(dòng)無法在非工作時(shí)間內(nèi)開始或結(jié)束。如果希望通過API創(chuàng)建或修改節(jié)點(diǎn)時(shí)行為保持一致,則需要通過手動(dòng)更正開始日期或結(jié)束日期來確保這一點(diǎn)。為此,位于非工作時(shí)間中的開始日期需要移動(dòng)到下一個(gè)工作時(shí)間間隔的開始,并且結(jié)束日期對(duì)應(yīng)于上一個(gè)工作時(shí)間間隔的結(jié)束。有一些方法可以確定間隔的極限。
示例代碼C#
if (calendar.IsWorkTime(startDate) == false) startDate = calendar.GetNextIntervalBorder(startDate); if (calendar.IsWorkTime(endDate) == false) endDate = calendar.GetStartOfInterval(endDate);
示例代碼VB.NET
If calendar.IsWorkTime(startDate) = False Then startDate = calendar.GetNextIntervalBorder(startDate) End If If calendar.IsWorkTime(endDate) = False Then endDate = calendar.GetStartOfInterval(endDate) End If
夏令時(shí)
VARCHART XGantt自動(dòng)支持夏令時(shí)。在中歐,DST從3月的最后一個(gè)星期日開始,到10月的最后一個(gè)星期日結(jié)束。在夏令時(shí)開始時(shí),時(shí)鐘從2:00 h延遲到3:00 h,在時(shí)鐘結(jié)束時(shí)從3:00 h延遲到2:00 h。
開始夏令時(shí):
夏令時(shí)結(jié)束:
如果將TimeUnit設(shè)置為小時(shí),則在夏時(shí)制的開始日期,方法calcDuration檢索23小時(shí)的時(shí)間跨度,而在最后一天,則返回25小時(shí)。如果設(shè)置為天,則兩種情況下的時(shí)間跨度均為1天。
檢索時(shí)間間隔的限制
Calendar對(duì)象用于檢索時(shí)間間隔GetStartOfInterval、GetNextIntervalBorder和GetPreviousIntervalBorder的限制的方法允許迭代工作時(shí)間間隔和非工作時(shí)間間隔。返回的結(jié)果是相對(duì)的,并以方法作為參數(shù)傳遞的參考日期為參考。
可以通過Calendar對(duì)象的IsWorkTime方法檢查日期是否在工作時(shí)間或非工作時(shí)間。盡管新間隔的開始日期等于上一個(gè)間隔的結(jié)束日期,但是開始日期始終屬于新間隔(向右打開)。
方法GetEndOfPreviousWorkTime和GetStartOfNextWorkTime不提供新的選項(xiàng),而只是簡化了工作時(shí)間間隔的處理。
在下面的編程示例中,將檢索日歷的時(shí)間間隔并將其寫入文件。此外,計(jì)算給定期間內(nèi)可用的工作時(shí)間:
示例代碼C#
void writeCalendarIntervalsToFile (string filename, VcCalendar calendar, DateTime startDate, DateTime endDate, bool listWorkIntervals, bool listNonWorkIntervals) { TextWriter tw = new StreamWriter(Path.GetDirectoryName(Application.ExecutablePath) + filename); tw.WriteLine("Time Intervals of {0} between \r\n{1} - {2}\r\n", calendar.Name, startDate, endDate); DateTime tmpStartDate = startDate; while (tmpStartDate < endDate) { DateTime nextStartDate = calendar.GetNextIntervalBorder(tmpStartDate); if (tmpStartDate == nextStartDate) nextStartDate = endDate; if (nextStartDate > endDate) nextStartDate = endDate; if (calendar.IsWorktime(tmpStartDate)) if (listWorkIntervals) tw.WriteLine("{0} - {1} WorkInterval", tmpStartDate, nextStartDate); else if (listNonWorkIntervals) tw.WriteLine("{0} - {1} NonWorkInterval", tmpStartDate, nextStartDate); tmpStartDate = nextStartDate; } int totalWorkTime = calendar.CalcDuration(startDate, endDate); tw.WriteLine("Total work time: {0} Units", totalWorkTime); tw.Close(); }
示例代碼VB.NET
Private Sub writeCalendarIntervalsToFile(ByVal filename As String, ByVal calendar As VcCalendar, ByVal startDate As DateTime, ByVal endDate As DateTime, ByVal listWorkIntervals As Boolean, ByVal listNonWorkIntervals As Boolean) Dim tw As TextWriter = New StreamWriter(Path.GetDirectoryName(Application.ExecutablePath) + filename) tw.WriteLine("Time Intervals of {0} between \r\n{1} - {2}\r\n", calendar.Name, startDate, endDate) Dim tmpStartDate As DateTime = startDate While tmpStartDate < endDate Dim nextStartDate As DateTime = calendar.GetNextIntervalBorder(tmpStartDate) if (tmpStartDate = nextStartDate) nextStartDate = endDate if (nextStartDate > endDate) nextStartDate = endDate if (calendar.IsWorktime(tmpStartDate)) if (listWorkIntervals) tw.WriteLine("{0} - {1} WorkInterval", tmpStartDate, nextStartDate) else if (listNonWorkIntervals) tw.WriteLine("{0} - {1} NonWorkInterval", tmpStartDate, nextStartDate) tmpStartDate = nextStartDate End While Dim totalWorkTime As Integer = calendar.CalcDuration(startDate,endDate) tw.WriteLine("Total work time: {0} Units", totalWorkTime) tw.Close() End Sub
請(qǐng)注意:日歷中的時(shí)間間隔可以精確地指定為秒,并且最多可以包含137年(以秒為單位)的間隔。
將時(shí)間間隔寫入文件的代碼
示例代碼C#
writeCalendarIntervalsToFile("CalenderIntervals.txt", calendar, vcGantt1.TimeScaleStart, vcGantt1.TimeScaleEnd, true, true); Time Intervals of CompanyCalendar_1 between 01.01.2011 00:00:00 - 01.01.2012 00:00:00 01.01.2011 00:00:00 - 02.01.2011 00:00:00 non-work time 02.01.2011 00:00:00 - 03.01.2011 00:00:00 non-work time 03.01.2011 00:00:00 - 03.01.2011 08:00:00 non-work time 03.01.2011 08:00:00 - 03.01.2011 12:00:00 work time 03.01.2011 12:00:00 - 03.01.2011 13:00:00 non-work time 03.01.2011 13:00:00 - 03.01.2011 17:00:00 work time 03.01.2011 17:00:00 - 04.01.2011 00:00:00 non-work time 04.01.2011 00:00:00 - 04.01.2011 08:00:00 non-work time 04.01.2011 08:00:00 - 04.01.2011 12:00:00 work time 04.01.2011 12:00:00 - 04.01.2011 13:00:00 non-work time 04.01.2011 13:00:00 - 04.01.2011 17:00:00 work time 04.01.2011 17:00:00 - 05.01.2011 00:00:00 non-work time ... 30.12.2011 00:00:00 - 30.12.2011 08:00:00 non-work time 30.12.2011 08:00:00 - 30.12.2011 12:00:00 work time 30.12.2011 12:00:00 - 30.12.2011 13:00:00 non-work time 30.12.2011 13:00:00 - 30.12.2011 17:00:00 work time 30.12.2011 17:00:00 - 31.12.2011 00:00:00 non-work time 31.12.2011 00:00:00 - 01.01.2012 00:00:00 non-work time Total work time: 2064 Units
將時(shí)間間隔寫入文件的代碼
示例代碼VB.NET
writeCalendarIntervalsToFile("CalenderIntervals.txt", calendar, vcGantt1.TimeScaleStart, vcGantt1.TimeScaleEnd, True, True) Time Intervals of CompanyCalendar_1 between 01.01.2011 00:00:00 - 01.01.2012 00:00:00 01.01.2011 00:00:00 - 02.01.2011 00:00:00 non-work time 02.01.2011 00:00:00 - 03.01.2011 00:00:00 non-work time 03.01.2011 00:00:00 - 03.01.2011 08:00:00 non-work time 03.01.2011 08:00:00 - 03.01.2011 12:00:00 work time 03.01.2011 12:00:00 - 03.01.2011 13:00:00 non-work time 03.01.2011 13:00:00 - 03.01.2011 17:00:00 work time 03.01.2011 17:00:00 - 04.01.2011 00:00:00 non-work time 04.01.2011 00:00:00 - 04.01.2011 08:00:00 non-work time 04.01.2011 08:00:00 - 04.01.2011 12:00:00 work time 04.01.2011 12:00:00 - 04.01.2011 13:00:00 non-work time 04.01.2011 13:00:00 - 04.01.2011 17:00:00 work time 04.01.2011 17:00:00 - 05.01.2011 00:00:00 non-work time ... 30.12.2011 00:00:00 - 30.12.2011 08:00:00 non-work time 30.12.2011 08:00:00 - 30.12.2011 12:00:00 work time 30.12.2011 12:00:00 - 30.12.2011 13:00:00 non-work time 30.12.2011 13:00:00 - 30.12.2011 17:00:00 work time 30.12.2011 17:00:00 - 31.12.2011 00:00:00 non-work time 31.12.2011 00:00:00 - 01.01.2012 00:00:00 non-work time Total work time: 2064 Units
本教程內(nèi)容就是這樣了,希望對(duì)您有所幫助!您可以繼續(xù)關(guān)注我們了解更多甘特圖相關(guān)文章資訊,您也可以下載VARCHART XGantt試用版體驗(yàn)一下~
相關(guān)內(nèi)容推薦:
VARCHART XGantt用戶手冊(cè)(.NET版):如何使用日歷(上)
VARCHART XGantt用戶手冊(cè)(.NET版):如何使用日歷(中)
想要購買VARCHART XGantt正版授權(quán),或了解更多產(chǎn)品信息請(qǐng)點(diǎn)擊“咨詢?cè)诰€客服”