2013-10-18 60 views
-3

請幫助我,我正在嘗試編寫一個PYTHON代碼,它使用名爲calcExcelDate的已定義函數來實現某些給定日期的excel日期等效方程:使用已定義的函數來實現某些方程式

  • year(4位整數從1900到9999);
  • month(從1到12的1或2位整數);
  • 天(從1到31的1或2位整數,取決於月份)。

我定義了函數,並使用了3個循環來指定上述給定的年,月和日的範圍。但是,如何才能使用該功能來實現follwoing公式:

y_2 = year - 1900 
em = math.floor((14 - month)/12) 
y_3 = y_2 - em 
m_2 = month + 12 * em 
I_ = 1 + min(y_3, 0) + math.floor(y_3/4) - math.floor(y_3/100) + math.floor((y_3 + 300)/400) 
d_1 = math.floor(-1.63 + (m_2 - 1) * 30.6) 
d_2 = day + y_3 * 365 + I_ + d_1 

我迄今爲止代碼:

import time 
import math 


DAYS = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday") 
FULL_MONTHS = ("January", "February", "March", "April", "May", "June", 
"July", "August", "September", "October", "November", "December") 


mm_ = tuple(str(date) for date in range(2013-10-17, 2100-01-01)) # a tuple of character strings containing dates in the date range Excel supports, 
                    # such as ("2013-Oct-17", "2100-01-01") 
print mm_ 


def calcExcelDate(year, month, day): 

    for year in range(1900, 10000): 
    for month in range(1, 13): 
     for day in range(1, 32): 
     y_2 = year - 1900 
     em = math.floor((14 - month)/12) 
     y_3 = y_2 - em 
     m_2 = month + 12 * em 
     I_ = 1 + min(y_3, 0) + math.floor(y_3/4) - math.floor(y_3/100) 
          + math.floor((y_3 + 300)/400) 
     d_1 = math.floor(-1.63 + (m_2 - 1) * 30.6) 
     d_2 = day + y_3 * 365 + I_ + d_1 


xx_ = calcExcelDate(2010, 10, 1) 
print xx_ 
+1

到底在期待什麼從'calcExcelDate輸出(2010 ,10,1)'?我不認爲你需要循環所有'year' /'month' /'day'(尤其是因爲它只是覆蓋了你傳入函數的'year' /'month' /''日)。所以刪除三個for循環可能是第一步,只是離開方程組,但我不知道你想要返回什麼。 'd_2'包含你想要的最終答案嗎? –

+0

同意。 「功能」一詞通常意味着輸入和輸出之間的某種關係。它看起來像輸入將是某個日期。你想要的輸出是什麼?有什麼具體的例子**你已經手工檢查你的代碼是否正在計算你期望的值? – dyoo

回答

0

這樣的事情最好還是使用標準庫datetime作爲接口您希望處理的日期和時間,然後根據需要將它們轉換爲Excel序列日期。

以下代碼顯示如何將datetime日期轉換爲XlsxWriter模塊中的Excel日期。

def _convert_date_time(self, dt_obj): 
    # We handle datetime .datetime, .date and .time objects but convert 
    # them to datetime.datetime objects and process them in the same way. 
    if isinstance(dt_obj, datetime.datetime): 
     pass 
    elif isinstance(dt_obj, datetime.date): 
     dt_obj = datetime.datetime.fromordinal(dt_obj.toordinal()) 
    elif isinstance(dt_obj, datetime.time): 
     dt_obj = datetime.datetime.combine(self.epoch, dt_obj) 
    else: 
     raise TypeError("Unknown or unsupported datetime type") 

    # Convert a Python datetime.datetime value to an Excel date number. 
    delta = dt_obj - self.epoch 
    excel_time = (delta.days 
        + (float(delta.seconds) 
        + float(delta.microseconds)/1E6) 
       /(60 * 60 * 24)) 

    # Special case for datetime where time only has been specified and 
    # the default date of 1900-01-01 is used. 
    if dt_obj.isocalendar() == (1900, 1, 1): 
     excel_time -= 1 

    # Account for Excel erroneously treating 1900 as a leap year. 
    if not self.date_1904 and excel_time > 59: 
     excel_time += 1 

    return excel_time 

使用datetime的好處是,你必須用各種方法來解析字符串轉換爲日期或時間的標準接口:

my_datetime = datetime.datetime.strptime('2013-01-23', '%Y-%m-%d') 
相關問題