delphi how to convert twitter timestamp to TDatetime - delphi

In continue to this question:
How to Convert Twitter Timestamp to DateTime?
what is the code to convert twitter date time stamp to TDateTime?
edit:
StrDateTime(const string;TFormatSettings);
could handle some of it,
now only to figure out how to intoduce new format.

Since we don't have the ParseExact function, you need to parse the components of the timestamp positionally. You could do it with the Copy() function. ex:
TheMonthAsString := Copy(TwitterDate,5,3);
TheDayAsString := Copy(TwitterDate,9,2);
etc..
Convert those pieces to Integers, and then you can use EncodeDateTime (in the DateUtils unit) (Thanks Jens!) to generate a TDateTime.
Summary: Pick the string apart into the individual components of the timestamp, and convert that to a TDateTime using EncodeDateTime or StrToDateTime.

Related

DelphiScript - String to DateTime/to 18 digit TimeStamp

I'm trying to automate some of my recurring tasks. Amongst other jobs, i want to automate the LDAP Account Creation. Since we have employes with fixed-term employment contract, i want to automatically set the deactivation Date of these LDAP-Accounts to the last Day working.
Our tool where I do the automation supports DelphiScript, VBScript and JavaScript. Additionaly it would support PowerShell scripts.
I have a Date variable which i could declare as DateTime or as String.
As DateTime it will look like 44366,3996712847 converted as a String it will look like 19.06.2021 09:36:35. In the end I need to convert one of these values as a 18 digit timestamp, so i can set a Account Expiration Date in LDAP.
A few years ago I did a lot in C#, but nothing with Delphi, JScript and VBScript. I'm also out of C# by now.
My approaches, where I first tried to Convert String to DateTime, look like this:
uses
Classes, SysUtils;
var
befristet: datetime;
timestamp: string;
begin
befristet := GetVarContent('DATA.Befristet');
timestamp := DateTimeToTimeStamp(befristet);
// Writing to Log
LogMessage('VarContent: ' + befristet);
// Returns 0 as script result
Result := 0;
end;

Declare units to Convert from a string variable

I am using the Convert function within delphi to write a simple multi device application that will allow me to convert measurements.
The form consists of 2 Comboboxes and en edit. To select Conversion From, Conversion To and a value.
So in my comboboxes i select the units then i pass the value of the comboboxes to 2 global strings
fromType := cbConvFrom.Selected.Text;
toType := cbConvTo.Selected.Text;
Which return the following.
cbConvFrom returns duCentimeters
cbConvTo returns duMeters
However when i try to pass these values into a Convert function obviously they do not work as i am trying to pass a String as a TConvType, Shown below
conversion := Convert(StrToFloat(editValue.Text), fromType, toType);
How can i use these strings as a TConvType so the code above will work correctly.
Drop using string representation of conversion types and use descriptions instead. Get the descriptions from the registration of the type, using ConvTypeToDescription. Eg:
ComboBox1.Items.Add(ConvTypeToDescription(duMeters));
ComboBox1.Items.Add(ConvTypeToDescription(duCentimeters));
Then you can use the inverse to provide to the conversion function.
DescriptionToConvType(ComboBox1.Items[ComboBox1.ItemIndex], fromType);
DescriptionToConvType(ComboBox2.Items[ComboBox2.ItemIndex], toType);
conversion := Convert(StrToFloat(editValue.Text), fromType, toType);

Extracting Utime on Fast Report 2.5

I wanted to extract Utime 2 types. One for AM ouput second for PM output.I learned how to create my own object but I couldn't extract from the [Utime] object.Could I use FRAC() here?To get the AM part.
here is my current ouput.
My current fast report code.
How could I extract from this object?
Yes, you may use Frac function. Delphi's TDateTime type is double, so Frac function will return time part of TDateTime as double value
begin
if frac([Utime]) < 0.5 then AmTime := [Utime] else PmTime := [Utime];
end

StrToDateDef not working

I want to convert the system date time to a specific format. My system format is dd/mm/yy which i wanted to convert to mm/dd/yyyy and so i am using StrToDateDef. I need to use StrToDateDef only because the date comes as string and if there is a string other than date i will use default date. My code is below
str := '30/01/14';
GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, FmtStngs);
FmtStngs.DateSeparator := '/';
FmtStngs.ShortDateFormat := 'mm/dd/yyyy';
FmtStngs.TimeSeparator := ':';
FmtStngs.LongTimeFormat := 'hh:nn';
date := StrToDateDef(str,01/28/2013,FmtStngs);
I am expecting the date to be of '01/30/2014' but it is coming as '30/01/14'. What is that i am doing wrong?
There are several bugs in this code:
First the TFormatSettings you are passing to the StrToDateDef routine are the format settings for the string you are passing (and not for the datetime variable that comes out, more on that later).
As you are passing '30/01/14' your ShortDateFormat should be 'dd/mm/yyyy' and not 'mm/dd/yyyy'
Then the default value you are passing equals to like 1ms after midnight of 30.12.1899 (because you actually are passing 1 divided by 28 divided by 2014). Use EncodeDate(2013, 1, 28) from DateUtils.pas.
Then you are saying
I am expecting the date to be of '01/30/2014' but it is coming as '30/01/14'.
Well you are looking at a TDateTime variable and it will be formatted according to your local settings of your windows system by the debugger. Nothing more. You don't have a string but a float value (which is what TDateTime is) that is presented as string to you to make it readable.
Also I think the result should be the passed default date because the passed ShortDateFormat does not match the value of the string you are passing (trying to put 30 into the month part).

Delphi inifiles ReadDateTime

The essence in the following:
procedure TForm1.Button1Click(Sender: TObject);
var
cfile: TInifile;
Date1: TDateTime;
begin
Date1 := IncYear(Now, -50);
cfile := TInifile.Create(ExtractFilePath(Application.ExeName) + 'Settings.ini');
try
cfile.WriteDateTime('Main', 'DateTime', Date1);
ShowMessage('Recorded in the ini file ' + DateTimeToStr(Date1));
Date1 := cfile.ReadDateTime('Main', 'DateTime', Now);
ShowMessage('Read from ini file ' + DateTimeToStr(Date1));
finally
cfile.Free;
end;
end;
Entry in the ini file passes without problems. In the file is written to 04-Dec-63 17:28:14. Read also from ini file does not work, the message falls "04-Dec-63 17:28:14 is not a valid date and time".
Windows 7 Enterprise х32, Embarcadero Delphi XE Portable
You've written the date/time to the file as text. And formatted it using the locale settings of the user who created that file. You are doomed to fail to read this file reliably since different users have different locale settings. You need to use a robust format for the date that does not depend on locale.
The two options that seem most natural:
Store as a floating point value, using the underlying representation of TDateTime.
Store as text using a pre-determined format.
For option 1 you'll need to make sure you use a pre-determined decimal separator to avoid the exact same problem you have now! That means you'll need to perform your own conversion between TDateTime and string because the WriteFloat and ReadFloat methods use the global format settings which are locale dependent. There are overloads of FloatToStr and StrToFloat in SysUtils that accept a format settings parameter.
For option 2, the RTL contains various functions to perform date/time conversions using specified formats. There are overloads of DateTimeToStr and StrToDateTime in SysUtils that accept a format settings parameter.
Option 2 is to be preferred if you wish the file to be easily read or edited by a human.

Resources