AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Date/Time convert to String (e.g. Seconds > 1:09:15.123)

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
KalleW



Joined: 09 Jun 2005
Posts: 16
Location: Germany

PostPosted: Sun Jun 19, 2005 5:48 pm    Post subject: Date/Time convert to String (e.g. Seconds > 1:09:15.123) Reply with quote

Code:
/******************************************************************************
 *  File:  #Include lib_DateTime.AHK
 *  Autor: KalleW
 *  Datum: 2005-06-19 07:31
 *  Vers:  0.10
 *  AHK:   1.0.31+
 *  OS:    W2000
 *
 *  PnP:   yes; ready for use; no action needed
 *         #Include somewhere in your programs
 *         has not to be the autoexecute section
 *
 ******************************************************************************

 ******************************************************************************
 *  Short Writeup:
 *
 *    strDate := fDate_YYYYMMDD( Date )      "2005-06-19"
 *    strDate := fDate_HHMMSS( Date )        "07:08:21"
 *    strDate := fDate_HHMM( Date )          "07:08"
 *
 *    strTime := fTime_HHMMSS( Seconds )     "321:08:05.123" / "321:08:05"
 *    strTime := fTime_HHMM( Seconds )       "321:08"
 *    strTime := fTime_DDHHMMSS( Seconds )   "13_09:08:05.123" / "13_09:08:05"
 *    strTime := fTime_DDHHMM( Seconds )     "13_09:08"
 *
 ******************************************************************************

 ******************************************************************************
 *  GLOBAL variables:
 *
 *  Set these variables explicitly in your main program
 *  or modify the initialisation function "fTime_Globals" below.
 *  These are the only variables you will see in the Global "ListVars".
 *
 *  vTime_FormatFixed      Typ: Logical
 *      You will find this variable shown as "Fixed" in the examples below.
 *      0: shows at least MM:SS
 *      1: (default) shows always all parts of the output, for:
 *         _HHMMSS:    HH:MM:SS
 *         _DDHHMM:    DD_HH:MM
 *         _DDHHMMSS:  DD_HH:MM:SS
 *
 *  vTime_DDHH_Separator   Typ: String
 *      "_"  (default)
 *      any other character(s): as you like it
 *        if you set the separator to "d" you will e.g. see:
 *        13d09:08:05.123
 *
 ******************************************************************************
 */

/********************************************************************
 *  for testing purposes enable this by removing the blank:
 * /
^D::   ; <Ctrl-D>
;vTime_FormatFixed := 0
vTime := 133*86400 + 7*3600 + 08*60 + 05.12345
vTime := 0*86400 + 321*3600 + 08*60 + 05.12345
vTimeReturn := fTime_ddHHMMss( vTime )
Tooltip MethodReturn: %vTime%  /  %vTimeReturn%
Return
/********************************************************************
 */

/********************************************************************
 *  Call:
 *    fTime_Globals()
 *
 *  Internal use only.
 *  Initialising global variables.
 *
 ********************************************************************
 */
fTime_Globals()
{
   ;#IncludeAgain lib_DateTime_GlobalsDef.AHK
   Global vTime_FormatFixed, vTime_DDHH_Separator

   If ( vTime_FormatFixed = "" )
      vTime_FormatFixed = 1
   If ( vTime_DDHH_Separator = "" )
      vTime_DDHH_Separator = _
   Return
}

/********************************************************************
 *  Call:
 *    strDate := fDate_YYYYMMDD( Date )      "2005-06-19"
 *
 *  Input:
 *    Date            YYYYMMDDHH24MISS-Format (AutoHotKey internal)
 *                    e.g. 20050619012627
 *
 *  Output:
 *    strDate         Format: YYYY-MM-DD (Year-Month-Day)
 *                    as numbers with 2 digits and leading zeros
 *                    example: "2005-06-19"
 ********************************************************************
 */
fDate_YYYYMMDD( Date )
{
   FormatTime, vDate_YYYY, %Date%, yyyy
   FormatTime, vDate_MM  , %Date%, MM
   FormatTime, vDate_DD  , %Date%, dd
   vDate_YYYYMMDD = %vDate_YYYY%-%vDate_MM%-%vDate_DD%
   Return %vDate_YYYYMMDD%
}

/********************************************************************
 *  Call:
 *    strDate := fDate_HHMMSS( Date )        "07:08:21"
 *
 *  Input:
 *    Date            YYYYMMDDHH24MISS-Format (AutoHotKey internal)
 *                    e.g. 20050619010805
 *
 *  Output:
 *    strDate         Format: HH:MM:SS
 *                    (Hour{23..00}:Minute{59..00}:Second{59..00})
 *                    as numbers with 2 digits and leading zeros
 *                    example: "01:08:05"
 ********************************************************************
 */
fDate_HHMMSS( Date )
{
   FormatTime, vDate_HH, %Date%, HH
   FormatTime, vDate_MM, %Date%, mm
   FormatTime, vDate_SS, %Date%, ss
   vDate_HHMMSS = %vDate_HH%:%vDate_MM%:%vDate_SS%
   Return %vDate_HHMMSS%
}

/********************************************************************
 *  Call:
 *    strDate := fDate_HHMM( Date )          "07:08"
 *
 *  Input:
 *    Date            YYYYMMDDHH24MISS-Format (AutoHotKey internal)
 *                    e.g. 20050619010805
 *
 *  Output:
 *    strDate         Format: HH:MM
 *                    (Hour{00..23}:Minute{59..00})
 *                    as numbers with 2 digits and leading zeros
 *                    example: "01:08"
 ********************************************************************
 */
fDate_HHMM( Date )
{
   FormatTime, vDate_HH, %Date%, HH
   FormatTime, vDate_MM, %Date%, mm
   vDate_HHMM = %vDate_HH%:%vDate_MM%
   Return %vDate_HHMM%
}

/********************************************************************
 *  Call:
 *    strTime := fTime_HHMMSS( inpSeconds )
 *
 *  Input:
 *    inpSeconds         Integer or Real
 *
 *  Output:
 *    strTime            Format: HH:MM:SS
 *                       (Hour{xxx..00}:Minute{59..00}:Second{59..00})
 *                       Minute and Second as numbers with 2 digits
 *                         and leading zeros
 *                       Second with 3 decimal digits (optional when real)
 *                       Hour: as many digits as neccessary
 *
 *  Example:
 *
 *  inpSeconds              Fixed   Output String
 *  --------------------    -----   -------------
 *  Integer  1156085        0|1     321:08:05
 *               485        0           08:05
 *               485        1         0:08:05
 *                 5        0            0:05
 *                 5        1         0:00:05
 *  Real     1156085.12345  0|1     321:08:05.123
 *               485.12345  0           08:05.123
 *               485.12345  1         0:08:05.123
 *                 5.12345  0            0:05.123
 *                 5.12345  1         0:00:05.123
 *
 ********************************************************************
 */
fTime_HHMMSS( Seconds )
{
   ;#IncludeAgain lib_DateTime_GlobalsFct.AHK
   Global vTime_FormatFixed, vTime_DDHH_Separator
   fTime_Globals()

   vFormat := A_FormatFloat
   SetFormat FLOAT, 0.3
   vSec60 := Mod( Seconds, 60 )
   vMin60 := Mod( (Floor( Seconds / 60 )), 60)
   vHrsxx :=      (Floor( Seconds / 3600 ))
   SetFormat FLOAT, %vFormat%

   If ( vSec60 < 10 )    ; pad with zeros
      vSec60 = 0%vSec60%
   If ( vMin60 < 10 )
      vMin60 = 0%vMin60%

   vTime_HHMMSS = %vHrsxx%:%vMin60%:%vSec60%
   If ( ! vTime_FormatFixed )
      If ( vHrsxx = 0 )
         vTime_HHMMSS = %vMin60%:%vSec60%
   Return %vTime_HHMMSS%
}

/********************************************************************
 *  Call:
 *    strTime := fTime_HHMM( inpSeconds )     "321:08"
 *
 *  Input:
 *    inpSeconds         Integer or Real
 *
 *  Output:
 *    strTime            Format: HH:MM:SS
 *                       (Hour{xxx..00}:Minute{59..00}:Second{59..00})
 *                       Minute as numbers with 2 digits
 *                         and leading zeros
 *                       Hour: as many digits as neccessary
 *
 *  Example:
 *
 *  inpSeconds              Fixed   Output String
 *  --------------------    -----   -------------
 *  Integer  1156085        0|1     321:08
 *               485        0         0:08
 *               485        1         0:08
 *                 5        0         0:00
 *                 5        1         0:00
 *  Real     1156085.12345  0|1     321:08
 *               485.12345  0         0:08
 *               485.12345  1         0:08
 *                 5.12345  0         0:00
 *                 5.12345  1         0:00
 *
 ********************************************************************
 */
fTime_HHMM( Seconds )
{
   vTime := fTime_HHMMSS( Seconds )
   StringGetPos, vColon, vTime, :,R
   StringLeft vTime_HHMM, vTime, vColon
   Return %vTime_HHMM%
}

/********************************************************************
 *  Call:
 *    strTime := fTime_DDHHMMSS( inpSeconds )     "133_07:08:05"
 *
 *  Input:
 *    inpSeconds      Integer or Real
 *
 *  Output:
 *    strTime         Format: DD_HH:MM:SS
 *                    (Day{xx..0}_Hour{23..00}:Minute{59..00}:Second{59..00})
 *                    Hour, Minute and Second as numbers with 2 digits
 *                      and leading zeros
 *                    Second with 3 decimal digits (optional when real)
 *                    Hour: as many digits as neccessary
 *
 *  Remark:
 *  Although people use very often the colon ":" as separator from Day to Hour
 *  in this case I prefer the underscore "_".
 *  When you use the output format DDHHMM you would see with the example below
 *  "13:09:08" and you would not know that the 13 means 13 days;
 *  (I tried "13d09:08" but you could not read that very easy too.)
 *  When a "13_09:08" is displayed you (hopefully) know the meaning.
 *
 *  Example:
 *
 *  inpSeconds              Fixed   Output String
 *  --------------------    -----   -------------
 *  Integer  1156085        0|1     13_09:08:05
 *               485        0           0:08:05
 *               485        1        0_00:08:05
 *                 5        0           0:00:05
 *                 5        1        0_00:00:05
 *  Real     1156085.12345  0|1     13_09:08:05.123
 *               485.12345  0           0:08:05.123
 *               485.12345  1        0_00:08:05.123
 *                 5.12345  0           0:00:05.123
 *                 5.12345  1        0_00:00:05.123
 *
 ********************************************************************
 */
fTime_DDHHMMSS( Seconds )
{
   ;#IncludeAgain lib_DateTime_GlobalsFct.AHK
   Global vTime_FormatFixed, vTime_DDHH_Separator
   fTime_Globals()

   vFormat := A_FormatFloat
   SetFormat FLOAT, 0.3
   vSec60 := Mod( Seconds, 60 )
   vMin60 := Mod( (Floor( Seconds / 60   )), 60)
   vHrs24 := Mod( (Floor( Seconds / 3600 )), 24)
   vDay   :=       Floor( Seconds / 86400 )
   SetFormat FLOAT, %vFormat%

   If ( vSec60 < 10 )
      vSec60 = 0%vSec60%
   If ( vMin60 < 10 )
      vMin60 = 0%vMin60%
   If ( vHrs24 < 10 )
      vHrs24 = 0%vHrs24%

   vTime_DDHHMMSS = %vDay%%vTime_DDHH_Separator%%vHrs24%:%vMin60%:%vSec60%
   If ( ! vTime_FormatFixed )
      If ( vDay = 0 )
         If ( vHrs24 = 0 )
            vTime_DDHHMMSS = %vMin60%:%vSec60%
         Else
            vTime_DDHHMMSS = %vHrs24%:%vMin60%:%vSec60%
   Return %vTime_DDHHMMSS%
}
/********************************************************************
 *  Call:
 *    strTime := fTime_DDHHMM( inpSeconds )     "133_07:08:05"
 *
 *  Input:
 *    inpSeconds      Integer or Real
 *
 *  Output:
 *    strTime         Format: DD_HH:MM
 *                    (Hour{23..00}:Minute{59..00}:Second{59..00})
 *                    Hour, Minute and Second as numbers with 2 digits
 *                      and leading zeros
 *                    Second with 3 decimal digits (optional when real)
 *                    Hour: as many digits as neccessary
 *
 *  Example:
 *
 *  inpSeconds               Output String
 *  --------------------     -------------
 *  Integer  11516885        133_07:08
 *  Real     11516885.12345  133_07:08
 *
 ********************************************************************
 */
fTime_DDHHMM( Seconds )
{
   vTime := fTime_DDHHMMSS( Seconds )
   vLength := StrLen( vTime ) - 3
   StringGetPos, vColon, vTime, :,R
   StringLeft vTime_HHMM, vTime, vColon
   Return %vTime_HHMM%
}
/******************************************************************************
 * END OF FILE lib_DateTime.AHK
 ******************************************************************************
 */

/******************************************************************************
 *  next 2 functions included for documentation purposes only - no action needed
 ******************************************************************************
 *  File:  #IncludeAgain lib_DateTime_GlobalsDef.AHK
 *  Autor: KalleW
 *  Datum: 2005-06-19 10:33
 *  AHK:   1.0.31+
 *
 *  neccessary for: lib_DateTime.AHK  Vers: 0.10
 *
 *
 *  Containing all GLOBAL variables
 *
 ******************************************************************************
 * /

   Global vTime_FormatFixed, vTime_DDHH_Separator

/******************************************************************************
 * END OF FILE lib_DateTime_GlobalsDef.AHK
 ******************************************************************************
 */

/******************************************************************************
 *  File:  #IncludeAgain lib_DateTime_GlobalsFct.AHK
 *  Autor: KalleW
 *  Datum: 2005-06-19 10:33
 *  AHK:   1.0.31+
 *
 *  neccessary for: lib_DateTime.AHK   Vers: 0.10
 *
 *  IncludeAgain (!) this in your methods for
 *
 *  - getting access to GLOBAL variables
 *  - initialising of default values
 *
 ******************************************************************************
 * /

   #IncludeAgain lib_DateTime_GlobalsDef.AHK
   fTime_Globals()

/******************************************************************************
 * END OF FILE lib_DateTime_GlobalsFct.AHK
 ******************************************************************************
 */
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group