Python で unixtime から JavaScript の Data オブジェクト形式の文字列に変換

usage

>>> unixtime_to_javascriptdatetime(1403481600)
'Date(2014,5,23,00,00,00)'
>>> 
>>> unixtime_to_javascriptdatetime(1402272000)
'Date(2014,5,09,00,00,00)'

 

definition

def unixtime_to_javascriptdatetime(unixtime):                                   
  python_datetime     = unixtime_to_pythondatetime(unixtime)                    
  javascript_datetime = pythondatetime_to_javascriptdatetime(python_datetime)   
  return javascript_datetime                                                    
                                                                                
def unixtime_to_pythondatetime(unix_time):
  return datetime.datetime.fromtimestamp(unix_time)                             
                                                                                
def pythondatetime_to_javascriptdatetime(python_datetime):
  s                   = re.search("(^Date\([0-9]{4},)([[0-9]{2}|[0-9]{1}])(.*\))", python_datetime.strftime('Date(%Y,%m,%d,%H,%M,%S)'))
  javascript_datetime = s.group(1) + str(int(s.group(2))-1) + s.group(3)        
  return javascript_datetime