Date, time and datetime functions - Data360_Analyze - Latest

Data360 Analyze Server Help

Product type
Software
Portfolio
Verify
Product family
Data360
Product
Data360 Analyze
Version
Latest
Language
English
Product name
Data360 Analyze
Title
Data360 Analyze Server Help
Copyright
2024
First publish date
2016
Last updated
2024-11-28
Published on
2024-11-28T15:26:57.181000

If you used BRAINscript date, time and datetime functions in LAE, the following examples show how to complete the same task in the Python-based nodes.

You can test the following examples by taking a Create Data node, connecting it to a Transform node, then copy and pasting the examples into the ConfigureFields and ProcessRecords properties of the Transform node.

Note: When copying code from the examples, ensure that you include any indentations.

For more information on the Python functions, please see the Python documentation, e.g. https://docs.python.org/2/library/datetime.html

Tip: You can use the Modify Fields node when working with date, time and datetime functions to simplify the parsing of strings containing dates and the extraction of elements of dates (for example, the year).
BRAINscript Python
  • date()
  • dateSubtract()

datetime.date()

  • dateAdjust()
  • timeSubtract()
  • dateTimeAdjust()
datetime.timedelta()
  • day()
  • month()
  • year()
date.day(), date.month() and date.year()
  • time()
datetime.time()
  • hours()
  • minutes()
  • seconds()
datetime.hour(), datetime.minute() and datetime.second()
  • timestamp()
datetime.datetime()

datetime.date()

This example shows two different ways to specify a date using the Python datetime.date() function:

ConfigureFields


      # Create the new field metadata
outputs[0]['test_Date_1'] = datetime.date
outputs[0]['test_Date_2'] = datetime.date
    

ProcessRecords


      #Copy all fields from input 'inputs[0]' to the 
#corresponding output fields in output 'outputs[0]'. 
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]
# Specify the date object from the elements of a date
outputs[0]['test_Date_1'] = datetime.date(2018, 3, 18)
# Specify the date object by parsing a string using a format spec
datestr = '2018-12-25'
outputs[0]['test_Date_2'] = datetime.datetime.strptime(datestr, '%Y-%m-%d').date()
    

This example shows how to find the difference between two dates. In this case, the unit is days:

ConfigureFields


      # Create the new field metadata
outputs[0]['test_Date_1'] = datetime.date
outputs[0]['test_Date_2'] = datetime.date
outputs[0]['num_days'] = int
    

ProcessRecords


      #Copy all fields from input 'inputs[0]' to the 
#corresponding output fields in output 'outputs[0]'. 
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]

# Specify the date object from the elements of a date
_firstDate = datetime.date(2018, 3, 18)

# Specify the date object by parsing a string using a format spec
datestr = '2018-12-25'
_secondDate = datetime.datetime.strptime(datestr, '%Y-%m-%d').date()

# Calculate the difference between the dates
_delta = _secondDate - _firstDate

# Output the reference dates
outputs[0]['test_Date_1'] = _firstDate
outputs[0]['test_Date_2'] = _secondDate

# Output the difference in days
outputs[0]['num_days'] = _delta.days
    

datetime.timedelta()

Adjust a date value by a specified amount of time e.g. by a number of days:

ConfigureFields


      # import the required modules
from datetime import timedelta
# Create the new field metadata
outputs[0]['test_Date_1'] = datetime.date
outputs[0]['test_Date_2'] = datetime.date
    

ProcessRecords


      #Copy all fields from input 'inputs[0]' to the 
#corresponding output fields in output 'outputs[0]'. 
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]
				
# Specify the date object from the elements of a date
_startDate = datetime.date(2018, 3, 18)
				
# Specify the number of days for the offset
N = 2
				
# Create the timedelta object using the required units
_delta =  timedelta(days=N)
_adjustedDate =  _startDate - _delta
				
# Output the original and adjusted dates
outputs[0]['test_Date_1'] = _startDate
outputs[0]['test_Date_2'] = _adjustedDate

    

Adjust a datetime value by a specified number of days:

ConfigureFields


      # import the required modules
from datetime import timedelta
# Create the new field metadata
outputs[0]['test_Datetime_1'] = datetime.datetime
outputs[0]['test_Datetime_2'] = datetime.datetime
    

ProcessRecords


      #Copy all fields from input 'inputs[0]' to the 
#corresponding output fields in output 'outputs[0]'. 
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]

# Specify the date object from the elements of a date
the_Day = datetime.date(2018, 3, 18)

# Specify the time object from the elements of a time
the_time = datetime.time(22, 58, 45)

# Create the datetime object
dt = datetime.datetime.combine(the_Day, the_time)

# Specify the number of days for the offset
N = 2

# Create the timedelta object using the required units
_delta =  timedelta(days=N)

# Calculate the adjusted datetime
adjusted_dt =  dt - _delta

# Output the original and adjusted datetimes
outputs[0]['test_Datetime_1'] = dt
outputs[0]['test_Datetime_2'] = adjusted_dt
    

Take two time objects and output the difference between the two values in seconds:

ConfigureFields


      # import the required modules
from datetime import timedelta
# Create the new field metadata
outputs[0]['test_Time_1'] = datetime.time
outputs[0]['test_Time_2'] = datetime.time
outputs[0]['test_diff_secs'] = int
    

ProcessRecords


      #Copy all fields from input 'inputs[0]' to the 
#corresponding output fields in output 'outputs[0]'. 
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]

# Specify the time object from the elements of a time
first_time = datetime.time(22, 58, 45)

# Specify the time object from the elements of a time
second_time = datetime.time(23, 0, 0)

# calculate the timedelta after converting to datetime objects
_today = datetime.date.today()
delta = datetime.datetime.combine(_today, second_time) - datetime.datetime.combine(_today, first_time)

# Output the reference times
outputs[0]['test_Time_1'] = first_time
outputs[0]['test_Time_2'] = second_time

# Output the timedelta in seconds
outputs[0]['test_diff_secs'] = delta.seconds
    

Adjust a datetime object by one month, and output a range. This example shows how to output a range of dates where the specified starting day of the month would be used where possible and where not, the last day of the month would be used:

ConfigureFields


      import calendar
def add_n_months(orig_date, num_mths = 1):
    if not isinstance(num_mths, (int, long)):
        node.logger.error("The number of months must be an integer.")
        raise node.fail()
    if num_mths < 1:
        node.logger.error("The number of months must be at least 1.")
        raise node.fail()
    if num_mths > 11:
        years_to_add = num_mths/ 12 ## Integer division used by default
        months_to_add = num_mths % 12
    else:
        years_to_add = 0
        months_to_add = num_mths
# advance year and month by number of months
    new_year = orig_date.year + years_to_add
    new_month = orig_date.month + months_to_add
# note: in datetime.date, months go from 1 to 12
    if new_month > 12:
        new_year += 1
        new_month -= 12

last_day_of_month = calendar.monthrange(new_year, new_month)[1]
new_day = min(orig_date.day, last_day_of_month)

return orig_date.replace(year=new_year, month=new_month, day=new_day)

# Configure all fields from input 'in1' to be mapped
# to the corresponding fields on the output 'out1'
out1 += in1
_startMonth = datetime.datetime(2015,8,31).date()

## Specify the number of records to be output
## for each input record (zero-based)
i = 20

## Define the new date field to hold the adjusted records
out1.FillInMonths = datetime.date
    

ProcessRecords


      # Copy all fields from input 'in1' to the corresponding output fields
# in output 'out1'. Copies the values of the fields that have been setup
# in the mapping defined in the ConfigureFields property
out1 += in1

## Set the first date value
out1.FillInMonths = _startMonth

## Explicitly write the output record
node.write(0, out1)

## Now loop around outputing an adjusted date
## in each record
j = 0
while j < i:
    j += 1
    _FillInMonths = add_n_months(_startMonth, j)
    out1.FillInMonths = _FillInMonths
    node.write(0, out1)
    

date.day(), date.month() and date.year()

Take a date and output the day, month and year in separate fields:

ConfigureFields


      # Create the new field metadata
outputs[0]['test_Date'] = datetime.date
outputs[0]['test_year'] = int
outputs[0]['test_month'] = int
outputs[0]['test_day'] = int
    

ProcessRecords


      #Copy all fields from input 'inputs[0]' to the 
#corresponding output fields in output 'outputs[0]'. 
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]
# Specify the date object from the elements of a date
_testdate = datetime.date(2018, 3, 18)
# Output the reference date
outputs[0]['test_Date'] = _testdate
# Output the elements of the date
outputs[0]['test_year'] = _testdate.year
outputs[0]['test_month'] = _testdate.month
outputs[0]['test_day'] = _testdate.day
    

datetime.time()

Output a time object in the format: hours:minutes:seconds

ConfigureFields


      # Create the new field metadata
outputs[0]['test_Time'] = datetime.time
    

ProcessRecords


      #Copy all fields from input 'inputs[0]' to the 
#corresponding output fields in output 'outputs[0]'. 
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]
# Specify the time object from the elements of a time
outputs[0]['test_Time'] = datetime.time(20, 33, 45)
    

datetime.hour(), datetime.minute() and datetime.second()

Output the elements of a time object in separate fields; hours, minutes and seconds:

ConfigureFields


      # Create the new field metadata
outputs[0]['test_Time'] = datetime.time
outputs[0]['test_Hours'] = int
outputs[0]['test_Mins'] = int
outputs[0]['test_Secs'] = int
    

ProcessRecords


      #Copy all fields from input 'inputs[0]' to the 
#corresponding output fields in output 'outputs[0]'. 
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]
# Specify the time object from the elements of a time
_refTime = datetime.time(20, 33, 45)
# Output the reference time object
outputs[0]['test_Time'] = _refTime
# Output the time elements
outputs[0]['test_Hours'] = _refTime.hour
outputs[0]['test_Mins'] = _refTime.minute
outputs[0]['test_Secs'] = _refTime.second
    

datetime.datetime()

This example shows two ways to create a datetime object:

ConfigureFields


      # Create the new field metadata
outputs[0]['test_Day'] = datetime.date
outputs[0]['test_Time'] = datetime.time
outputs[0]['test_DateTime'] = datetime.datetime
outputs[0]['test_alt_DateTime'] = datetime.datetime
    

ProcessRecords


      #Copy all fields from input 'inputs[0]' to the 
#corresponding output fields in output 'outputs[0]'. 
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]

#### Construct a datetime object from date and time objects
# Specify a date object from the elements of a date
the_day = datetime.date(2018, 3, 18)

# Specify the time object from the elements of a time
the_time = datetime.time(22, 58, 45)
dt = datetime.datetime.combine(the_day, the_time)

# Output the reference date and time
outputs[0]['test_Day'] = the_day
outputs[0]['test_Time'] = the_time

# Output the datetime object
outputs[0]['test_DateTime'] = dt

# Construct a datetime object from a string 
# representation and a format spec.
# The following should be written on one line:
alt_dt = datetime.datetime.strptime('2018-03-18 22:58:45', '%Y-%m-%d %H:%M:%S')

# Output the datetime object
outputs[0]['test_alt_DateTime'] = alt_dt