While trying the new zoneinfo support in python3.9.1, I noticed that time differences of datetime aware objects differ from those produced by pytz as shown by the output of the below program:
import datetime,zoneinfo,pytz
from sys import version_info
print(f'Python{version_info.major}.{version_info.minor}{version_info.micro}'
f' pytz{pytz.__version__}')
Athens=zoneinfo.ZoneInfo('Europe/Athens')
f='%Y-%m-%d %H:%M:%S'
d=[datetime.datetime.strptime('2020-10-25 00:00:00',f),
datetime.datetime.strptime('2020-10-25 23:59:59',f)]
print('naive ',d[1]-d[0])
d=[x.astimezone(Athens) for x in d]
print('zoneinfo',d[1]-d[0])
d=[datetime.datetime.strptime('2020-10-25 00:00:00',f),
datetime.datetime.strptime('2020-10-25 23:59:59',f)]
athens=pytz.timezone('Europe/Athens')
print('pytz as ',d[1].astimezone(athens)-d[0].astimezone(athens))
print('pytz loc',athens.localize(d[1])-athens.localize(d[0]))
Python3.91 pytz2020.4
naive 23:59:59
zoneinfo 23:59:59
pytz as 1 day, 0:59:59
pytz loc 1 day, 0:59:59
It appears that the native timezone supports ignores the fact that 2020-10-25 was the day of changeover from summertime to winter time and therefore that day's duration was 25 hours.
What am I missing?
An illustration of my comment; aware datetime with tzinfo set with a ZoneInfo from zoneinfo returns a wall time timedelta. If you do the same with pytz.timezone aware datetime, you get absolute time timedelta.
from datetime import datetime
from zoneinfo import ZoneInfo
import pytz
from sys import version_info
print(f'Python {version_info.major}.{version_info.minor}{version_info.micro} pytz {pytz.__version__}')
# Python 3.90 pytz 2020.4
d=[datetime.fromisoformat('2020-10-25 00:00:00'), datetime.fromisoformat('2020-10-25 23:59:59')]
Athens = ZoneInfo('Europe/Athens')
print('wall time diff, zoneinfo:', d[1].replace(tzinfo=Athens)-d[0].replace(tzinfo=Athens))
# wall time diff, zoneinfo: 23:59:59
athens = pytz.timezone('Europe/Athens')
print('absolute time diff, pytz:', athens.localize(d[1])-athens.localize(d[0]))
# absolute time diff, pytz: 1 day, 0:59:59
# to get absolute time delta with zoneinfo:
utc = ZoneInfo('UTC')
print('absolute time diff, zoneinfo:', d[1].replace(tzinfo=Athens).astimezone(utc)
-d[0].replace(tzinfo=Athens).astimezone(utc))
# absolute time diff, zoneinfo: 1 day, 0:59:59
Related
How to get the current time and 15 min ago time in iso 8601 format (YYYY-MM-DDTHH:mm:ss) in groovy?
You can use java time's Instant and the toString() format
import java.time.*
def now = Instant.now()
def fifteenAgo = now.minus(Duration.ofMinutes(15))
println "Now is ${now} and 15 mins ago was ${fifteenAgo}"
Prints:
Now is 2020-06-30T19:53:17.445039Z and 15 mins ago was 2020-06-30T19:38:17.445039Z
You can formast the date in any way you want in Groovy, by doing e.g.
println new Date().format("yyyy-MM-dd HH.mm.ss.SSSSS Z")
Then, you can do calculations on the date, like this:
new Date(System.currentTimeMillis()-91*60*1000)
which will minus 91 minutes (91min * 60sec * 1000ms).
Then you can put the statements together, which is why Groovy is great:
def a = new Date(System.currentTimeMillis()-91*60*1000).format("YYYY-MM-DD")
And so you can get the half before the T. And the half after the T:
def b = new Date(System.currentTimeMillis()-91*60*1000).format("HH:mm:ss")
And then concatenate them with a T:
println "91 minutes ago in iso 8601 format is: ${a}T${b}"
There are other ways of doing it, like with TimeCategory.minus, but this is a good illustration. I used 91 minutes, but you can adapt it to your own requirtement.
What's the difference between US/Mountain and AZ timezone. Why is it adding an extra 28 min?
>>> strtime = datetime.datetime.strptime('10:00pm', '%I:%M%p')
>>> tz = timezone('US/Mountain').localize(strtime)
>>> print tz
1900-01-01 22:00:00-07:00
>>> tz = timezone(us.states.lookup('AZ').capital_tz).localize(strtime)
>>> print tz
1900-01-01 22:00:00-07:28 <<-----
this is most likely due to the fact that your year is 1900 (see also this question); it works fine if you add a current year:
import datetime
from pytz import timezone
import us
strtime = datetime.datetime.strptime('2020 10:00pm', '%Y %I:%M%p')
tz = timezone('US/Mountain').localize(strtime)
print(tz)
# 2020-01-01 22:00:00-07:00
tz = timezone(us.states.lookup('AZ').capital_tz).localize(strtime)
print(tz)
# 2020-01-01 22:00:00-07:00
(I'm using Python3 but that shouldn't make a difference, I get the same 28 min offset for year 1900)
I'm trying to use liner_profiler library in jupyter notebook with cython function. It is working only halfway. The result I get only consist of first row of the function and no profiling results.
%%cython -a
# cython: linetrace=True
# cython: binding=True
# distutils: define_macros=CYTHON_TRACE_NOGIL=1
import numpy as np
cimport numpy as np
from datetime import datetime
import math
cpdef np.int64_t get_days(np.int64_t year, np.int64_t month):
cdef np.ndarray months=np.array([31,28,31,30,31,30,31,31,30,31,30,31])
if month==2:
if (year%4==0 and year%100!=0) or (year%400==0):
return 29
return months[month-1]
For the profiling result int onlt shows one line of code
Timer unit: 1e-07 s
Total time: 0.0015096 s
File: .ipython\cython\_cython_magic_0154a9feed9bbd6e4f23e57d73acf50f.pyx
Function: get_days at line 15
Line # Hits Time Per Hit % Time Line Contents
==============================================================
15 cpdef np.int64_t get_days(np.int64_t year, np.int64_t month):
This can be seen as a bug in the line_profiler (if it is supposed to support Cython). To get the code of the profiled function, line_profiler reads the pyx-file and tries to extract the code with help of inspect.getblock:
...
# read pyx-file
all_lines = linecache.getlines(filename)
# try to extract body of the function strarting at start_lineno:
sublines = inspect.getblock(all_lines[start_lineno-1:])
...
However, getblock knows nothing about cpdef-function, as python has only def-functions and thus yields wrong function-body (i.e. only the signature).
Workaround:
A simple work around would be to introduce a dummy def-function, which would be a sentinel for the cpdef-function in such a way, that inspect.getblock would yield the whole body of the cpdef-function + body of the the sentinel function, i.e.:
%%cython
...
cpdef np.int64_t get_days(np.int64_t year, np.int64_t month):
...
def get_days_sentinel():
pass
and now the report %lprun -f get_days get_days(2019,3) looks as follows:
Timer unit: 1e-06 s
Total time: 1.7e-05 s
File: XXXX.pyx
Function: get_days at line 10
Line # Hits Time Per Hit % Time Line Contents
==============================================================
10 cpdef np.int64_t get_days(np.int64_t year, np.int64_t month):
11 1 14.0 14.0 82.4 cdef np.ndarray months=np.array([31,28,31,30,31,30,31,31,30,31,30,31])
12 1 1.0 1.0 5.9 if month==2:
13 if (year%4==0 and year%100!=0) or (year%400==0):
14 return 29
15 1 2.0 2.0 11.8 return months[month-1]
16
17 def get_days_sentinel():
18 pass
There are still somewhat ugly trailing lines from the sentinel, but it is probably better as not seeing anything at all.
I try to get month and year format like "06/19" after evaluated month but I got just "6/19".
Month and year
${currentYear}= Get Current Date result_format=%y
${currentDate}= Get Current Date
${datetime} = Convert Date ${currentDate} datetime
${getMonth}= evaluate ${datetime.month} - 1
log to console ${getMonth}/${currentYear}
I already tried another way by created variable #{MONTHSNO} ${EMPTY} 01 02 03 04 05 06 07 08 09 10 11 12 and return ${MONTHSNO}[${getMonth}]/${currentYear} I got 06/19 but I'm not sure the robot have another way to convert month to "06" by without to make the variable like these.
You can acheive this by using a custom keywords that will return the date in month/year format
Then you can use relativedelta() to subtract a month from your date
to install dateutil:
pip install python-dateutil
test.py
from datetime import datetime
from dateutil.relativedelta import relativedelta
def return_current_date_minus_one_month():
strDate = datetime.today()
Subtracted_date = strDate + relativedelta(months=-1)
Date = Subtracted_date.strftime('%m/%y')
return Date
test.robot
*** Settings ***
Library test.py
*** Test Cases ***
Month and year
${current_date} = Test.Return Current Date Minus One Month
log ${current_date}
result = ${current_date} = 06/19
When you run Evaluate command, you are running python commands. So let's take a look at datetime docs:
https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
They have this part in the end saying that library time can be useful. So I suggest you to use this command to return month in 0X format:
${getMonth}= evaluate time.strftime("%m")
This just return 07 to me (because now it's July)
You can substract from your current date one month like this with the DateTime Library:
${date1}= Get Current Date result_format=%d.%m.%Y
${date2}= Substract Time To Date ${date1} 30 days date_format=%d.%m.%Y result_format=%m.%Y
maybe this helps
I am confused about the output of the following code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import datetime
import pytz
local_time = datetime.datetime.now(pytz.timezone('Europe/Berlin'))
utc_time = datetime.datetime.now(datetime.timezone.utc)
print(local_time - utc_time)
I thought a timezone is simply an offset (depending on factors such as the date and the location with DST and changing definitions over time... so not that simple, but still an offset). So a timezone-aware datetime I thought would simply be:
utc_time == '12:34'
+ timezone is Europe/Berlin in UTC 2018-01-01 at 12:34
=> local time = (utc + local timezone) = 2018-01-01 at 13:34
Then I thought, the difference between two datetime objects should be equal to the difference to the UTC at the same point in time (not considering the thought-construct of timezones).
Hence, if I execute "datetime.now" at (about) the same point in time and "assign" it to different time zones, the difference should be on the order of milliseconds that passed between the two commands.
But it actually is -1 day, 23:59:59.999982.
Found it; it's just a representation issue:
>>> print(local_time)
2018-03-13 14:01:14.973876+01:00
>>> print(utc_time)
2018-03-13 13:01:14.973899+00:00
>>> print(utc_time - local_time)
0:00:00.000023
>>> print(local_time - utc_time)
-1 day, 23:59:59.999977
>>> print((local_time - utc_time).total_seconds())
-2.3e-05
so it is -1 day + 23:59:59.999977