将数字日期转换为POSIXct R

时间:2019-09-12 16:12:18

标签: r posixct

我有一个要转换为数字的日期,然后又想转换回日期。

将日期转换为数字:

import maya.cmds as cmds
import re

global myUI
myUI = {}

# please use for def lower case for the first letter and upper case for class
def nameDialogBox(*args):
    print('prompt')

class TabSet(object):
    # don't put self here, they are implicit
    gtF0 = []
    gtF1 = []
    def __init__(self, parent=None):
        self.name = parent

    def addTab(self, name):
        nb = cmds.tabLayout(self.name, q=True, tabLabelIndex=True)
        index = len(nb) +  1

        myUI['tab{}'.format(index)] = cmds.columnLayout(p=self.name)

        cmds.tabLayout( self.name, q=True, tabLabel=True)

        cmds.tabLayout( self.name, edit=True, tabLabel=[myUI['tab{}'.format(index)], name])

        return myUI['tab{}'.format(index)]

    def listTabs(self):

        keys = [i for i in myUI.keys() if re.match('^tab\d+$', i)]
        keys = sorted(keys)
        output = [myUI[k] for k in keys]

        return output

def toolWidget(parent=str):
    wgt = cmds.rowColumnLayout(numberOfColumns=2, p=parent)
    cmds.button(l='WidgetButton1', p=wgt)
    cmds.button(l='WidgetButton2',p=wgt)
    cmds.button(l='WidgetButton3',p=wgt)
    return wgt

def testWin():

    cmds.window( widthHeight=(200, 150) )
    form = cmds.columnLayout()
    myUI['tabLayout1'] = cmds.tabLayout(innerMarginWidth=5, innerMarginHeight=5, p=form)

    myUI['tab1'] = cmds.rowColumnLayout(numberOfColumns=2, p=myUI['tabLayout1'])
    myUI['tab1_button1'] = cmds.button(p=myUI['tab1'])
    cmds.button(p=myUI['tab1'])
    cmds.button(p=myUI['tab1'])

    myUI['tab2'] = cmds.rowColumnLayout(numberOfColumns=2, p=myUI['tabLayout1'])
    cmds.button(p=myUI['tab2'])
    cmds.button(p=myUI['tab2'])
    cmds.button(p=myUI['tab2'])

    cmds.tabLayout( myUI['tabLayout1'], edit=True, tabLabel=((myUI['tab1'], 'One'), (myUI['tab2'], 'Two')) )

    cmds.showWindow()


testWin()

# create a tab
tabMan = TabSet(myUI['tabLayout1'])
new_tab = tabMan.addTab('myAddition')

# update the color
cmds.button(myUI['tab1_button1'], e=True, bgc=[.1,1,.5])

# adding a widget to the tab3 we just created
myUI['tab3_widget1'] = toolWidget(new_tab)

将数字转换为日期:

date1 = as.POSIXct('2017-12-30 15:00:00')
date1_num = as.numeric(date1)
# 1514646000

重新转换让我遗漏了什么?我希望最后一个命令返回我原来的as.Date(date1_num, origin = '1/1/1970') # "4146960-12-12"

4 个答案:

答案 0 :(得分:4)

由于数字矢量是由具有时间成分的对象创建的,因此重新转换的方式也可以相同,即首先转换为POSIXct,然后使用as.Date包装

as.Date(as.POSIXct(date1_num, origin = '1970-01-01'))
#[1] "2017-12-30"

答案 1 :(得分:3)

您可以使用anytime软件包中的anytime()anydate()

R> pt <- anytime("2017-12-30 15:00:00")
R> pt
[1] "2017-12-30 15:00:00 CST"
R>
R> anydate(pt)
[1] "2017-12-30"
R>
R> as.numeric(pt)
[1] 1514667600
R>
R> anydate(as.numeric(pt))
[1] "2017-12-30"
R> 

答案 2 :(得分:2)

val fragment = ProtocolRunningCompaqFragment.newInstance() fragment.arguments = bundle childFragmentManager .beginTransaction() .add(R.id.fragmentProtocolRunningCompaqContainer, fragment, PROTOCOL_RUNNING_COMPAQ_FRAGMENT_TAG) .commit() 计算自Unix纪元以来的秒数,而POSIXct计算天数。因此,您可以通过除以Date(让我们忽略leap秒)来恢复日期,或者改为转换回(60*60*24)

POSIXct

答案 3 :(得分:1)

使用 lubridate

lubridate::as_datetime(1514646000)
[1] "2017-12-30 15:00:00 UTC"