Jump to content

Date functions


GEOSoft
 Share

Recommended Posts

For the Star Trek fans we have _DateToStardate() and _StardateToDate()

Modified functons are my versions of _Now() and _DateIsLeapYear()

_Now() was modified to return the same information that the VB Now function returns and to return it as an array

_DateIsLeapYear() was modified to shorten it

#include-once

;===============================================================================
; Function Name:   _DateIsLeapYear
; Description:   Determines if a given year is a leap year
; Syntax:   _DateIsLeapYear($iYear)
; Parameter(s):   $iYear - The year to check
; Requirement(s):   
; Return Value(s):   Success - 1 = leap year, 0 = not a leap year
;                            Failure - 0 and sets @error to 1
; Author(s):   GEOSoft
; Modification(s):   
; Note(s):   
; Example(s):   
;===============================================================================

Func _DateIsLeapYear($iYear)
    If NOT StringIsInt($iYear) Then Return SetError(1,1,0)
    If Mod($iYear, 400) = 0 OR (Mod($iYear, 4) = 0 And Mod($iYear, 100) <> 0) Then Return 1 ;;<<===== it's a leap year
    Return 0
EndFunc

;===============================================================================
; Function Name:   _DateToStarDate()
; Description:   Converts a given year to a Star date (as used on Star Trek)
; Syntax:   _DateToStarDate([$iYear])
; Parameter(s):   $iYear (optional) - The year to convert. The default is the current year
; Requirement(s):
; Return Value(s):   Success - The star date
; Author(s):   GEOSoft
; Modification(s):   
; Note(s):   
; Example(s):   
;===============================================================================

Func _DateToStarDate($iYear = '')
    If $iYear = '' Then $iYear = @Year
    Local $Leapyear = _DateIsLeapYear($iYear)
    $sY = $iYear - 2323
    $sX = Int(1000 * @YDAY / ( 365 + $leapyear ))
    $sF = Round((60 * @Hour + @Min ) / 144, 2)
    $sDate = $sY & $sX & $sF
    Return $sDate
EndFunc   ;<==> _DateToStarDate($iYear = '')

;===============================================================================
; Function Name:   _StardateToDate()
; Description:   Converts a star date (as used on Star Trek) to the date
; Syntax:   
; Parameter(s):   $iDate - Star date to convert
; Requirement(s):   
; Return Value(s):   Success - Date (See Note)
; Author(s):   GEOSoft
; Modification(s):   
; Note(s):   Currently it only returns the year.  If you want more then figure out the algorithm from _DateToStarDate()
; Example(s):   
;===============================================================================

Func _StardateToDate($iDate)
$iDate = Round($iDate,2)
If StringLeft($iDate, 1) = '-' Then
    $sY = StringLeft($iDate, 4) + 2323
    $iDate = StringTrimLeft($iDate,4)
Else
  $sY = StringLeft($iDate,3) + 2323
  $iDate = StringTrimLeft($iDate,3)
  EndIf
  Return $sY
EndFunc

;===============================================================================
; Function Name:           _Now()
; Description:           Current date and time in several formats and quarter of the year
; Syntax:                 _Now($Format = 0, $tFmt = 12, $wDay = 1, $Mth = 1, $Day = 1, $Yr = 1)
; Parameter(s):         $Format - Return as: 0 = Array (default), 1 = date as formatted string ( <day of week>  <month/day/year>  <time> )
;                               $tFmt  - Time format 12 = 12 hour (default), anything else = 24 hour
;                               $wDay - Day format  1 = full name (default), anything else = abbreviation
;                               $Mth - Month format 1 = full name,  2 = 0 padded numeric value, anything else = abbreviation
;                               $Day - Day format  1 = 0 padded day (default),  anything else = @MDAY
;                               $Yr - Year format  1 = 4 digit, anything else = 2 digit
; Requirement(s):         
; Return Value(s):       If $Format = 0 then returns an array of date and time values where
;                               $array[1] = Day name
;                               $array[2] = Month name or 0 padded number
;                               $array[3] = 0 padded day of month
;                               $array[4] = formatted year eg:(2007 or 07)
;                               $array[5] = formatted time eg:(14:32 or 02:32 PM)
;                               $array[6] = Quarter of the year
;                               $array[7] = Quarter suffix (st quarter, nd quarter, rd quarter, th quarter)
;                               If $Format = 1 Then returns an array with a pre-formatted day / date string where
;                               $array[1] = Day name
;                               $array[2] = Pre- formatted date eg:(02/04/07 or Feb 04/07 or February 04, 2007)
;                               $array[3] = formatted time eg:(14:32 or 02:32 PM)
;                               $array[4] = Quarter of the year
;                               $array[5] = Quarter suffix (st quarter, nd quarter, rd quarter, th quarter)
; Author(s):               GEOSoft
; Note(s):               
; Modification(s):       
; Example(s):             $Dt = _Now(1)
;                               MsgBox(0,'Now',$Dt[1] & ' ' & $Dt[2] & '  ' & $Dt[3])
;===============================================================================

Func _Now($Format = 0, $tFmt = 12, $wDay = 1, $Mth = 1, $Day = 1, $Yr = 1)
    Local $Ap = 'AM', $Rtn[8], $rHour = @Hour, $Qtr, $ap = '', $hDay = StringSplit('Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday','|')
    Local $Tmth, $hMonth = StringSplit('January|February|March|April|May|June|July|August|September|October|November|December', '|')
;;  
    If $wDay = 1 Then
        $Rtn[1] = $hDay[@WDAY]
    Else
        $Rtn[1] = StringLeft($hDay[@WDAY], 3)
    EndIf
    If $Mth <> 2 Then
        If $Mth = 1 Then
            $rMth = $hMonth[@Mon]
        Else
            $rMth = StringLeft($hMonth[@Mon],3)
        EndIf
    Else
        $rMth = @Mon
    EndIf
    If StringLen($rMth) = 1 Then $rMth = '0' & $rMth
    $Rtn[2] = $rMth
    $rDay = @MDay
    If $Day = 1 AND StringLen($rDay) = 1 Then $rDay = '0' & $rDay
    $Rtn[3] = $rDay
    $rYr = @Year
    If $Yr <> 1 Then $rYr = StringRight($rYr,2)
    $Rtn[4] = $rYr
    If $tFmt = 12 Then
        If $rHour > 12 Then $rHour = $rHour - 12
        If $rHour > 11 Then $ap = 'PM'
    EndIf
    $ap = Chr(32) & $ap
    $Rtn[5] = $rHour & ':' & @Min & $ap
    Switch @Mon
        Case 1 To 3
            $Qtr = 1
            $Qs = 'st quarter'
        Case 4 To 6
            $Qtr = 2
            $Qs = 'nd quarter'
        Case 7 To 9
            $Qtr = 3
            $Qs = 'rd quarter'
        Case Else
            $Qtr = 4
            $Qs = 'th quarter'
    EndSwitch
    $Rtn[6] = $Qtr
    $Rtn[7] = $Qs
    If $Format = 1 Then
        $Td = $Rtn[1]
        If $Mth = 2 Then
            $Tmth = $Rtn[2] & '/' & $Rtn[3] & '/' & $Rtn[4]
        Else
            $Tmth = $Rtn[2] & ' ' & $Rtn[3] & ', ' & $Rtn[4]
            If $Yr <> 1 Then $Tmth = StringReplace($Tmth, ', ','/')
        EndIf
        $Ta = $Tmth
        $Ttm = $Rtn[5]
        Redim $Rtn[6]
        $Rtn[1] = $Td
        $Rtn[2] = $Ta
        $Rtn[3] = $Ttm
        $Rtn[4] = $Qtr
        $Rtn[5] = $Qs
    EndIf
    Return $Rtn
EndFunc   ;<==> _Now()

You can just add the two StarDate functions into the Date.au3 file in your include folder but the other two will have to replace the existing functions in Date.au3.

Alternatly create a new Date2.au3 file with all these functions and then add in the rest of the functions from Date.au3

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

I didn't expect star date's to be real.. I just thought they made stuff up anyway. Next thing, you'll tell me about phoning without a cord!! :P Now that would be something..

They did make it up but it was based on an algorithm that someone figured out a long time ago. The formula was probably originally written by some wacko that used a random number generator to create it. I forget now even where I found the algorithm. :whistle:

The algorith to go the otherway is a bit more difficult That's why I just have it returning the year. I couldn't get into doing the math for the rest. Feel free to have a go at it though.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...