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