Sign in to follow this  
Followers 0
CGRemakes

String date data to timestamp UDF?

5 posts in this topic

#1 ·  Posted (edited)

Are there any good UDFs out there that allow me to take a string date and/or time and convert it to a timestamp. I have something like this:

Nov 27 2009 10:27PM

This is the function what I've come up with (using the UnixTime.au3 file), but I'd like to see if there is something better (or a way to improve this):

#include <UnixTime.au3>

$timestamp = ConvertDateTime("Nov 27 2009 10:27PM")

MsgBox(0, "Timestamp", $timestamp)

Func ConvertDateTime($value)
    
    $temp = StringRegExp($value, '(\w+)\s+(\d+)\s+(\d+)\s+(\d+):(\d+)(AM|PM)', 1)

    If $temp[5] = "PM" Then $temp[3] += 12
        
    Return _TimeMakeStamp(0, $temp[4], $temp[3], $temp[1], ConvertMonth($temp[0]), $temp[2])
EndFunc

Func ConvertMonth($month)
    
    Local $m
    
    Switch $month
    
        Case "Jan"
            $m = "1"
            
        Case "Feb"
            $m = "2"

        Case "Mar"
            $m = "3"
            
        Case "Apr"
            $m = "4"

        Case "May"
            $m = "5"
            
        Case "Jun"
            $m = "6"

        Case "Jul"
            $m = "7"
            
        Case "Aug"
            $m = "8"

        Case "Sep"
            $m = "9"
            
        Case "Oct"
            $m = "10"

        Case "Nov"
            $m = "11"
            
        Case "Dec"
            $m = "12"           
    EndSwitch
        
    Return $m
EndFunc

Something like PHP's strtotime function would be wonderful!

Edited by CGRemakes

Share this post


Link to post
Share on other sites



How many of the _Date* functions from the help file have you tried? They all have examples with them. Post a short demo script showing your issue.

:graduated:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

How many of the _Date* functions from the help file have you tried? They all have examples with them. Post a short demo script showing your issue.

:graduated:

I looked at them, but I didn't see anything built in that does timestamps. There were functions to convert date style strings to other date style strings. Let me know if I'm overlooking something. However, looking at the source for Date.au3 did give me an idea for a cleaner way to convert Jan, Feb, etc to 1, 2 just using arrays rather than a bunch of case statements. It's not that the solution I've come up with doesn't work, I just figured there was some way already discovered.

Edited by CGRemakes

Share this post


Link to post
Share on other sites

I think your solution is fine. I don't have UnixTime.au3, but I can imagine what it does.

You could probably get a slight speed increase out of your StringRegExp by replacing each "+" with a set number.

You can reduce the size of ConvertMonth to something like this:

Func ConvertMonth($month)
    Return String(StringInStr("  JanFebMarAprMayJunJulAugSepOctNovDec",$month)/3)
EndFunc

But that's slower than your solution. (by like 30-40% i believe). You could use that code in ConvertDateTime and do away with the ConvertMonth function. Just depends on your priorities.

Share this post


Link to post
Share on other sites

I think your solution is fine. I don't have UnixTime.au3, but I can imagine what it does.

You could probably get a slight speed increase out of your StringRegExp by replacing each "+" with a set number.

You can reduce the size of ConvertMonth to something like this:

Func ConvertMonth($month)
    Return String(StringInStr("  JanFebMarAprMayJunJulAugSepOctNovDec",$month)/3)
EndFunc

But that's slower than your solution. (by like 30-40% i believe). You could use that code in ConvertDateTime and do away with the ConvertMonth function. Just depends on your priorities.

Good suggestions. Since there doesn't seem to be anything pre-written, I will just keep what I've got (with some modifications as you've mentioned). Thanks!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0