Arrow模塊生成時間

import arrow
def isLeapYear(years):
    '''
    通過判斷閏年,獲取年份years下一年的總天數
    :param years: 年份,int
    :return:days_sum,一年的總天數
    '''
    # 斷言:年份不爲整數時,拋出異常。
    assert isinstance(years, int), "請輸入整數年,如 2018"
 
    if ((years % 4 == 0 and years % 100 != 0) or (years % 400 == 0)):  # 判斷是否是閏年
        # print(years, "是閏年")
        days_sum = 366
        return days_sum
    else:
        # print(years, '不是閏年')
        days_sum = 365
        return days_sum
 
 
def getAllDayPerYear(years):
    '''
    獲取一年的所有日期
    :param years:年份
    :return:全部日期列表
    '''
    start_date = '%s-1-1' % years
    a = 0
    all_date_list = []
    days_sum = isLeapYear(int(years))
    print()
    while a < days_sum:
        b = arrow.get(start_date).shift(days=a).format("YYYY-MM-DD")
        a += 1
        all_date_list.append(b)
    # print(all_date_list)
    return all_date_list
 
 
if __name__ == '__main__':
    # years = "2001"
    # years = int(years)
    # # 通過判斷閏年,獲取一年的總天數
    # days_sum = isLeapYear(years)
 
    # 獲取一年的所有日期
    all_date_list = getAllDayPerYear(years)
tzinfo

獲取tzinfo的的Arrow對象。

>>> arw=arrow.utcnow()
>>> arw.tzinfo
tzutc()
datetime

返回Arrow對象的日期時間表示形式

>>> arw=arrow.utcnow()
>>> arw.datetime
datetime.datetime(2019, 1, 24, 16, 35, 27, 276649, tzinfo=tzutc())
naive

返回Arrow 對象的簡單日期時間表示

>>> nairobi = arrow.now('Africa/Nairobi')
>>> nairobi
<Arrow [2019-01-23T19:27:12.297999+03:00]>
>>> nairobi.naive
datetime.datetime(2019, 1, 23, 19, 27, 12, 297999)
timestamp

Arrow以UTC時間形式返回對象的時間戳表示形式

>>> arrow.utcnow().timestamp
1548260567
float_timestamp

Arrow 以UTC時間形式返回對象的浮點表示形式

>>> arrow.utcnow().float_timestamp
1548260516.830896
clone()

返回Arrow從當前對象克隆的新對象。

>>> arw = arrow.utcnow()
>>> cloned = arw.clone()
replace(**kwargs)

返回具有Arrow根據輸入更新的屬性的新對象。

使用屬性名稱絕對設置其值:

>>> import arrow
>>> arw = arrow.utcnow()
>>> arw
<Arrow [2013-05-11T22:27:34.787885+00:00]>
>>> arw.replace(year=2014, month=6)
<Arrow [2014-06-11T22:27:34.787885+00:00]>

您還可以使用時區表達式替換時區而不進行轉換 

>>> arw.replace(tzinfo=tz.tzlocal())
<Arrow [2013-05-11T22:27:34.787885-07:00]>
shift(**kwargs)

返回具有Arrow根據輸入更新的屬性的新對象。

使用多個屬性名稱相對地移動它們的當前值:

>>> import arrow
>>> arw = arrow.utcnow()
>>> arw
<Arrow [2013-05-11T22:27:34.787885+00:00]>
>>> arw.shift(years=1, months=-1)
<Arrow [2014-04-11T22:27:34.787885+00:00]>

星期幾相對移位可以使用Python的工作日數字(星期一= 0,星期二= 1 ...星期日= 6)或使用dateutil.relativedelta的日期實例(MO,TU ... SU)。使用工作日數字時,返回的日期將始終大於或等於開始日期。

使用上面的代碼(這是一個星期六)並要求它轉移到星期六:

>>> arw.shift(weekday=5)
<Arrow [2013-05-11T22:27:34.787885+00:00]>

在要求週一時:

>>> arw.shift(weekday=0)
<Arrow [2013-05-13T22:27:34.787885+00:00]>
to(tz)

返回一個Arrow轉換爲目標時區的新對象。


>>>
utc = arrow.utcnow()Usage:

>>> utc
<Arrow [2013-05-09T03:49:12.311072+00:00]>

>>> utc.to('US/Pacific')
<Arrow [2013-05-08T20:49:12.311072-07:00]>

>>> utc.to(tz.tzlocal())
<Arrow [2013-05-08T20:49:12.311072-07:00]>

>>> utc.to('-07:00')
<Arrow [2013-05-08T20:49:12.311072-07:00]>

>>> utc.to('local')
<Arrow [2013-05-08T20:49:12.311072-07:00]>

>>> utc.to('local').to('utc')
<Arrow [2013-05-09T03:49:12.311072+00:00]>
span(framecount=1)

返回兩個新Arrow對象,表示Arrow給定時間範圍內對象的時間跨度

支持的幀值:年,季度,月,周,日,小時,分鐘,秒。

>>> arrow.utcnow()
<Arrow [2013-05-09T03:32:36.186203+00:00]>

>>> arrow.utcnow().span('hour')
(<Arrow [2013-05-09T03:00:00+00:00]>, <Arrow [2013-05-09T03:59:59.999999+00:00]>)

>>> arrow.utcnow().span('day')
(<Arrow [2013-05-09T00:00:00+00:00]>, <Arrow [2013-05-09T23:59:59.999999+00:00]>)

>>> arrow.utcnow().span('day', count=2)
(<Arrow [2013-05-09T00:00:00+00:00]>, <Arrow [2013-05-10T23:59:59.999999+00:00]>)
floor(frame)

返回一個新Arrow對象,表示Arrow給定時間範圍內對象的時間跨度的“下限” 相當於返回的2元組中的第一個元素 span


>>>
arrow.utcnow().floor('hour')Usage:

<Arrow [2013-05-09T03:00:00+00:00]>
ceil(frame)

返回一個新Arrow對象,表示Arrow給定時間範圍內對象的時間跨度的“上限” 相當於返回的2元組中的第二個元素 span


>>>
arrow.utcnow().ceil('hour')Usage:

<Arrow [2013-05-09T03:59:59.999999+00:00]>
format(fmt='YYYY-MM-DD HH:mm:ssZZ'locale='en_us')

返回Arrow對象的字符串表示形式,根據格式字符串進行格式化。


>>>
arrow.utcnow().format('YYYY-MM-DD HH:mm:ss ZZ')Usage:

'2013-05-09 03:56:47 -00:00'

>>> arrow.utcnow().format('X')
'1368071882'

>>> arrow.utcnow().format('MMMM DD, YYYY')
'May 09, 2013'

>>> arrow.utcnow().format()
'2013-05-09 03:56:47 -00:00'
humanize(other=Nonelocale='en_us'only_distance=Falsegranularity='auto')

返回相對時間差異的本地化人性化表示。


>>>
earlier = arrow.utcnow().shift(hours=-2)Usage:

>>> earlier.humanize()
'2 hours ago'

>>> later = earlier.shift(hours=4)
>>> later.humanize(earlier)
'in 4 hours'
date()

返回date具有相同年,月和日的對象。

 

>>> arrow.utcnow().date()
datetime.date(2019, 1, 23)
time()

返回time具有相同小時,分鐘,秒,微秒的對象。

 

>>> arrow.utcnow().time()
datetime.time(12, 15, 34, 68352)
timetz()

返回time具有相同小時,分鐘,秒,微秒和tzinfo的對象。

 

>>> arrow.utcnow().timetz()
datetime.time(12, 5, 18, 298893, tzinfo=tzutc())
astimezone(tz)

返回一個datetime轉換爲指定時區對象。


>>>
pacific=arrow.now('US/Pacific')Usage:

>>> nyc=arrow.now('America/New_York').tzinfo
>>> pacific.astimezone(nyc)
datetime.datetime(2019, 1, 20, 10, 24, 22, 328172, tzinfo=tzfile('/usr/share/zoneinfo/America/New_York'))
utcoffset()

返回timedelta表示與UTC時間的整數分鐘差異對象。

 

>>> arrow.now('US/Pacific').utcoffset()
datetime.timedelta(-1, 57600)
dst()

返回夏令時調整。

 

>>> arrow.utcnow().dst()
datetime.timedelta(0)
timetuple()

在當前時區中返回time.struct_time

 

>>> arrow.utcnow().timetuple()
time.struct_time(tm_year=2019, tm_mon=1, tm_mday=20, tm_hour=15, tm_min=17, tm_sec=8, tm_wday=6, tm_yday=20, tm_isdst=0)
utctimetuple()

以UTC時間返回 time.struct_time

 

>>> arrow.utcnow().utctimetuple()
time.struct_time(tm_year=2019, tm_mon=1, tm_mday=19, tm_hour=21, tm_min=41, tm_sec=7, tm_wday=5, tm_yday=19, tm_isdst=0)
toordinal()

返回日期的公曆格里高利序數。

 

>>> arrow.utcnow().toordinal()
737078
weekday()

以整數(0-6)返回星期幾。

>>> arrow.utcnow().weekday()
5
isoweekday()

以整數(1-7)返回一週的ISO日期。

>>> arrow.utcnow().isoweekday()
6
isocalendar()

返回3元組(ISO年份,ISO週數,ISO工作日)。

>>> arrow.utcnow().isocalendar()
(2019, 3, 6)
isoformat(sep='T')

返回ISO 8601格式的日期和時間表示。

>>> arrow.utcnow().isoformat()
'2019-01-19T18:30:52.442118+00:00'
ctime()

返回日期和時間的ctime格式表示。

>>> arrow.utcnow().ctime()
'Sat Jan 19 18:26:50 2019'
strftime(format)

datetime.strftime的格式


>>>
arrow.utcnow().strftime('%d-%m-%Y %H:%M:%S')Usage:

'23-01-2019 12:28:17'
for_json()

序列for_json化爲simplejson 協議。

>>> arrow.utcnow().for_json()
'2019-01-19T18:25:36.760079+00:00'
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章