Revision: 3830d8418969
Branch: default
Author: Janne Piironen <[email protected]>
Date: Fri May 30 15:07:26 2014 UTC
Log: DateTime: Added tests for Get Current time and improved Convert
Date output format %f directive handling.
http://code.google.com/p/robotframework/source/detail?r=3830d8418969
Added:
/atest/robot/standard_libraries/datetime/get_current_date.txt
Modified:
/atest/robot/standard_libraries/datetime/convert_date_result_format.txt
/atest/testdata/standard_libraries/datetime/convert_date_result_format.txt
/atest/testdata/standard_libraries/datetime/datesandtimes.py
/atest/testdata/standard_libraries/datetime/get_current_date.txt
/src/robot/libraries/DateTime.py
=======================================
--- /dev/null
+++ /atest/robot/standard_libraries/datetime/get_current_date.txt Fri May
30 15:07:26 2014 UTC
@@ -0,0 +1,26 @@
+*** Settings ***
+Suite Setup Run Tests ${EMPTY}
standard_libraries/datetime/get_current_date.txt
+Force Tags regression pybot jybot
+Resource atest_resource.txt
+
+*** Test Cases ***
+Local time increases
+ Check Test Case ${TESTNAME}
+
+UTC Time
+ Check Test Case ${TESTNAME}
+
+Increment
+ Check Test Case ${TESTNAME}
+
+Negative Increment
+ Check Test Case ${TESTNAME}
+
+Result format timestamp
+ Check Test Case ${TESTNAME}
+
+Result format epoch
+ Check Test Case ${TESTNAME}
+
+Result format datetime
+ Check Test Case ${TESTNAME}
=======================================
--- /atest/robot/standard_libraries/datetime/convert_date_result_format.txt
Fri May 30 13:23:14 2014 UTC
+++ /atest/robot/standard_libraries/datetime/convert_date_result_format.txt
Fri May 30 15:07:26 2014 UTC
@@ -18,3 +18,6 @@
Should exclude milliseconds
Check Test Case ${TESTNAME}
+
+Formatted with %f in middle
+ Check Test Case ${TESTNAME}
=======================================
---
/atest/testdata/standard_libraries/datetime/convert_date_result_format.txt
Fri May 30 13:23:14 2014 UTC
+++
/atest/testdata/standard_libraries/datetime/convert_date_result_format.txt
Fri May 30 15:07:26 2014 UTC
@@ -15,7 +15,6 @@
Should convert to timestamp with format
2014-04-24 21:45:12.123
TimeStamp:%H:%M:%S %Y-%m-%d 21:45:12 2014-04-24
- 2014-04-24 21:45:12.123
TimeStamp:%H:%M:%S.%f %Y-%m-%d 21:45:12.123000 2014-04-24
2014/04/24 21:45:12.123
timestamp:%H:%M %Y-%m-%d 21:45
2014-04-24 %Y/%m/%d %H:%M:%S.%f
Should convert to epoch
@@ -36,6 +35,14 @@
2014-04-24 21:45:12.999
timestamp 2014-04-24 21:45:13
${DATE}
timestamp 2014-04-24 21:45:12
${EPOCH + 0.123}
datetime ${datetime(2014, 4, 24, 21, 45, 12)}
+
+Formatted with %f in middle
+ [Template] NONE
+ Run Keyword If sys.version_info < (2, 6) or sys.platform == 'cli'
+ ... Run Keyword And Expect Error ValueError: %f directive is
supported only at the end of the format string on this Python interpreter.
+ ... Date Conversion Should Succeed 2014-04-24 21:45:12.123
TimeStamp:%H:%M:%S.%f %Y-%m-%d 21:45:12.123000 2014-04-24
+ ... ELSE
+ ... Date Conversion Should Succeed 2014-04-24 21:45:12.123
TimeStamp:%H:%M:%S.%f %Y-%m-%d 21:45:12.123000 2014-04-24
*** Keywords ***
Date Conversion Should Succeed
=======================================
--- /atest/testdata/standard_libraries/datetime/datesandtimes.py Fri May 30
13:23:14 2014 UTC
+++ /atest/testdata/standard_libraries/datetime/datesandtimes.py Fri May 30
15:07:26 2014 UTC
@@ -1,4 +1,5 @@
from datetime import timedelta, datetime
import time
-EPOCH = 1398375912.0 + (time.altzone if time.localtime().tm_isdst else
time.timezone)
+TIMEZONE = time.altzone if time.localtime().tm_isdst else time.timezone
+EPOCH = 1398375912.0 + TIMEZONE
=======================================
--- /atest/testdata/standard_libraries/datetime/get_current_date.txt Mon
May 26 12:20:29 2014 UTC
+++ /atest/testdata/standard_libraries/datetime/get_current_date.txt Fri
May 30 15:07:26 2014 UTC
@@ -1,13 +1,37 @@
*** Settings ***
Library DateTime
Variables datesandtimes.py
-Test Template Compare Current Date
*** Test Cases ***
-Local Time
+Local time increases
+ ${date1} = Get Current Date result_format=epoch
+ ${date2} = Get Current Date increment=1s result_format=epoch
+ Should Be True ${date2} > ${date1}
+
UTC Time
+ ${before} = Get Current Date result_format=epoch
+ ${utc} = Get Current Date UTC result_format=epoch
+ ${after} = Get Current Date result_format=epoch
+ Should Be True ${before} <= ${utc} - ${TIMEZONE} <= ${after}
+
Increment
+ ${date1} = Get Current Date result_format=epoch
+ ${date2} = Get Current Date increment=2h result_format=epoch
+ Should Be True 7201 >= ${date2} - ${date1} >= 7200
+
Negative Increment
-Result formats
-*** Keywords ***
-Compare Current Date
+ ${date1} = Get Current Date result_format=epoch
+ ${date2} = Get Current Date increment=-3minutes result_format=epoch
+ Should Be True -179 >= ${date2} - ${date1} >= -180
+
+Result format timestamp
+ ${date} = Get Current Date result_format=timestamp
+ Should Match Regexp ${date} \\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}
+
+Result format epoch
+ ${date} = Get Current Date result_format=epoch
+ Should Be True 1000000000 < ${date} < 2000000000
+
+Result format datetime
+ ${date} = Get Current Date result_format=datetime
+ Should Be Equal ${date.replace(microsecond=0)}
${datetime.now().replace(microsecond=0)}
=======================================
--- /src/robot/libraries/DateTime.py Mon May 26 12:20:29 2014 UTC
+++ /src/robot/libraries/DateTime.py Fri May 30 15:07:26 2014 UTC
@@ -151,27 +151,34 @@
if not input_format:
dt = self._normalize_timestamp(dt)
input_format = '%Y-%m-%d %H:%M:%S.%f'
- if '%f' in input_format and (sys.version_info < (2, 6) or
- sys.platform == 'cli'):
+ if self._need_to_handle_f_directive(input_format):
return self._handle_un_supported_f_directive(dt, input_format)
return self._mktime_with_millis(datetime.strptime(dt,
input_format))
+ def _need_to_handle_f_directive(self, format):
+ if '%f' in format and (sys.version_info < (2, 6) or sys.platform
== 'cli'):
+ return True
+ return False
+
def _normalize_timestamp(self, date):
ts = ''.join(d for d in date if d in string.digits).ljust(20, '0')
return '%s-%s-%s %s:%s:%s.%s' % (ts[:4], ts[4:6], ts[6:8],
ts[8:10],
ts[10:12], ts[12:14], ts[14:])
def _handle_un_supported_f_directive(self, dt, input_format):
- if not input_format.endswith('%f'):
- raise ValueError('%f directive is supported only at the end
of '
- 'the format string on this Python
interpreter.')
- input_format = input_format[:-2]
+ input_format = self._remove_f_from_format(input_format)
micro = re.search('\d+$', dt).group(0)
dt = dt[:-len(micro)]
epoch = time.mktime(time.strptime(dt, input_format))
epoch += float(micro) / 10**len(micro)
return epoch
+ def _remove_f_from_format(self, format):
+ if not format.endswith('%f'):
+ raise ValueError('%f directive is supported only at the end
of '
+ 'the format string on this Python
interpreter.')
+ return format[:-2]
+
def _dt_from_timestamp(self, ts):
# Jython and IronPython handle floats incorrectly. For example,
# datetime.fromtimestamp(1399410716.123).microsecond == 122999
@@ -196,8 +203,11 @@
def _convert_to_timestamp(self, seconds, millis=True,
output_format=None):
dt = self._dt_from_timestamp(seconds)
if output_format:
- output_format = str(output_format) #Python 2.5 does not accept
unicode
- millis = False
+ if self._need_to_handle_f_directive(output_format):
+ output_format = self._remove_f_from_format(output_format)
+ else:
+ output_format = str(output_format) #Python 2.5 does not
accept unicode
+ millis = False
else:
output_format = '%Y-%m-%d %H:%M:%S'
ts = dt.strftime(output_format)
@@ -205,7 +215,6 @@
ts += '.%03d' % (round(seconds % 1 * 1000))
return ts
-
def _convert_to_epoch(self, dt, millis=True):
return dt
--
---
You received this message because you are subscribed to the Google Groups "robotframework-commit" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.