📜  Python中的祖鲁模块

📅  最后修改于: 2022-05-13 01:54:52.957000             🧑  作者: Mango

Python中的祖鲁模块

Zulu是原生 DateTime 的直接替代品。在此,所有 DateTime 对象都转换为 UTC 并存储为 UTC。 UTC 和时区之间有一个大纲。因此,时区表示仅适用于转换为原始 DateTime。它支持使用 strptime strftime 指令和 Unicode 日期模式的多种字符串格式。

安装

要安装此模块,请在终端中键入以下命令。

pip install zulu

Zulu 模块的类

  • zulu.Zulu:用于表示不可变 UTC DateTime 对象的 Zulu 类。提供给它的任何时区信息都将从时区转换为 UTC。如果没有给出时区信息,则假定 DateTime 是 UTC 值。所有类型的算术都在 Fundamental UTC DateTime 对象上执行。在这方面,祖鲁没有改变时区的概念。尽管如此,本地化仅在 Zulu 对象被格式化为字符串时才会发生。

    函数或类方法

    1. now() :- 它以 Zulu 对象的形式返回当前的 UTC 日期和时间。

    import zulu
      
      
    dt = zulu.now()
    print("Today date and time is:", dt)
    

    输出:

    2. parse(obj, format=None, default_tz=None) :-默认情况下,它将查找 ISO8601 格式的字符串或 POSIX 时间戳。如果没有给出时区,则假设 UTC 时区。它从 obj 返回 Zulu 对象解析。

    import zulu
      
      
    print("Zulu object when timezone is passed:",
          zulu.parse('2020-04-17T16:10:15.708064+00:00'))
      
    print("Zulu object when only date is passed:", 
          zulu.parse('2020-04-17'))
      
    print("Zulu object when date and time is passed:",
          zulu.parse('2020-04-17 16:10'))
      
    print("Zulu object when ISO8601 is passed as formats:",
          zulu.parse('2020-04-17T16:10:15.708064+00:00',
                     zulu.ISO8601))
      
    print("Zulu object when Default timezone is used :", 
          zulu.parse('2020-04-17', 
                     default_tz ='US/Eastern'))
    

    输出:

    3. format(format=None, tz=None, locale='en_US_POSIX') :使用字符串格式的格式返回字符串日期时间。在可选地首先转换为时区 tz 时。

    import zulu
      
      
    dt = zulu.parse('2020-04-17T16:10:15.708064+00:00')
      
    print("The Datetime string without timezone is:", 
          dt.format('% m/% d/% y % H:% M:% S % z'))
      
    print("The Datetime string without timezone is:", 
          dt.format('MM / dd / YY HH:mm:ss Z'))
      
    print("The Datetime string when timezone is given  is:",
          dt.format('% Y-% m-% d % H:% M:% S % z', 
                    tz ='US/Eastern'))
      
    print("The Datetime string when timezone is given as local is:", 
          dt.format('%Y-%m-%d %H:%M:%S %z', 
                    tz ='local'))
    

    输出:

    4. range(frame, start, end) : Zulu 实例的范围从开始到结束并以给定时间范围的步长返回。

    import zulu
      
      
    dt = zulu.parse('2020-04-17T16:10:15.708064+00:00')
      
    range1 = list(zulu.range('hour', dt, 
                             dt.shift(hours = 4)))
    range2 = list(zulu.range('minute', dt, 
                             dt.shift(minutes = 4)))
      
    print("The range when time frame is in hour:", 
          range1)
    print("The range when time frame is in minute:",
          range2)
    

    输出:

    5. shift(other=None, years=0, months=0, week=0, days=0, hours=0, minutes=0, seconds=0, microseconds=0) :它可以使用向前或向后移动日期时间从传递的参数创建的 timedelta 并返回一个新的 Zulu 实例。

    import zulu
      
      
    dt = zulu.parse('2020-04-17T16:10:15.708064+00:00')
    shifted1 = dt.shift(hours =-5, minutes = 10)
      
    print("The shifted time is:", shifted1)
      
    shifted2 = dt.shift(minutes = 55, seconds = 11,
                        microseconds = 10)
    print("The new shifted time is:", shifted2)
    

    输出:

    The shifted time is: 2020-04-17T11:20:15.708064+00:00
    The new shifted time is: 2020-04-17T17:05:26.708074+00:00
    

    6. add(other=None, years=0, months=0, week=0, days=0, hours=0, minutes=0, seconds=0, microseconds=0) :它使用从传递参数并返回一个新的 Zulu 实例。第一个参数是 'timedelta 或 dateutil.relativedelta 对象,在这种情况下,其他参数将被忽略,并在此 datetime 对象中添加。

    import zulu
      
      
    dt = zulu.parse('2020-04-17T16:10:15.708064+00:00')
      
    shifted = dt.add(minutes = 5)
    print("The new shifted time zone is :", shifted)
    

    输出:

    7.减法(其他=无,年=0,月=0,周=0,天=0,小时=0,分钟=0,秒=0,微秒=0):它使用从传递参数并返回一个新的 Zulu 实例。 Zulu、datetime、timedelta 或 dateutil.relativedelta 对象可以是第一个参数,在这种情况下,其他参数将被忽略,并且在此 datetime 对象中被减去。

    import zulu
      
      
    dt = zulu.parse('2020-04-17T16:10:15.708064+00:00')
    shifted1 = dt.subtract(hours = 5)
    shifted2 = dt.subtract(hours = 9).add(minutes = 56)
      
    print("The new shifted timezone is:", shifted1)
    print("The new shifted timezone using both add\
     and subtract is:", shifted2)
    

    输出:

    8. replace(year=None, month=None, day=None, hour=None, minute=None, second=None, microsecond=None, tzinfo=None, *, fold=None) :替换日期时间属性和返回新的 Zulu 实例。

    import zulu
      
      
    dt = zulu.parse('2020-04-17T16:10:15.708064+00:00')
      
    replaced1 = dt.replace(day = 23, hour = 15)
    print("Replaced time is:", replaced1)
      
    replaced2 = dt.replace(minute = 10, second = 11,
                           microsecond = 18)
    print("The new replaced time is:", replaced2)
    

    输出:

    9. copy() :它返回一个新的“Zulu”实例,但具有相同的日期时间值。
    退货
    祖鲁语

    import zulu
      
      
    dt = zulu.parse('2020-04-17T16:10:15.708064 + 00:00')
    print("The copied datetime is:", dt.copy())
    

    输出:

    因为祖鲁语是不可变的。因此,移位、替换和复制会在更改原始实例的同时返回新的 Zulu 实例。

    10. span(frame, count=1) :返回两个新的 Zulu 对象,与此对象和给定时间帧之间的时间跨度有关。默认情况下,正在跨越的帧数为 1。它返回一个元组(start_of_frame,end_of_frame)。

    import zulu
      
      
    dt = zulu.parse('2020-04-17T16:10:15.708064 + 00:00')
      
    print("The span of a century:", dt.span('century'))
    print("The span of a month:", dt.span('month'))
    print("The span of a day:", dt.span('day'))
    print("The span of a decade:", dt.span('decade'))
    print("The span of a century with given count is:",
           dt.span('century', count = 3))
    

    输出:

    11. span_range(frame, start, end) :返回给定时间范围的步骤中从给定开始到结束的时间范围。

    import zulu
      
      
    start = zulu.parse('2020-04-17T16:10:15.708064+00:00')
    end = zulu.parse('2020-04-17T22:10:15.708064+00:00')
      
    for span in zulu.span_range('hour', start, end):
        print(span)
    

    输出:

    12. start_of(frame) :对于这个日期时间,它返回给定时间框架 f 的开始。

    13. start_of_day() :对于这个日期时间,它返回一个新的 Zulu 对象/设置为一天的开始。

    import zulu
      
      
    dt = zulu.parse('2020-04-17T16:10:15.708064 + 00:00')
    print("The start of month is:", dt.start_of('month'))
    print("The start of day is:", dt.start_of_day())
    

    输出:

    The start of month is: 2020-04-01T00:00:00+00:00
    The start of day is: 2020-04-17T00:00:00+00:00
    

    其他 start_of 函数是:

    Function NameDescription
    start_of_century()Return a new Zulu set for this datetime to the start of the century.
    start_of_decade()Return a new Zulu set for this datetime to the start of the decade.
    start_of_hour()Return a new Zulu set for this datetime to the start of the hour.
    start_of_minute()Return a new Zulu set for this datetime to the start of the minute.
    start_of_month()Return a new Zulu set for this datetime to the start of the month.
    start_of_second()Return a new Zulu set for this datetime to the start of the second.
    start_of_year()Return a new Zulu set for this datetime to the start of the year.

    14. end_of(frame, count=1) :对于这个日期时间,它返回给定时间帧 f 的结束。默认情况下,正在跨越的帧数为 1。

    15. end_of_day(count=1) :对于这个日期时间,它返回一个新的 Zulu 对象/设置为一天结束。
    默认情况下,正在跨越的帧数为 1。

    import zulu
      
      
    dt = zulu.parse('2020-04-17T16:10:15.708064+00:00')
      
    print("The end of month is:", dt.end_of('month', 1))
    print("The end of day is without count:", dt.end_of_day())
    print("The end of day is with the count of 2:", dt.end_of_day(2))
    

    输出:

    其他 end_of 函数是:

    Function NameDescription
    end_of_century(count=1)Return a new Zulu set for this datetime to the end of the century.
    end_of_decade(count=1)Return a new Zulu set for this datetime to the end of the decade.
    end_of_hour(count=1)Return a new Zulu set for this datetime to the end of the hour.
    end_of_minute(count=1)Return a new Zulu set for this datetime to the end of the minute.
    end_of_month(count=1)Return a new Zulu set for this datetime to the end of the month.
    end_of_second(count=1)Return a new Zulu set for this datetime to the end of the second.
    end_of_year(count=1)Return a new Zulu set for this datetime to the end of the year.

    16. time_from(dt, **options) :它返回'time ago' 中的这个和另一个给定日期时间之间的差异。
    参数

    • dtime -- 一个日期时间对象。

      退货
      字符串

    17. time_from_now(**options) :它返回“time ago”中这个和现在之间的差异。

    18. time_to(dt, **options) :它返回这个日期时间和另一个日期时间在“时间到”之间的差异。

    19. time_to_now(**options) :它返回这个日期时间和现在在“时间到”之间的差异。

    import zulu
      
      
    dt = zulu.parse('2020-04-17T16:10:15.708064+00:00')
      
    print("The difference between this time and end of the day is:",
          dt.time_from(dt.end_of_day()))
    print("The difference between this time and end of the day is:",
          dt.time_to(dt.end_of_day()))
    print("The difference between this time and start of the day is:",
          dt.time_from(dt.start_of_day()))
    print("The difference between this time and start of the day is:",
          dt.time_to(dt.start_of_day()))
      
    print("The difference is", dt.time_from_now())
    print("The difference is", dt.time_to_now())
    

    输出:

    20. astimezone(tz='local') :它返回转移到给定时区的本地日期时间对象。默认时区是本地的。

    import zulu
      
      
    dt = zulu.parse('2020-04-17T16:10:15.708064 + 00:00')
    local = dt.astimezone()
      
    print("Local timezone is", local)
      
    pacific = dt.astimezone('US / Pacific')
    print("Pacific timezone is:", pacific)
    

    输出:

    21. timetuple() :它返回时间元组。

    import zulu
      
      
    dt = zulu.parse('2020-04-17T16:10:15.708064 + 00:00')
    print("The timetuple is:", dt.timetuple())
    

    输出

    22. utcnow() :返回当前的UTC日期和时间。

    23. utcoffset() :它返回特定地点与相应世界时的小时、分钟和秒的差异。

    24. utctimetuple() :它返回与 time.localtime() 协调的 UTC 时间元组

    import zulu
      
      
    dt = zulu.parse('2020-04-17T16:10:15.708064 + 00:00')
      
    print("The current UTC datetime is:", dt.utcnow())
    print("The utcoffest is:", dt.utcoffset())
    print("The utctimetuple is:", dt.utctimetuple())
    

    输出:

    此类中的其他功能是:

    Function NameDescription
    ctime()Return a ctime() style string.
    date()Return a date object But with same year, month and day and in same order.
    datetimeReturns a native datetime object.
    datetimetuple()Return a datetime tuple which contain year, month, day, hour, minute, second, microsecond, tzoneinfo.
    datetuple()Return a date tuple which contain year, month, day.
    days_in_month()Return the total number of days in the month
    dst()Return daylight saving time
    is_after(other)Return whether this datetime is after other or not
    is_before(other)Return whether this datetime is before other or not
    is_between(start, end)Return whether this datetime is between start and end inclusively or not
    is_leap_year()Return whether this datetime’s year is a leap year or not.
    is_on_or_after(other)Return whether this datetime is on or after other
    is_on_or_before(other)Return whether this datetime is on or before other
    isocalendar()Return a 3-tuple which contain ISO year, week number, and weekday
    isoformat()Return a string in ISO 8601 format i.e.. YYYY-MM-DDTHH:MM:SS[.mmmmmm][+HH:MM].
    isoweekday()Return the day of the week represented by the date E.g. Monday == 1, Tuesday == 2. . . Sunday ==7
    naiveReturns a native datetime object.
    strftime()Return format – strftime() style string.
    strptime()Return string, format – new datetime which is parsed from a string.
    time()Return a time object with the same time but with timezoneinfo=None
    timetz()Return a time object but with same time and timezoneinfo.
    today()Return current date or datetime.
    toordinal()Return proleptic Julian/Gregorian ordinal.
    tzname()Return the name of timezone associated with the datetime object.
    utcfromtimestamp(timestamp)Return a Zulu object from a POSIX timestamp.
    weekday()Return the day of the week represented by the date. Monday == 0, Tuesday == 1. . . Sunday ==6
    fromdatetime(dt)Return a Zulu object from a native datetime object.
    fromgmtime(struct)Return a Zulu object from a tuple like that returned by time.gmtime which represents a UTC datetime.
    fromlocaltime(struct)Return a Zulu object from a tuple like that returned by time.localtime which represents a local datetime.
    fromordinal(ordinal)Return Zulu object from a proleptic Julian/Gregorian ordinal.
    fromtimestamp(timestamp, tz=tzutc())Return Zulu object from a POSIX timestamp.
  • 祖鲁三角洲:-
    它是提供新功能的 datetime.timedelta 的扩展版本。

    函数或类方法

    1. parse_delta(obj) :它返回从给定obj解析的Delta对象。

    import zulu
      
      
    delta1 = zulu.parse_delta('4h 45m')
    delta2 = zulu.parse_delta('-4h 45m')
      
    print("The delta is:", delta1)
    print("The delta is:", delta2)
    

    输出:

    The delta is: 4:45:00
    The delta is: -1 day, 19:15:00
    

    2. format(format='long', grainity='second', threshold=0.85, add_direction=False, locale=None) :将timedelta作为格式化字符串返回。

    import zulu
      
      
    delta = zulu.parse_delta('4h 45m')
      
    print("The timedelta with given granularity is:", 
          delta.format(granularity ='day'))
      
    print("The timedelta with given locale is:",
          delta.format(locale ='de'))
      
    print("The timedelta with given locale and add_direction is:",
          delta.format(locale ='fr', add_direction = True))
      
    print("The timedelta with given threshold is:", 
          delta.format(threshold = 5))
      
    print("The timedelta with given threshold and granularity is:", 
          delta.format(threshold = 155, granularity ='minute'))
    

    输出:

    3. fromtimedelta(delta) :从本地 timedelta 对象返回 Delta 对象。

    import zulu
      
      
    delta = zulu.parse_delta('4h 45m')
    delta1 = zulu.parse_delta('6h 42m 11s')
      
    print("The timedelta is:", delta.fromtimedelta(delta1))
    

    输出:

    The timedelta is: 6:42:11
    
  • zulu.ParseError :-
    当无法将对象解析为日期时间时,它会引发异常。