Advertisement
beeswax

Untitled

Aug 30th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 8.07 KB | None | 0 0
  1. #include-once
  2.  
  3. ;===============================================================================
  4. ;
  5. ; AutoIt Version: 3.2.3.0
  6. ; Language:       English
  7. ; Description:    Dll wrapper functions for dealing with Unix timestamps.
  8. ; Requirement(s): CrtDll.dll
  9. ; Notes:          If CrtDll.dll is not available then functions will return false
  10. ;                 and set @error = 99.
  11. ;
  12. ;===============================================================================
  13.  
  14.  
  15. ;===============================================================================
  16. ;
  17. ; Description:      _TimeGetStamp - Get current time as Unix timestamp value.
  18. ; Parameter(s):     None
  19. ; Return Value(s):  On Success - Returns Unix timestamp
  20. ;                   On Failure - Returns False, sets @error = 99
  21. ; Author(s):        Rob Saunders (admin@therks.com)
  22. ; User Calltip:     _TimeGetStamp() - Get current time as Unix timestamp value. (required: <_UnixTime.au3>)
  23. ;
  24. ;===============================================================================
  25.  
  26. Func _TimeGetStamp()
  27.     Local $av_Time
  28.     $av_Time = DllCall('CrtDll.dll', 'long:cdecl', 'time', 'ptr', 0)
  29.     If @error Then
  30.         SetError(99)
  31.         Return False
  32.     EndIf
  33.     Return $av_Time[0]
  34. EndFunc
  35.  
  36. ;===============================================================================
  37. ;
  38. ; Description:      _TimeMakeStamp - Create Unix timestamp from input values.
  39. ; Syntax:           _TimeMakeStamp( [ second [, minute [, hour [, day [, month [, year [, isDST ]]]]]]] )
  40. ; Parameter(s):     Second - Second for timestamp (0 - 59)
  41. ;                   Minute - Minute for timestamp (0 - 59)
  42. ;                   Hour   - Hour for timestamp (0 - 23)
  43. ;                   Day    - Day for timestamp (1 - 31)
  44. ;                   Month  - Month for timestamp (1 - 12)
  45. ;                   Year   - Year for timestamp (1970 - 2038)
  46. ;                   * All the above values default to the 'Default' keyword, where the current
  47. ;                     time/date value will be used.
  48. ;                   IsDST  - Set to 1 during Daylight Saving Time (DST)
  49. ;                          - Set to 0 not during DST
  50. ;                          - Set to -1 if unknown, function will try to figure it out
  51. ;                          - Default is -1
  52. ; Return Value(s):  On Success - Returns Unix timestamp
  53. ;                   On Failure - Parameter error, returns -1
  54. ;                              - Dll error, returns False, sets @error = 99
  55. ; Notes:            The function will try and calculate dates for numbers outside of the
  56. ;                   usual range.
  57. ;                   For example: _TimeMakeStamp(0, 0, 0, 32, 1, 1995)
  58. ;                   32nd day of January? Obviously that's not a valid date, but the function
  59. ;                   automatically calculates this to be February 1st. A date of 0 will return
  60. ;                   the last day of the previous month.
  61. ; User CallTip:     _TimeMakeStamp($i_Sec = Default, $i_Min = Default, $i_Hour = Default, $i_Day = Default, $i_Mon = Default, $i_Year = Default, $i_IsDST = -1) - Create a UNIX timestamp from input values. (required: <_UnixTime.au3>)
  62. ; Author(s):        Rob Saunders (admin@therks.com)
  63. ;
  64. ;===============================================================================
  65. Func _TimeMakeStamp($i_Sec = Default, $i_Min = Default, $i_Hour = Default, $i_Day = Default, $i_Mon = Default, $i_Year = Default, $i_IsDST = -1)
  66.     Local $struct_Time, $ptr_Time, $av_Time
  67.     $struct_Time = DllStructCreate('uint;uint;uint;uint;uint;uint;uint;uint;uint')
  68.  
  69.     Select
  70.         Case $i_Sec = Default
  71.             $i_Sec = @SEC
  72.             ContinueCase
  73.         Case $i_Min = Default
  74.             $i_Min = @MIN
  75.             ContinueCase
  76.         Case $i_Hour = Default
  77.             $i_Hour = @HOUR
  78.             ContinueCase
  79.         Case $i_Day = Default
  80.             $i_Day = @MDAY
  81.             ContinueCase
  82.         Case $i_IsDST = Default
  83.             $i_IsDST = -1
  84.     EndSelect
  85.     ; The following is done because the mktime function demands
  86.     ; that the month be in 0-11 (Jan = 0) format instead of 1-12.
  87.     Select
  88.         Case $i_Mon = Default
  89.             $i_Mon = (@MON - 1)
  90.         Case $i_Mon <> Default
  91.             $i_Mon -= 1
  92.     EndSelect
  93.     ; The following is done because the mktime function expects the year in format
  94.     ; (full year - 1900), thus 99 = 1999 and 100 = 2005. The function will try
  95.     ; to figure out what year the user is trying to use. Thus if the function recieves
  96.     ; 70, it's untouched, but if the user gives 1970, 1900 is subtracted automatically.
  97.     ; Any year above 99 has 1900 automatically subtracted.
  98.     Select
  99.         Case $i_Year = Default
  100.             $i_Year = (@YEAR - 1900)
  101.         Case $i_Year < 70
  102.             $i_Year += 100
  103.         Case $i_Year > 99
  104.             $i_Year -= 1900
  105.     EndSelect
  106.  
  107.     DllStructSetData($struct_Time, 1, $i_Sec)
  108.     DllStructSetData($struct_Time, 2, $i_Min)
  109.     DllStructSetData($struct_Time, 3, $i_Hour)
  110.     DllStructSetData($struct_Time, 4, $i_Day)
  111.     DllStructSetData($struct_Time, 5, $i_Mon)
  112.     DllStructSetData($struct_Time, 6, $i_Year)
  113.     DllStructSetData($struct_Time, 9, $i_IsDST)
  114.  
  115.     $ptr_Time = DllStructGetPtr($struct_Time)
  116.     $av_Time = DllCall('CrtDll.dll', 'long:cdecl', 'mktime', 'ptr', $ptr_Time)
  117.     If @error Then
  118.         SetError(99)
  119.         Return False
  120.     EndIf
  121.  
  122.     Return $av_Time[0]
  123. EndFunc
  124.  
  125. ;===============================================================================
  126. ;
  127. ; Description:      _StringFormatTime - Get a string representation of a timestamp
  128. ;                   according to the format string given to the function.
  129. ; Syntax:           _StringFormatTime( "format" [, timestamp [, max length ]] )
  130. ; Parameter(s):     Format String - A format string to convert the timestamp to.
  131. ;                                   See notes for some of the values that can be
  132. ;                                   used in this string.
  133. ;                   Timestamp     - A timestamp to format, possibly returned from
  134. ;                                   _TimeMakeStamp. If left empty, default, or less
  135. ;                                   than 0, the current time is used. (default is -1)
  136. ;                   Max Length    - Maximum length of the string to be returned.
  137. ;                                   Default is 255.
  138. ; Return Value(s):  On Success - Returns string formatted timestamp.
  139. ;                   On Failure - Returns False, sets @error = 99
  140. ; Requirement(s):   _TimeGetStamp
  141. ; Notes:            The date/time specifiers for the Format String:
  142. ;                       %a  - Abbreviated weekday name (Fri)
  143. ;                       %A  - Full weekday name (Friday)
  144. ;                       %b  - Abbreviated month name (Jul)
  145. ;                       %B  - Full month name (July)
  146. ;                       %c  - Date and time representation (MM/DD/YY hh:mm:ss)
  147. ;                       %d  - Day of the month (01-31)
  148. ;                       %H  - Hour in 24hr format (00-23)
  149. ;                       %I  - Hour in 12hr format (01-12)
  150. ;                       %j  - Day of the year (001-366)
  151. ;                       %m  - Month number (01-12)
  152. ;                       %M  - Minute (00-59)
  153. ;                       %p  - Ante meridiem or Post Meridiem (AM / PM)
  154. ;                       %S  - Second (00-59)
  155. ;                       %U  - Week of the year, with Sunday as the first day of the week (00 - 53)
  156. ;                       %w  - Day of the week as a number (0-6; Sunday = 0)
  157. ;                       %W  - Week of the year, with Monday as the first day of the week (00 - 53)
  158. ;                       %x  - Date representation (MM/DD/YY)
  159. ;                       %X  - Time representation (hh:mm:ss)
  160. ;                       %y  - 2 digit year (99)
  161. ;                       %Y  - 4 digit year (1999)
  162. ;                       %z, %Z  - Either the time-zone name or time zone abbreviation, depending on registry settings; no characters if time zone is unknown
  163. ;                       %%  - Literal percent character
  164. ;                   The # character can be used as a flag to specify extra settings:
  165. ;                       %#c - Long date and time representation appropriate for current locale. (ex: "Tuesday, March 14, 1995, 12:41:29")
  166. ;                       %#x - Long date representation, appropriate to current locale. (ex: "Tuesday, March 14, 1995")
  167. ;                       %#d, %#H, %#I, %#j, %#m, %#M, %#S, %#U, %#w, %#W, %#y, %#Y  - Remove leading zeros (if any).
  168. ;
  169. ; User CallTip:     _StringFormatTime($s_Format, $i_Timestamp = -1, $i_MaxLen = 255) - Get a string representation of a timestamp according to the format string given to the function. (required: <_UnixTime.au3>)
  170. ; Author(s):        Rob Saunders (admin@therks.com)
  171. ;
  172. ;===============================================================================
  173. Func _StringFormatTime($s_Format, $i_Timestamp = -1, $i_MaxLen = 255)
  174.     Local $struct_Time, $ptr_Time, $av_Time, $av_StrfTime
  175.  
  176.     If $i_Timestamp = default OR $i_Timestamp < 0 Then
  177.         $i_Timestamp = _TimeGetStamp()
  178.     EndIf
  179.     $ptr_Time = DllCall('CrtDll.dll', 'ptr:cdecl', 'localtime', 'long*', $i_Timestamp)
  180.     If @error Then
  181.         SetError(99)
  182.         Return False
  183.     EndIf
  184.  
  185.     $av_StrfTime = DllCall('CrtDll.dll', 'int:cdecl', 'strftime', _
  186.         'str', '', _
  187.         'int', $i_MaxLen, _
  188.         'str', $s_Format, _
  189.         'ptr', $ptr_Time[0])
  190.     Return $av_StrfTime[1]
  191. EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement