Forum Discussion

agmark's avatar
agmark
Visitor
14 years ago

Date conversion

I have another roDateTime question. What would be an efficient code to convert the following pubdate format so I can convert it using asSeconds()?
<pubDate>Fri, 30 Dec 2011 19:55:55 EST</pubDate>

I've been hacking at it for a while and thought I'd see if someone has a cleaner way to get it to an acceptable format such as 2011-12-20 19:55:55. If I'm reading roDateTime correctly, a date needs this format in order to convert to seconds and everything I do seems like a hack.
Mark

EDIT... I think I finally got it working right. I'll post the code I'm using in case anyone can clean it up or comment. later...

2 Replies

  • Very nicely done belltown! It's certainly more efficient than what I had written. I don't know how you do it, but I can't imagine a more efficient way. Regex is something I haven't attempted to tackle yet (and by the looks of it, I'm not sure I want to) haha

    In case anyone searches for this in the future, here's the completed code to turn a common date string used in an rss feed into seconds.


    newItem.releaseDate = ValidStr(item.pubdate.GetText()) 'parse date from rss feed
    dateIn = newItem.releaseDate
    ro = CreateObject ("roRegex", "(\d+)\s+([a-z]+)\s+(\d+)\s+(\d+:\d+:\d+)\D", "i")
    da = ro.Match (dateIn)
    ml = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC"
    mon% = (Instr (1, ml, Ucase (da [2])) - 1)/3 + 1
    dateOut = da[3] + "-" + mon%.ToStr () + "-" + da [1] + " " + da [4]
    dt.FromISO8601String(dateOut)
    dateInSeconds = dt.asSeconds()
    print dateOut
    print "asSeconds = " dateInSeconds
    print "-----------------------------"


    Thanks for that belltown. Have a nice new years eve!
  • Thank you belltown and agmark for these code samples. I didn't know how to use the roRegex for this type of function either. I'd searched far and wide on how to convert these non-standard dates to ones I could manipulate and I finally came across this post.

    For my needs I was trying to convert into a more standard date format but I was working with the same type of date/time as agmark, although mine was in this format "Tue, 03 Feb 2015 04:02:11 +0000". Fortunately this same code formatted it properly for me as well. I'm using the MRSS Template to pull in rss feeds to make my channel and some of the rss feeds don't use standard date formats. This was a big help. However it took me a little tweaking and further research to be able to manipulate my dates into the format I wanted.

    I wound up making a function I could call in the NWM_Utilities.brs file of the MRSS Template, so I could re-use this code as needed. I call it using the following code.

    dateTmp = util.convertDate(releaseDate)


    I'm using the 1.2.1 version of the MRSS Template and the code above was added to the NWM_MRSS.brs file in the "else" section of the "release date" section. Here is the whole section if you just want to replace it.

    			
    ' release date
    if item.GetNamedElements("blip:datestamp").Count() > 0
    dt = CreateObject("roDateTime")
    dt.FromISO8601String(ValidStr(item.GetNamedElements("blip:datestamp")[0].GetText()))
    newItem.releaseDate = dt.AsDateStringNoParam()
    else
    releaseDate = ValidStr(item.pubdate.GetText())
    dateTmp = util.convertDate(releaseDate)
    newItem.releaseDate = dateTmp
    end if
    newItem.shortDescriptionLine2 = newItem.releaseDate


    If you use the MRSS example you have to edit the NWM_Utilities.brs file at the top to add this call in the NWM_Utilities function at the top of that file.

    convertDate:			NWM_UT_convertDate


    I then have the following function in that file.


    Function NWM_UT_convertDate(inStr)
    dateIn = inStr
    ro = CreateObject ("roRegex", "(\d+)\s+([a-z]+)\s+(\d+)\s+(\d+:\d+:\d+)\D", "i")
    da = ro.Match (dateIn)
    ml = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC"
    mon% = (Instr (1, ml, Ucase (da [2])) - 1)/3 + 1
    dateOut = da[3] + "-" + mon%.ToStr () + "-" + da [1] + " " + da [4]

    dt=CreateObject("roDateTime")
    dt.fromISO8601String(dateOut)
    dateShrt = dt.AsDateString("short-month-short-weekday")
    result = dateShrt
    print dateShrt
    return result
    End Function


    I'm posting this here so that anyone else who might encounter the same problem with RSS feeds not having standard date format, but rather iTunes formats, can use this to convert the date so they can format it for their channel as they see fit. You can change the format, "short-month-short-weekday", to any of the ones in the Roku SDK documentation for the ifDateTime interface under the AsDateString operation http://sdkdocs.roku.com/display/sdkdoc/ifDateTime. Of course you could probably also modify this to support different locales and make the format a variable based on the proper locale. However I found this is a good start to at least be able to format the Date/Time to your liking in your own channel.

    Thanks again to belltown for the code I could modify to make this work.