Revision: 9acef4e304a4
Branch: default
Author: Janne Piironen <janne.piiro...@gmail.com>
Date: Mon May 26 12:20:29 2014 UTC
Log: DateTime: Implemented calculation keywords for Dates and Times.
Update issue 415
Implemented keywords Subtract Dates, Add To Date, Subtract From Date, Add
To Time, Subtract From Time and Get Current Date.
http://code.google.com/p/robotframework/source/detail?r=9acef4e304a4
Added:
/atest/robot/standard_libraries/datetime/add_to_date.txt
/atest/robot/standard_libraries/datetime/subtract_dates.txt
/atest/robot/standard_libraries/datetime/subtract_from_date.txt
/atest/robot/standard_libraries/datetime/time_calculations.txt
/atest/testdata/standard_libraries/datetime/add_to_date.txt
/atest/testdata/standard_libraries/datetime/get_current_date.txt
/atest/testdata/standard_libraries/datetime/subtract_dates.txt
/atest/testdata/standard_libraries/datetime/subtract_from_date.txt
/atest/testdata/standard_libraries/datetime/time_calculations.txt
Modified:
/atest/testdata/standard_libraries/datetime/convert_date_input_format.txt
/atest/testdata/standard_libraries/datetime/convert_date_result_format.txt
/atest/testdata/standard_libraries/datetime/convert_time_input_format.txt
/src/robot/libraries/DateTime.py
=======================================
--- /dev/null
+++ /atest/robot/standard_libraries/datetime/add_to_date.txt Mon May 26
12:20:29 2014 UTC
@@ -0,0 +1,8 @@
+*** Settings ***
+Suite Setup Run Tests ${EMPTY}
standard_libraries/datetime/add_to_date.txt
+Force Tags regression pybot jybot
+Resource atest_resource.txt
+
+*** Test Cases ***
+Time addition to date should succeed
+ Check Test Case ${TESTNAME}
=======================================
--- /dev/null
+++ /atest/robot/standard_libraries/datetime/subtract_dates.txt Mon May 26
12:20:29 2014 UTC
@@ -0,0 +1,8 @@
+*** Settings ***
+Suite Setup Run Tests ${EMPTY}
standard_libraries/datetime/subtract_dates.txt
+Force Tags regression pybot jybot
+Resource atest_resource.txt
+
+*** Test Cases ***
+Subtraction between two dates should succeed
+ Check Test Case ${TESTNAME}
=======================================
--- /dev/null
+++ /atest/robot/standard_libraries/datetime/subtract_from_date.txt Mon May
26 12:20:29 2014 UTC
@@ -0,0 +1,8 @@
+*** Settings ***
+Suite Setup Run Tests ${EMPTY}
standard_libraries/datetime/subtract_from_date.txt
+Force Tags regression pybot jybot
+Resource atest_resource.txt
+
+*** Test Cases ***
+Time subtraction from date should succeed
+ Check Test Case ${TESTNAME}
=======================================
--- /dev/null
+++ /atest/robot/standard_libraries/datetime/time_calculations.txt Mon May
26 12:20:29 2014 UTC
@@ -0,0 +1,11 @@
+*** Settings ***
+Suite Setup Run Tests ${EMPTY}
standard_libraries/datetime/time_calculations.txt
+Force Tags regression pybot jybot
+Resource atest_resource.txt
+
+*** Test Cases ***
+Time addition to time should succeed
+ Check Test Case ${TESTNAME}
+
+Time subtraction from time should succeed
+ Check Test Case ${TESTNAME}
=======================================
--- /dev/null
+++ /atest/testdata/standard_libraries/datetime/add_to_date.txt Mon May 26
12:20:29 2014 UTC
@@ -0,0 +1,20 @@
+*** Settings ***
+Library DateTime
+Variables datesandtimes.py
+Test Template Addition should succeed
+
+*** Variables ***
+${DATE1} ${datetime(2014, 4, 24, 21, 45, 12, 123000)}
+${DATE2} ${datetime(2014, 4, 24, 22, 45, 12, 123000)}
+
+*** Test Cases ***
+Time addition to date should succeed
+ ${DATE1} 1 hour ${DATE2}
+ ${DATE1} 1h ${DATE2}
+ 22:45:12 2014.04.24 01:02:01.000 23:47:13 2014.04.24
timestamp:%H:%M:%S %Y.%m.%d %H:%M:%S %Y.%m.%d
+
+*** Keywords ***
+Addition Should Succeed
+ [Arguments] ${date} ${time} ${expected}
${result_format}=datetime ${input_format}=${NONE}
+ ${new_date} = Add To Date ${date} ${time}
${result_format} date_format=${input_format}
+ Should Be Equal ${new_date} ${expected}
=======================================
--- /dev/null
+++ /atest/testdata/standard_libraries/datetime/get_current_date.txt Mon
May 26 12:20:29 2014 UTC
@@ -0,0 +1,13 @@
+*** Settings ***
+Library DateTime
+Variables datesandtimes.py
+Test Template Compare Current Date
+
+*** Test Cases ***
+Local Time
+UTC Time
+Increment
+Negative Increment
+Result formats
+*** Keywords ***
+Compare Current Date
=======================================
--- /dev/null
+++ /atest/testdata/standard_libraries/datetime/subtract_dates.txt Mon May
26 12:20:29 2014 UTC
@@ -0,0 +1,26 @@
+*** Settings ***
+Library DateTime
+Variables datesandtimes.py
+Test Template Subtraction Should Succeed
+
+*** Variables ***
+${DATE1} ${datetime(2014, 4, 24, 21, 45, 12, 123000)}
+${DATE2} ${datetime(2014, 4, 24, 22, 45, 12, 123000)}
+
+*** Test Cases ***
+Subtraction between two dates should succeed
+ [Template] Subtraction should succeed
+ ${DATE2} ${DATE1} 1 hour
+ ${DATE1} ${DATE2} - 1 hour
+ ${DATE2} ${DATE1}
${3600.0} result_format=number
+ ${DATE2} ${DATE1}
01:00:00.000 result_format=clock
+ ${DATE2} ${DATE1}
1h result_format=compact
+ ${DATE2} ${DATE1}
${timedelta(hours=1)} result_format=timedelta
+ 2014.04.24 22:45:12.123 ${DATE1} 1 hour
+ 22:45:12 2014.04.24 2014-04-24 21.43.11
01:02:01.000 result_format=clock
date1_format=%H:%M:%S %Y.%m.%d date2_format=%Y-%m-%d %H.%M.%S
+
+*** Keywords ***
+Subtraction Should Succeed
+ [Arguments] ${latter_date} ${former_date} ${expected}
${result_format}=verbose ${date1_format}=${NONE}
${date2_format}=${NONE}
+ ${diff} = Subtract Dates ${latter_date} ${former_date}
${result_format} date1_format=${date1_format}
date2_format=${date2_format}
+ Should Be Equal ${diff} ${expected}
=======================================
--- /dev/null
+++ /atest/testdata/standard_libraries/datetime/subtract_from_date.txt Mon
May 26 12:20:29 2014 UTC
@@ -0,0 +1,21 @@
+*** Settings ***
+Library DateTime
+Variables datesandtimes.py
+Test Template Time Subtraction Should Succeed
+
+*** Variables ***
+${DATE1} ${datetime(2014, 4, 24, 21, 45, 12, 123000)}
+${DATE2} ${datetime(2014, 4, 24, 22, 45, 12, 123000)}
+
+*** Test Cases ***
+Time subtraction from date should succeed
+ ${DATE2} 1 hour ${DATE1}
+ ${DATE2} ${timedelta(hours=1)} ${DATE1}
+ 23:47:13 2014.04.24 01:02:01.000 22:45:12
2014.04.24 %H:%M:%S %Y.%m.%d timestamp:%H:%M:%S %Y.%m.%d
+ 23:47:13 2014.04.24 00:00:00.100 23:47:12
2014.04.24 %H:%M:%S %Y.%m.%d timestamp:%H:%M:%S %Y.%m.%d
+
+*** Keywords ***
+Time Subtraction Should Succeed
+ [Arguments] ${date} ${time} ${expected}
${input_format}=${NONE} ${result_format}=datetime
+ ${new_date} = Subtract From Date ${date} ${time}
${result_format} date_format=${input_format}
+ Should Be Equal ${new_date} ${expected}
=======================================
--- /dev/null
+++ /atest/testdata/standard_libraries/datetime/time_calculations.txt Mon
May 26 12:20:29 2014 UTC
@@ -0,0 +1,27 @@
+*** Settings ***
+Library DateTime
+Variables datesandtimes.py
+
+*** Test Cases ***
+Time addition to time should succeed
+ [Template] Time Addition To Time Should Succeed
+ 01:00:00.000 1 hour 2 hours
+ 5 hours 3 minutes 4 minutes 05:07:00.000 clock
+ 5 hours 3 minutes ${4.0} 05:03:04.000 clock
+
+Time subtraction from time should succeed
+ [Template] Time Subtraction From Time Should Succeed
+ 02:00:00.000 1 hour 1 hour
+ 5 hours 3 minutes 4 minutes 04:59:00.000 clock
+ 5 hours 3 minutes ${4.0} 05:02:56.000 clock
+
+*** Keywords ***
+Time Addition To Time Should Succeed
+ [Arguments] ${time1} ${time2} ${expected}
${result_format}=verbose
+ ${new_time} = Add To Time ${time1} ${time2}
${result_format}
+ Should Be Equal ${new_time} ${expected}
+
+Time Subtraction From Time Should Succeed
+ [Arguments] ${time1} ${time2} ${expected}
${result_format}=verbose
+ ${new_time} = Subtract From Time ${time1} ${time2}
${result_format}
+ Should Be Equal ${new_time} ${expected}
=======================================
---
/atest/testdata/standard_libraries/datetime/convert_date_input_format.txt
Mon May 12 13:09:05 2014 UTC
+++
/atest/testdata/standard_libraries/datetime/convert_date_input_format.txt
Mon May 26 12:20:29 2014 UTC
@@ -56,5 +56,5 @@
*** Keywords ***
Date Conversion Should Succeed
[Arguments] ${input} ${expected} ${input_format}=${NONE}
- ${ts} = Convert Date ${input} input_format=${input_format}
+ ${ts} = Convert Date ${input} date_format=${input_format}
Should Be Equal ${ts} ${expected}
=======================================
---
/atest/testdata/standard_libraries/datetime/convert_date_result_format.txt
Mon May 12 13:09:05 2014 UTC
+++
/atest/testdata/standard_libraries/datetime/convert_date_result_format.txt
Mon May 26 12:20:29 2014 UTC
@@ -14,8 +14,9 @@
${DATE}
TimeStamp 2014-04-24 21:45:12.123
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 %Y-%m-%d 21:45
2014-04-24 %Y/%m/%d %H:%M:%S.%f
+ 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
2014-04-24 21:45:12.123
epoch ${1398365112.123}
@@ -39,7 +40,7 @@
*** Keywords ***
Date Conversion Should Succeed
[Arguments] ${input} ${output_format} ${expected}
${input_format}=${NONE}
- ${ts} = Convert Date ${input} ${output_format}
${input_format}
+ ${ts} = Convert Date ${input} ${output_format}
date_format=${input_format}
Should Be Equal ${ts} ${expected}
Date Conversion Should Succeed Without Milliseconds
=======================================
---
/atest/testdata/standard_libraries/datetime/convert_time_input_format.txt
Fri Apr 25 13:22:40 2014 UTC
+++
/atest/testdata/standard_libraries/datetime/convert_time_input_format.txt
Mon May 26 12:20:29 2014 UTC
@@ -30,6 +30,12 @@
01:02:03.004 3723.004
99:59:59.999 359999.999
100:00:00.000 360000
+ -01:02:03.004 -3723.004
+ +01:02:03.004 3723.004
+ 0:00:00.001 0.001
+ 1:00:00.000 3600
+ 000000000:00:00.001 0.001
+ 000000001:00:00.000 3600
Clock without millis
00:00:00 0
=======================================
--- /src/robot/libraries/DateTime.py Mon May 12 13:09:05 2014 UTC
+++ /src/robot/libraries/DateTime.py Mon May 26 12:20:29 2014 UTC
@@ -1,10 +1,24 @@
+# Copyright 2008-2014 Nokia Solutions and Networks
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
from datetime import timedelta, datetime
import time
import string
import sys
import re
-from robot.utils import elapsed_time_to_string, secs_to_timestr,
timestr_to_secs, parse_time
+from robot.utils import elapsed_time_to_string, secs_to_timestr,
timestr_to_secs
class DateTime(object):
@@ -12,16 +26,57 @@
def convert_time(self, time, result_format='number',
exclude_millis=False):
return Time(time).convert(result_format, millis=not exclude_millis)
- def convert_date(self, date, result_format='timestamp',
input_format=None, exclude_millis=False):
- return Date(date, input_format).convert(result_format, millis=not
exclude_millis)
+ def convert_date(self, date, result_format='timestamp',
exclude_millis=False, date_format=None):
+ return Date(date, date_format).convert(result_format, millis=not
exclude_millis)
+ def subtract_dates(self, date1, date2, result_format='number',
exclude_millis=False, date1_format=None, date2_format=None):
+ time = Date(date1, date1_format) - Date(date2, date2_format)
+ return time.convert(result_format, millis=not exclude_millis)
+
+ def add_to_date(self, date, time, result_format='timestamp',
exclude_millis=False, date_format=None):
+ date = Date(date, date_format) + Time(time)
+ return date.convert(result_format, millis=not exclude_millis)
+
+ def subtract_from_date(self, date, time, result_format='timestamp',
exclude_millis=False, date_format=None):
+ date = Date(date, date_format) - Time(time)
+ return date.convert(result_format, millis=not exclude_millis)
+
+ def add_to_time(self, time1, time2, result_format='number',
exclude_millis=False):
+ time = Time(time1) + Time(time2)
+ return time.convert(result_format, millis=not exclude_millis)
+
+ def subtract_from_time(self, time1, time2, result_format='number',
exclude_millis=False):
+ time = Time(time1) - Time(time2)
+ return time.convert(result_format, millis=not exclude_millis)
+
+ def get_current_date(self, time_zone='local', increment='0',
result_format='timestamp', exclude_millis=False):
+ dt = self._get_current_date(time_zone.upper())
+ date = Date(dt) + Time(increment)
+ return date.convert(result_format, millis=not exclude_millis)
+
+ def _get_current_date(self, time_zone):
+ if time_zone == 'LOCAL':
+ return datetime.now()
+ if time_zone == 'UTC':
+ return datetime.utcnow()
+ raise ValueError('Unsupported timezone %s' % time_zone)
class Time(object):
- _clock_re = re.compile('(\d{2,}):(\d{2}):(\d{2})(\.(\d{3}))?')
+ _clock_re = re.compile('([-+])?(\d+):(\d{2}):(\d{2})(\.\d{3})?')
def __init__(self, time):
self.seconds = self._convert_time_to_seconds(time)
+ def __add__(self, other):
+ if isinstance(other, Time):
+ return Time(self.seconds + other.seconds)
+ raise TypeError('Can only add Time to Time, not %s' %
type(other).__name__)
+
+ def __sub__(self, other):
+ if isinstance(other, Time):
+ return Time(self.seconds - other.seconds)
+ raise TypeError('Can only subtract Time from Time, not %s' %
type(other).__name__)
+
def _convert_time_to_seconds(self, time):
if isinstance(time, timedelta):
# timedelta.total_seconds() is new in Python 2.7
@@ -34,11 +89,12 @@
return timestr_to_secs(time)
def _convert_clock_to_secs(self, match):
- hours, minutes, seconds, millis_included, millis = match.groups()
+ prefix, hours, minutes, seconds, millis = match.groups()
result = 60 * 60 * int(hours) + 60 * int(minutes) + int(seconds)
- print match.groups()
- if millis_included:
- result += int(millis) / 1000.0
+ if millis:
+ result += int(millis[1:]) / 1000.0
+ if prefix == '-':
+ result *= -1
return result
def convert(self, format, millis=True):
@@ -67,9 +123,21 @@
class Date(object):
- def __init__(self, dt, input_format):
+ def __init__(self, dt, input_format=None):
self.seconds = self._convert_to_secs(dt, input_format)
+ def __add__(self, other):
+ if isinstance(other, Time):
+ return Date(self.seconds + other.seconds)
+ raise TypeError('Can only add Time to Date, not %s' %
type(other).__name__)
+
+ def __sub__(self, other):
+ if isinstance(other, Date):
+ return Time(self.seconds - other.seconds)
+ if isinstance(other, Time):
+ return Date(self.seconds - other.seconds)
+ raise TypeError('Can only subtract Date or Time from Date,
not %s' % type(other).__name__)
+
def _convert_to_secs(self, timestamp, input_format):
if isinstance(timestamp, basestring):
timestamp = self._string_to_epoch(timestamp, input_format)
@@ -86,8 +154,7 @@
if '%f' in input_format and (sys.version_info < (2, 6) or
sys.platform == 'cli'):
return self._handle_un_supported_f_directive(dt, input_format)
- else:
- return self._mktime_with_millis(datetime.strptime(dt,
input_format))
+ return self._mktime_with_millis(datetime.strptime(dt,
input_format))
def _normalize_timestamp(self, date):
ts = ''.join(d for d in date if d in string.digits).ljust(20, '0')
@@ -126,16 +193,18 @@
dt = self.seconds if millis else round(self.seconds)
return result_converter(dt, millis)
- def _convert_to_timestamp(self, dt, millis=True, output_format=None):
- dtime = self._dt_from_timestamp(dt)
+ def _convert_to_timestamp(self, seconds, millis=True,
output_format=None):
+ dt = self._dt_from_timestamp(seconds)
if output_format:
- return datetime.strftime(dtime, output_format)
- if dtime.microsecond and millis:
- return str(dtime)[:-3]
- elif millis:
- return str(dtime) + '.000'
+ output_format = str(output_format) #Python 2.5 does not accept
unicode
+ millis = False
else:
- return str(dtime)
+ output_format = '%Y-%m-%d %H:%M:%S'
+ ts = dt.strftime(output_format)
+ if millis:
+ 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 robotframework-commit+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.