2

In robot framework, the current supported keyword to get timezone is:

${month}  Get Current Date  result_format=%B%Y

which will return: July 2017

The question is how to get current date from other country and locale? as example in Vietnam should return: Tháng Bảy 2017 and Thailand should return : กรกฎาคม พ.ศ. 2560

wan mohd payed
  • 171
  • 2
  • 13

2 Answers2

4

Unfortunately, python doesn't have good built-in support for formatting dates in locales other than the current one. You can temporarily switch locales, but that's not a great solution.

You can use the the Babel package to format the dates, though it doesn't provide precisely the same value you expect -- the case is slightly different.

For example, you could create a keyword that dates a month, year, and locale, and returns the formatted version.

First, create a python file named CustomKeywords.py with the following contents:

# CustomKeywords.py
import datetime
from babel.dates import format_date

class CustomKeywords:
    def get_date_for_locale(self, locale, year, month=1, day=1):
        d = datetime.date(int(year), int(month), int(day))
        return format_date(d, "MMMM Y", locale=locale)

Next, import this library into your test case and then call get date for locale to get the date for a given locale:

*** Settings ***
Library  CustomKeywords.py

*** Test Case ***
Vietnamese
    ${date}=  get date for locale  vi_VN  2017  7
    Should be equal  ${date}  tháng bảy 2017

Thailand
    ${date}=  get date for locale  th_TH  2017  7
    Should be equal  ${date}  กรกฎาคม 2017
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
0

In the Robot Framework Datetime library the concept of changing the TimeZone is not present. The Robot Framework Custom Timestamp functions rely on the underlying Python datatime.strptime function. This function uses the python Locale for it's formatting.

As this has now become a more general Python problem, I've searched SO and found this particular SO answer to fulfill the criteria to create a custom Python Keyword for Robot Framework that would allow changing the Locale and thus determine the output.

A. Kootstra
  • 6,827
  • 3
  • 20
  • 43