2013-05-21

Date / Datetime string in FME

FME converts a datetime value read from a source dataset into 'YYYYmmddHHMMSS.000' string - having a decimal fraction.
Since the DateFormatter transformer doesn't recognize a digit string with a decimal fraction as a datetime representation, we have to round it before formatting with the transformer.
And we also have to format a date / datetime string into 'YYYYmmdd' or 'YYYYmmddHHMMSS' before writing it into a date / datetime type attribute (field) of a destination dataset.
Community > MS SQL Server Writer: Failed to parse `Date' from attribute value

=====
2013-11-01
There seems to be a case that the Excel writer doesn't convert a datetime string to a datetime value. Quote my post for the Community:
"I tested Excel writer in FME 2013 SP4, Windows Xp SP3.
Input datetime string format is YYYYmmddHHMMSS in all cases.
Results:
1) XLS_ADO writer, Excel version 97/2000/2002/2003 (*.xls)
Writer User Attribute Type = datetime
The value is written as a datetime value. But the field format cannot be specified in FME, it has to be modified manually after writing if necessary.

2) XLS_ADO writer, Excel version 2007 (*.xlsx)
Writer User Attribute Type = datetime
The value is written as a character string. Why?

3) XLSXW writer (new Excel writer added in FME 2013 SP2)
Writer User Attribute Type = datetime
The value is written as a datetime value, and also the field format (e.g. yyyy-mm-dd) can be specified in the writer feature type dialog box."
-- Community > Change ouput format of datetime in Excel_ADO Writer?
=====

2013-05-19

FME stores all attributes as character strings

FME Workbench > FME Architecture (Reference) > Attributes
"Feature attributes are usually a primitive type: integers, floats, characters. Internally, FME stores all attributes as character strings and automatically converts between a string representation and a numeric representation as needed."
=====
2015-05-07: Unfortunately the page has been retired. The URL is invalid now.
=====

But, we can change the type through Python script if necessary and convertible.
Community > How to coerce attribute type for JSONTemplater?
-----
2013-09-30
Continue to FME stores all attributes as character strings: Part 2
-----

2013-05-18

Parsing attribute values

Community > Parsing attribute values

- StringSearcher or AttributeSplitter

- Tester
- StringReplacer or StringConcatenator

I would use a combination of StringSearcher and StringReplacer with:

^(.+) to (.+)$

# Python script also can be used.

import fmeobjects
import re

class UpDownRegulator(object):

    def __init__(self):
        self.attrName = 'up_to_down'
        self.regex = re.compile('^\s*(.+)\s+to\s+(.+)\s*$')

    def input(self, feature):

        s = feature.getAttribute(self.attrName)
        if s:
            m = self.regex.match(s)
            if m:
                u, d = m.group(1), m.group(2)
                if u < d: u, d = d, u
                feature.setAttribute(self.attrName, '%s to %s' % (u, d))
        self.pyoutput(feature)

    def close(self):

        pass

Sort only part of an attribute in a list

Community > Sort only part of an attribute in a list

# Python script version
import fmeobjects
import re

AttrName = ['field0', 'field1', 'field2', 'field3']

def sortAttributes(feature):
    v = {}
    for n in AttrName:
        s = feature.getAttribute(n)
        if s and re.match('^.+_.+$', s): v[s.split('_')[-1]] = s
    i = 0
    for k in sorted(v.keys()):
        feature.setAttribute(AttrName[i], v[k])
        i = i + 1
    for j in range(i, len(AttrName)):
        feature.setAttribute(AttrName[j], '')