LAE

Welcome to the LAE community!  Please feel free to start a discussion in the discussion tab or join in a conversation.

Discussions

Members

Resources

Events

 View Only
  • 1.  String to timestamp convertion

    Employee
    Posted 07-27-2010 07:39

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: vachouli

    I want to convert a string into a timestamp but I cannot find the way. Can you please help me with this? the string are formatted as shown below, that means that not all of the are of the same length. (some don't show seconds and some do)

    30/6/2010 12:31:43
    4/6/2010 12:53
    10/6/2010 15:13:24
    etc...

    I want to convert them all in timestamp to be of the following format:
    CCYY-MM-DD HH:MM:SS

    Thanks in advance


  • 2.  RE: String to timestamp convertion

    Employee
    Posted 07-27-2010 08:45

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: stonysmith

    I split the code up here to allow some comments:

    v = InputCol #pickup value from input column
    v = v.replace(" ","/") #change space to slash
    v = v.replace(":","/") #change colon to slash
    v = v + "/0/0/0" #pad with extra fields for time if needed
    s = v.split("/") #split into an array

    year = s.getItem(2).long()
    month = s.getItem(1).long()
    day s.getItem(0).long()
    d = date(year,month,day) #convert to date

    hour = s.getItem(3).long()
    minute = s.getItem(4).long()
    second = s.getItem(5).long()
    t = time(hour,minute,second) #convert to time

    datestr = d.str()+" "+t.str() #convert back to string
    emit *
    emit datestr

    =======================
    It can also be expressed shorter:

    s = split(InputCol.replace(" ","/").replace(":","/") + "/0/0/0","/") #split into an array
    d = date(s.getItem(2).long(),s.getItem(1).long(),s.get Item(0).long())
    t = time(s.getItem(3).long(),s.getItem(4).long(),s.get Item(5).long())
    datestr = d.str()+" "+t.str() #convert back to string

    =======================
    If you want to use US dates, switch the month/day:
    d = date(s.getItem(2).long(),s.getItem(0).long(),s.get Item(1).long()) #convert to date


  • 3.  RE: String to timestamp convertion

    Employee
    Posted 07-28-2010 02:28

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: vachouli

    Thanks a lot. It really helped!
    I'm not an experienced Brain user and not very familiar to BrainScript, so any help is appreciated!