Open In App

Zulu module in Python

Last Updated : 22 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Zulu is a drop-in-replacement for native DateTime. In this, all DateTime objects converted into UTC and also stored as UTC. There is an outline between UTC and time zones. Thus, time zones representation is applicable only for conversions to naive DateTime. It supports multiple string formatting using strptime strftime directives and Unicode date patterns.

Installation

To install this module type the below command in the terminal.

pip install zulu

Classes of Zulu module

  • zulu.Zulu: The Zulu class used to represent an immutable UTC DateTime object. Any timezone information which is given to it will be converted from timezone to UTC. If there is no timezone information is given, then it is assumed that the DateTime is a UTC value. All types of arithmetic are performed on the Fundamental UTC DateTime object. In this respect, Zulu has no concept of shifting the timezone. In spite of this, localization occurs only when a Zulu object is formatted as a string.

    Functions or classmethods

    1. now():- It returns the current UTC date and time as Zulu object .




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

    
    

    Output:

    Today date and time is: 2020-04-17T16:10:15.708064+00:00

    2. parse(obj, format=None, default_tz=None) :- It will look for an ISO8601 formatted string or a POSIX timestamp by default. While assuming UTC timezone if there is no timezone is given.It returns Zulu object parse from obj.




    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'))

    
    

    Output:

    Zulu object when timezone is passed: 2020-04-17T16:10:15.708064+00:00
    Zulu object when only date is passed: 2020-04-17T00:00:00+00:00
    Zulu object when date and time is passed: 2020-04-17T16:10:00+00:00
    Zulu object when ISO8601 is passed as formats: 2020-04-17T16:10:15.708064+00:00
    Zulu object when Default timezone is used : 2020-04-17T04:00:00+00:00

    3. format(format=None, tz=None, locale=’en_US_POSIX’) :Return string datetime using the format of string format. While converting to timezone tz first optionally.




    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'))

    
    

    Output:

    The Datetime string without timezone is: 04/17/20 16:10:15 +0000
    The Datetime string without timezone is: 04/17/20 16:10:15 +0000
    The Datetime string when timezone is given is: 2020-04-17 12:10:15-0400
    The Datetime string when timezone is given as local is: 2020-04-17 21:40:15+0530

    4. range(frame, start, end) : Range of Zulu instances is returned from start to end and in steps of the given time frame.




    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)

    
    

    Output:

    The range when time frame is in hour: [<Zulu [2020-04-17T16:10:15.708064+00:00]>,
    <Zulu [2020-04-17T17:10:15.708064+00:00]>,
    <Zulu [2020-04-17T18:10:15.708064+00:00]>,
    <Zulu [2020-04-17T19:10:15.708064+00:00]>]
    The range when time frame is in minute: [<Zulu [2020-04-17T16:10:15.708064+00:00]>,
    <Zulu [2020-04-17T16:11:15.708064+00:00]>,
    <Zulu [2020-04-17T16:12:15.708064+00:00]>,
    <Zulu [2020-04-17T16:13:15.708064+00:00]>]

    5. shift(other=None, years=0, months=0, weeks=0, days=0, hours=0, minutes=0, seconds=0, microseconds=0) :It can Shift the datetime forward or backward using a timedelta which is being created from the passing arguments and a new Zulu instance is returned.




    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)

    
    

    Output:

    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, weeks=0, days=0, hours=0, minutes=0, seconds=0, microseconds=0) :It add time using a timedelta created from the passed arguments and a new Zulu instance is returned. The first argument is either a ‘timedelta or dateutil.relativedelta object in that case other arguments are ignored and in this datetime object is added.




    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)

    
    

    Output:

    The new shifted time zone is : 2020-04-17T16:15:15.708064+00:00

    7. subtract(other=None, years=0, months=0, weeks=0, days=0, hours=0, minutes=0, seconds=0, microseconds=0):It subtract time using a timedelta created from the passed arguments and a new Zulu instance is returned. A Zulu, datetime, timedelta or dateutil.relativedelta object can be the first argument in that case other arguments are ignored and in this datetime object is subtracted.




    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)

    
    

    Output:

    The new shifted timezone is: 2020-04-17T11:10:15.708064+00:00
    The new shifted timezone using both add and subtract is: 2020-04-17T08:06:15.708064+00:00

    8. replace(year=None, month=None, day=None, hour=None, minute=None, second=None, microsecond=None, tzinfo=None, *, fold=None) :It replace the datetime attributes and a new Zulu instance is returned.




    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)

    
    

    Output:

    Replaced time is: 2020-04-23T15:10:15.708064+00:00
    The new replaced time is: 2020-04-17T16:10:11.000018+00:00

    9. copy() : It return a new ‘Zulu’ instance But with same datetime value.
    Returns
    Zulu




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

    
    

    Output:

    The copied datetime is: 2020-04-17T16:10:15.708064+00:00

    Since Zulu is immutable. Thus shift, replace, and copy return new Zulu instances while changing the original instance.

    10. span(frame, count=1) :The two new Zulu objects is returned related to the time span between this object and the time frame which is being given.By default Number of frame which is being spanned is 1.It returns a tuple (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))

    
    

    Output:

    The span of a century: (<Zulu [2000-01-01T00:00:00+00:00]>, <Zulu [2099-12-31T23:59:59.999999+00:00]>)
    The span of a month: (<Zulu [2020-04-01T00:00:00+00:00]>, <Zulu [2020-04-30T23:59:59.999999+00:00]>)
    The span of a day: (<Zulu [2020-04-17T00:00:00+00:00]>, <Zulu [2020-04-17T23:59:59.999999+00:00]>)
    The span of a decade: (<Zulu [2020-01-01T00:00:00+00:00]>, <Zulu [2029-12-31T23:59:59.999999+00:00]>)
    The span of a century with given count is: (<Zulu [2000-01-01T00:00:00+00:00]>, <Zulu [2299-12-31T23:59:59.999999+00:00]>)

    11. span_range(frame, start, end) :A range of time spans from the given start till end in the steps of given time frame is returned.




    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)

    
    

    Output:

    (<Zulu [2020-04-17T16:00:00+00:00]>, <Zulu [2020-04-17T16:59:59.999999+00:00]>)
    (<Zulu [2020-04-17T17:00:00+00:00]>, <Zulu [2020-04-17T17:59:59.999999+00:00]>)
    (<Zulu [2020-04-17T18:00:00+00:00]>, <Zulu [2020-04-17T18:59:59.999999+00:00]>)
    (<Zulu [2020-04-17T19:00:00+00:00]>, <Zulu [2020-04-17T19:59:59.999999+00:00]>)
    (<Zulu [2020-04-17T20:00:00+00:00]>, <Zulu [2020-04-17T20:59:59.999999+00:00]>)
    (<Zulu [2020-04-17T21:00:00+00:00]>, <Zulu [2020-04-17T21:59:59.999999+00:00]>)

    12. start_of(frame) : For this datetime it returns the start of the given time frame f.

    13. start_of_day() :For this datetime it return a new Zulu object/set to the start of the day .




    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())

    
    

    Output:

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

    Other start_of functions are:

    Function Name Description
    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) :For this datetime it returns the end of the given time frame f.By default Number of frame which is being spanned is 1.

    15. end_of_day(count=1) :For this datetime it return a new Zulu object/set to the end of the day .
    By default Number of frame which is being spanned is 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))

    
    

    Output:

    The end of month is: 2020-04-30T23:59:59.999999+00:00
    The end of day is without count: 2020-04-17T23:59:59.999999+00:00
    The end of day is with the count of 2: 2020-04-18T23:59:59.999999+00:00

    Other end_of functions are:

    Function Name Description
    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) : It Return the difference between this one and another given datetime in ‘time ago’ .
    Parameters

    • dtime – A datetime object.
    • Returns
      str

    17. time_from_now(**options) :It Return the difference between this one and now in “time ago” .

    18. time_to(dt, **options) :It Return the difference between this datetime and another datetime in “time to”.

    19. time_to_now(**options) :It Return the difference between this datetime and now in “time to” .




    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())

    
    

    Output:

    The difference between this time and end of the day is: 8 hours ago
    The difference between this time and end of the day is: in 8 hours
    The difference between this time and start of the day is: in 16 hours
    The difference between this time and start of the day is: 16 hours ago
    The difference is 2 days ago
    The difference is in 2 days

    20. astimezone(tz=’local’) :It return the native datetime object shifted to given timezone.By default timezone is 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)

    
    

    Output:

    Local timezone is 2020-04-17 21:40:15.708064+05:30
    Pacific timezone is: 2020-04-17 09:10:15.708064-07:00

    21. timetuple() :It Returns the time tuple.




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

    
    

    OUTPUT

    The timetuple is: time.struct_time(tm_year=2020, tm_mon=4, tm_mday=17, tm_hour=16, tm_min=10, tm_sec=15, tm_wday=4, tm_yday=108, tm_isdst=-1)

    22. utcnow() :It return the current UTC date and time.

    23. utcoffset() :It return the difference in hours, minutes and seconds from corresponding universal time for a particular place.

    24. utctimetuple() :It Return UTC time tuple coordinated with time.localtime()




    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())

    
    

    Output:

    The current UTC datetime is: 2020-04-19T07:17:30.162600+00:00
    The utcoffest is: 0:00:00
    The utctimetuple is: time.struct_time(tm_year=2020, tm_mon=4, tm_mday=17, tm_hour=16, tm_min=10, tm_sec=15, tm_wday=4, tm_yday=108, tm_isdst=0)

    Other functions in this class is :

    Function Name Description
    ctime() Return a ctime() style string.
    date() Return a date object But with same year, month and day and in same order.
    datetime Returns 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
    naive Returns 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.
  • zulu.Delta :-
    It is an extended version of datetime.timedelta that provides new functionality.

    Functions or classmethods

    1. parse_delta(obj) :It return Delta object parsed from the given obj.




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

    
    

    Output:

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

    2. format(format=’long’, granularity=’second’, threshold=0.85, add_direction=False, locale=None) : Return timedelta as a formatted string.




    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'))

    
    

    Output:

    The timedelta with given granularity is: 1 day
    The timedelta with given locale is: 5 Stunden
    The timedelta with given locale and add_direction is: dans 5 heures
    The timedelta with given threshold is: 285 minutes
    The timedelta with given threshold and granularity is: 285 minutes

    3. fromtimedelta(delta) :From a native timedelta object it return Delta object.




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

    
    

    Output:

    The timedelta is: 6:42:11
    
  • zulu.ParseError :-
    It raised Exception when an object is not able to be parsed as a datetime.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads