Jump to content
Sign in to follow this  
BartW

Unix Date Time conversion to readable date

Recommended Posts

BartW

Hi all,

I needed to change a Unix Time stamp into somting that i can read .

so i created a funcion to do that .

I whant to know if this is the best way of doing it.

Thanks in advance

Func Unixdatetime($data)
    local $day ,$Month
    $LeapYear = False
    $Dayamount = $data / (60 * 60 * 24)
    $Tempdata = StringSplit($Dayamount, ".")
    $Dayamount = $Tempdata[1]
    $Tempdata = StringSplit($Dayamount / 365, ".")
    $LeapDayAmound = $Tempdata[1] / 4
    $Year = 1970 + $Tempdata[1]
    $DaysLeft = $Dayamount - ($Tempdata[1] * 365)
    If StringInStr($LeapDayAmound, ".") Then
        $Tempdata = StringSplit($LeapDayAmound, ".")
        $LeapDayAmound = $Tempdata[1]
    Else
        $LeapDayAmound = $LeapDayAmound-1
        $LeapYear = True
    EndIf
    
    If $DaysLeft < 31 Then
        $Month = 1
        $day = $DaysLeft
    ElseIf $DaysLeft < 58 Then
        $Month = 2
        $day = $DaysLeft - 31
    ElseIf $DaysLeft < 90 Then
        If $LeapYear = True Then
            If $DaysLeft < 59 Then
                $Month = 2
                $day = $DaysLeft - 31
            Else
                $Month = 3
                $day = $DaysLeft - 58
            EndIf
        Else
            $Month = 3
            $day = $DaysLeft - 58
        EndIf
    ElseIf $DaysLeft < 120 Then
        $Month = 4
        $day = $DaysLeft - 90
    ElseIf $DaysLeft < 151 Then
        $Month = 5
        $day = $DaysLeft - 120
    ElseIf $DaysLeft < 181 Then
        $Month = 6
        $day = $DaysLeft - 151
    ElseIf $DaysLeft < 212 Then
        $Month = 7
        $day = $DaysLeft - 181
    ElseIf $DaysLeft < 243 Then
        $Month = 8
        $day = $DaysLeft - 212
    ElseIf $DaysLeft < 273 Then
        $Month = 9
        $day = $DaysLeft - 243
    ElseIf $DaysLeft < 304 Then
        $Month = 10
        $day = $DaysLeft - 273
    ElseIf $DaysLeft < 334 Then
        $Month = 11
        $day = $DaysLeft - 304
    ElseIf $DaysLeft < 365 Then
        $Month = 12
        $day = $DaysLeft - 334
    EndIf
    $day = $day-$LeapDayAmound
    $SecondsLeft1 = $data - ($Dayamount * (60 * 60 * 24))
    $Tempdata = StringSplit($SecondsLeft1 / 60 / 60, ".")
    $uren = $Tempdata[1]
    $SecondsLeft2 = $SecondsLeft1 - ($uren * (60 * 60))
    $Tempdata = StringSplit($SecondsLeft2 / 60, ".")
    $min = $Tempdata[1]
    $sec = $SecondsLeft2 - ($min * (60))
    If StringLen($Month) < 2 Then $Month = 0 & $Month
    If StringLen($day) < 2 Then $day = 0 & $day
    If StringLen($uren) < 2 Then $uren = 0 & $uren
    If StringLen($min) < 2 Then $min = 0 & $min
    If StringLen($sec) < 2 Then $sec = 0 & $sec
    $export =  $day & "/" & $Month & "/" & $Year &" "&$uren & ":" & $min & ":" & $sec
    Return $export
EndFunc   ;==>datetime

Share this post


Link to post
Share on other sites
jchd

Aren't you making your life harder than it should be?

ConsoleWrite(_EpochToISO(0) & @LF)
ConsoleWrite(_EpochToISO(1230768002) & @LF)

Func _EpochToISO($iEpoch)
    Return _DateAdd("s", $iEpoch, "1970/01/01 00:00:00")
EndFunc

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
BartW

Jep that is much easier.

:sweating:

Thanx

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  

×