convert string to date in Lazarus Pascal - pascal

I am much of a new bee to Pascal programming I have spent the entire day trying to convert a string to a valid date that I can later use to subtract another date from to discover the number of days between the two dates. Can you pls pls help me.
I started with this code to try to convert the first date entered in string format to a date that can be used in a calulation:
program TryDate;
Var
date1: TDateTime;
thedate:string;
Begin
Writeln ('Enter date');
Readln (thedate);
date1:=StrToDate (thedate);
Writeln ('The date is ',date1);
end.
The program's basic structure can be seen here:
Begin
Writeln ('Enter customer last name');
readln (clname);
Writeln ('Enter customer first name');
readln (cfname);
Writeln ('Enter Dvd Title');
readln (dvdtit);
Writeln ('Enter Due Date');
readln (dued);
Writeln ('Enter Actual Date Returned');
readln (adret);
daysover:=adret-dued;
readln;
end.
I am expected to expand the program further but was trying to get this small part to work before trying the other components.
Simple instructions and examples or possible solutions will be greatly appreciated.

You haven't specified what the issue with the code you've got so far is. The only issue I can see, if your actual code is exactly like what you've posted, is that you haven't specified uses sysutils;, like this:
program TryDate;
uses
sysutils;
Var
date1: TDateTime;
thedate: string;
Begin
Writeln ('Enter date');
Readln (thedate);
date1 := StrToDate (thedate);
Writeln ('The date is ',date1);
end.
The StrToDate function is part of the sysutils unit, which you need to include in your program through uses to be able to use its procedures, functions, types etc.

In addition to Andriy, you also don't supply OS information, or what format you enter the date.
This is important because on *nix you have to add clocale to your USES clause to initialize locale systems which also include prefered dateformat.
For really scary stuff there is the function scandatetime that can parse most custom created dates: http://www.freepascal.org/docs-html/rtl/dateutils/scandatetime.html

Related

Function Now in Pascal

I am trying to find an extended description about the function Now(), which returns time. On the official FreePascal site is this description:
Now returns the current date and time. It is equivalent to Date+Time.
I am looking for time which is measured from the epoch January, 1, 1970 00:00:00 in FreePascal.
Can you help me?
Pascal has a very different idea of the "Start of Time" one that I think is more mathematically simple, as well as just makes better sense. At any rate, if I understand your question right, I think you are looking for something like a Unix Timestamp value?
This is how you would get that in FreePascal:
uses sysutils, dateutils;
function GetUnixNow() : Int64;
begin
Result := DateTimeToUnix(Now());
end;
Calling this function will return a 64bit Integer that is the equivalent to (unsigned long)time(NULL); in C11

Very simple procedure statement from school slide. I want to know how it works

create or replace procedure HelloWorld(s varchar)
as
begin
dbms_output.put_line(s);
end;
-------- click execute button now
exec HelloWorld('Hello');
The codes above is from the school slide. The first code is shown as 'procedure created' which means good to go. When I execute it by using 'exec HelloWorld(‘Hello’);', it shows errors. May I ask some of question regarding this code please?
1) Why it does not work when I execute it?
2) I know that 's' is a parameter, and 'varchar' is the datatype for 's'. However, the code requires that print 's'. As I can see, there is nothing to assigned on 's'. Then, how can the code runs the value of 's' in 'dbms_output.put_line(s);'?
3)Can anybody just explain what is the basic function for each single word?

Which date format does VarToDateTime(VarDateFromStr) use?

I've been having problems lately with date conversion lately. Some workstations my application run on don't convert string to date correctly.
I tracked the issue down to VarDateFromStr that doesn't seem to be checking LOCALE_SSHORTDATE to make the conversion. I was wondering if anyone knew what it DID check for the conversion. Or does the different behavior only linked to different DLL version?
GetLocaleStr(GetThreadLocale, LOCALE_SSHORTDATE, 'm/d/yy'); // returns 'dd-MM-yyyy'
FormatDateTime('dd-MM-yyyy', VarToDateTime('05-11-2010')); //returns '11-05-2010'
EDIT:
I've been told that changing the short date format (in the control panel) from 'dd-MM-yyyy' to whatever and back to 'dd-MM-yyyy' fixed the problem. I still have to verify this though.
EDIT2: Kindda forgot to mention, the problem has only been confirmed on WinXP SP3 yet.
Ken, the VarToDateTime function internally calls the VarDateFromStr function wich uses the VAR_LOCALE_USER_DEFAULT constant to format the date.
to determine wich format contains the VAR_LOCALE_USER_DEFAULT you can use this code
var
FormatSettings : TFormatSettings;
begin
GetLocaleFormatSettings(VAR_LOCALE_USER_DEFAULT, formatSettings);
ShowMessage('VarToDateTime is using this format to convert dates '+formatSettings.ShortDateFormat);
end;
now to avoid your problem you can convert your variant value to string and then to datetime using the StrToDateTime function
var
v : variant;
FormatSettings : TFormatSettings;
Begin
v:='05-11-2010';//this is your variant.
FormatSettings.ShortDateFormat:='dd-mm-yyyy';//use this format in the conversion
ShowMessage(FormatDateTime('dd-MM-yyyy', StrToDateTime(V,FormatSettings)));
end;

Checking to see if the Pascal Syntax is correct

Have a bit of a weird one and hopefully someone can help out.
The company I work for is doing an ad and we are looking for a Pascal programmer and we thought we'd incorporate some Pascal code into the ad itself. The only problem is we do not have any knowledge regarding Pascal. So after a little research the code we have come up with is:
Begin
Write('Enter in Name:');
readln(company);
Write('Enter in Australia:');
readln(country);
writeln;{new line}
writeln;{new line}
Writeln('Programming specialists:', 'company' ,'country');
Readln;
End.
And what we are trying to say is:
The person types in Name
And then types in Australia
And then on the screen appears Programming specialists: Name Australia
So is the syntax correct are we missing anything? like comma's or semi-colons etc
It seems fine except for this line:
Writeln('Programming specialists:', 'company' ,'country');
You're printing the strings "company" and "country", but I assume you actually want the values entered by the user. So it should be:
Writeln('Programming specialists:', company ,country);
That seems fine to me. I'm pretty fresh for programming in Pascal - did it in my college course only a couple of months ago. Take into account casablanca's comment though.
Also, make sure you have the top half of the program correct. Like so:
Program advert; {or any other pertinent name}
Uses crt; {This may be unneeded, but we were taught to always put it in}
Var
company, country: string;
Begin
Writeln('Enter in name');
{Writeln or write depends on how you want this to work - write will make the input on the same line (in a terminal) and writeln will make the input on line below}
Readln(company);
Write('Enter in Australia');
Readln(country);
Writeln;
Writeln;
Writeln('Programming specialists: ', company, ' ', country);
Readln;
End.
In regards to the Readln at the end of the program, you might not need to use it. This essentially 'pauses' the program until the user presses the enter key. I noticed that in Windows the command prompt had a habit of closing at the end, making a final readln necessary, but in a Linux terminal, running the program from the terminal, this doesn't happen. Just a side note for you to consider.
You could test this yourself with Free Pascal.
you must remove the ' (single cuotes) character from the company and country variables, try this
var
company,country :string;
Begin
Write('Enter in Name:');
readln(company);
Write('Enter in Australia:');
readln(country);
writeln;{new line}
writeln;{new line}
Writeln('Programming specialists:', company,' ' ,country);
Readln;
End.
you can check this free ebook to learn more about the pascal syntax
Marco Cantù's Essential Pascal

Is there programmatical way to get short day names in windows?

Is there a way to get a 2 character day-name of the week such as MO/TU/WE/TH/FR/SA/SU?
Currently I only know of using FormatDateTime():
"ddd" returns "Fri"
"dddd" returns "Friday"
The main reason is that I want to obtain localized version of the 1 or 2 character day names:
Say FRIDAY in "ddd" would return:
French Windows = "Vendredi", the 2 char would be "VE", note it's the 1st and 2nd char.
Chinese Windows = "星期五", the char would be "五", note it's the 3rd char.
Japanese Windows = "金曜日", the char would be "金", note it's the 1st char.
Edit1:
Currently using Delphi, but i think applies to other languages too.
Edit2:
Simply put, I'm looking to obtain the shorter version of "ShortDayName" through the use of some functions or constants, so that I don't have to build a table of constants containing the 7 day "Shorter" day names for every possible windows language.
I wonder if such functions really exist.
Maybe the calendar 1 or 2 char day names in Outlook are hard-coded themselves, right?
You can get the local names for the days of the week with ShortDayNames and LongDayNames, and you can use DayOfWeek to get the numeric value for the day.
ShortDayNames[Index]; //Returns Fri
or
LongDayNames[Index]; //Returns Friday
The only way I know to shorten them to two chars would be to trim the resulting string
LeftStr(LongDayNames[Index],2);//Returns Fr
So today's Day would be
LeftStr(LongDayNames[DayOfWeek(date)],2); //Returns Fr
Click Here
Depicts the standards in custom date formatting.
You may also use the 'ddd' standard and trim it.
Delphi's routines does nothing special - they just ask OS.
Here is how to to it: Retrieving Time and Date Information. I looked through MSDNs docs and found this.
Note, that there is no really such thing as "2 character day-name" or "3 character day-name" here. There are: native ("long" in Delphi), abbreviated ("short" in Delphi) or short (Vista and above, not present in Delphi) formats.
For example, abbreviated name of the day of the week for Monday: Mon (3 chars, en-US), Пн (2 chars, ru-RU).
So, you probably look for LOCALE_SSHORTESTDAYNAMEX format (which is called "short" by MSDN and doesn't appear in Delphi), but it is availavle only on Vista and above.
For example, the following code:
const
LOCALE_SSHORTESTDAYNAME1 = $60;
procedure TForm1.Button1Click(Sender: TObject);
begin
SetThreadLocale($409);
ShowMessage(
GetLocaleStr(GetThreadLocale, LOCALE_SSHORTESTDAYNAME1, '') + #13#10 +
GetLocaleStr(GetThreadLocale, LOCALE_SABBREVDAYNAME1, '')
);
end;
will show you:
Mo
Mon
But doing this for Russian will output:
Пн
Пн
Hope my edits make answer more clear ;)

Resources