Skip to content

Commit

Permalink
Conversion: func. TryGetMilliseconds - added DefaultTimeUnit parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
jackdp committed Aug 6, 2022
1 parent e573bf7 commit 9400c20
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
20 changes: 14 additions & 6 deletions Base/JPL.Conversion.pas
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ interface
SysUtils,
JPL.Strings;

type
TTimeUnit = (tuMillisecond, tuSecond, tuMinute, tuHour, tuDay {24h});


function BoolToStr(const b: Boolean; TrueStr: string = 'True'; FalseStr: string = 'False'): string;
Expand Down Expand Up @@ -127,7 +129,7 @@ function TryHexToInt(Hex: string; var xInt: Int64): Boolean; overload;
function TryHexToInt(Hex: string; var xInt: integer): Boolean; overload;
function TryHexToByte(Hex: string; var xb: Byte): Boolean;

function TryGetMilliseconds(const NumStr: string; out MilliSeconds: Int64): Boolean;
function TryGetMilliseconds(const NumStr: string; out MilliSeconds: Int64; DefaultTimeUnit: TTimeUnit = tuMillisecond): Boolean;



Expand All @@ -137,7 +139,7 @@ implementation
JPL.TStr;


function TryGetMilliseconds(const NumStr: string; out MilliSeconds: Int64): Boolean;
function TryGetMilliseconds(const NumStr: string; out MilliSeconds: Int64; DefaultTimeUnit: TTimeUnit = tuMillisecond): Boolean;
var
s: string;
dx64: Double;
Expand Down Expand Up @@ -176,7 +178,16 @@ function TryGetMilliseconds(const NumStr: string; out MilliSeconds: Int64): Bool
bDays := True;
s := TStr.TrimFromEnd(s, 'd');
end
else bSeconds := True;
else
begin
// No time unit specified. Using DefaultTimeUnit
case DefaultTimeUnit of
tuSecond: bSeconds := True;
tuMinute: bMinutes := True;
tuHour: bHours := True;
tuDay: bDays := True;
end;
end;

dx64 := 0;
if (TStr.StartsWithHexPrefix(s)) or (Copy(s, 1, 1) = '%') then
Expand All @@ -191,11 +202,8 @@ function TryGetMilliseconds(const NumStr: string; out MilliSeconds: Int64): Bool
else if bHours then dx64 := dx64 * 1000 * 60 * 60
else if bDays then dx64 := dx64 * 1000 * 60 * 60 * 24;

//if dx64 > TIME_MAX then Exit;

MilliSeconds := Int64(Round(dx64));
Result := True;

end;

function TryHexToByte(Hex: string; var xb: Byte): Boolean;
Expand Down
26 changes: 15 additions & 11 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
![](https://tokei.rs/b1/github/jackdp/JPLib?category=files)
![visitors](https://visitor-badge.glitch.me/badge?page_id=jackdp.jplib)

Small library of Pascal units needed to compile my projects published on GitHub.
A library of general-purpose pascal units needed to compile my projects published on GitHub.

Supported Delphi versions: **2009**, **2010**, **XE**, **XE2**, **XE3**, **XE4**, **XE5**, **XE6**, **XE7**, **XE8**, **10.0 Seattle**, **10.1 Berlin**, **10.2 Tokyo**, **10.3 Rio**, **10.4 Sydney**, **11.0 Alexandria**.
**Lazarus**: Required FPC version ~~3.0.4~~ **3.2.0** or newer.
**CodeTyphon**: Tested on CodeTyphon 7.00, 7.10, 7.20 with FPC 3.3.1

---
**FPC** (Free Pascal Compiler): required version ~~3.0.4~~ **3.2.0** or newer.

## Installation

Expand All @@ -19,16 +16,23 @@ The packages are "run-time" type and do not contain any components.

The JPLib package must be installed if you want to install the [JPPack](https://github.com/jackdp/JPPack) components package. If you do not want to install JPPack, you can use JPLib without any installation.

---

## Hash

Some units in the [Hash](./Hash) folder uses **HashLib4Pascal** library from <https://github.com/Xor-el/HashLib4Pascal> and **Wolfgang Ehrhardt's** hashing units from <https://github.com/jackdp/www.wolfgang-ehrhardt.de/tree/master/src/crc-hash/crc_hash>

---

## License

The license for my work: **Public domain**. You can do with my code whatever you want without any limitations.
The license for my work: **public domain**. You can do with my code whatever you want without any limitations.

But in some units I use code from other open source projects, so you should look at the PAS source files and license of the authors of these projects for more information.

## Important changes

<mark>A list of changes that may break existing code.</mark>

But, in some units I use code from other open source projects, so you should look at the PAS source files and license of the authors of these projects for more information.
2022.08.06
JPL.Conversion.pas
```pascal
function TryGetMilliseconds(const NumStr: string; out MilliSeconds: Int64; DefaultTimeUnit: TTimeUnit = tuMillisecond): Boolean;
```
The `DefaultTimeUnit` parameter has been added. It specifies the default time unit to be used if it is not given in `NumStr`. Now the default unit is a **millisecond**, before the change it was a second.
12 changes: 6 additions & 6 deletions src_stats_PAS.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Command line: TxtAn64 *.pas *.dpr *.lpr *.pp -r 1 -s
Processed files: 129
Total size: 1.88 MB
Elapsed time: 00:00:00.072
All lines: 63 523
All blank lines: 12 104
All not blank lines: 51 419
Processed files: 130
Total size: 1.89 MB
Elapsed time: 00:00:00.063
All lines: 64 080
All blank lines: 12 200
All not blank lines: 51 880

0 comments on commit 9400c20

Please sign in to comment.