Function Now in Pascal - 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

Related

How to read a timestamp

I have joined in a Data competition for students. They gave me timestamps of users' interaction. Is it useful?
Some of them are:
1615983880510
1615767552552
1615767577100
1616036788631
What you are looking at is a linux timestamp. It's actually a pretty interesting time representation. In short, it's just a number. And it represents the total number of seconds that have passed since January 1st 1970. A date known as "Unix Epoch Time".
Now, if you want to convert that into a readable date there are many ways to do it in basically every programing language. For example in python you might do something like this:
from datetime import datetime
def printdate(unix):
print(datetime.utcfromtimestamp(unix).strftime('%Y-%m-%d %H:%M:%S'))
BUT! It seems like your dates might actually be in miliseconds. Meaning that you might actually want to divide your dates by 1000 before passing them trough the function. So...
def printdatems(unix):
return printdate(unix/1000)
And there you go!
printdatems(1615983880510) #2021-03-17 12:24:40
printdatems(1615767552552) #2021-03-15 00:19:12
printdatems(1615767577100) #2021-03-15 00:19:37
printdatems(1616036788631) #2021-03-18 03:06:28
That's the output for the example dates you provided.
Of course you can find much more information on Wikipedia:
https://en.wikipedia.org/wiki/Unix_time
It's an intresting read!

Time getting printed as '0x814ff30'

Learning go lang. Might be a basic question.
I have an time.Time object and I wanted to get the epoch time for it. What I write to get that is :
fmt.Println(startTime.Unix)
where startTime is an object of time.Time. Now, I would have expected it to print some big number, something as, 1257894000, but what I get is : 0x814ff30.
Did not understand why? Would be happy to write more in case it is not clear.
Use time.Unix() function
fmt.Println(time.Now().Unix())
//1479454089

MINUTE FROM NUMTODSINTERVAL( formatting

I don't see anything on the oracle docs saying how to change the formatting of the output of a numtodsinterval function so I was hoping you could help
EXTRACT(MINUTE FROM NUMTODSINTERVAL(TOTAL_HOURS, 'HOUR'))
I would like this to come out in a two digit format :'MM', now it works in converting 1.25 into 1:15 but if the time is 8.1333 it displays 8:8
thanks!
Hey sorry yes you're right, I had not given enough information, I did want to display it in typical time fashion HH:MM, and also you are right it does return a number, I did not have a to_char , adding it made this a simple problem to solve so thank you!

Yearless Ruby dates?

Is there a way to represent dates like 12/25 without year information? I'm thinking of just using an array of [month, year] unless there is a better way.
You could use the Date class and hard set the year to a leap year (so that you could represent 2/29 if you wanted). This would be convenient if you needed to perform 'distance' calculations between two dates (assuming that you didn't need to wrap across year boundaries and that you didn't care about the off-by-one day answers you'd get when crossing 2/29 incorrectly for some years).
It might also be convenient because you could use #strftime to display the date as (for example) "Mar-3" if you wanted.
Depending on the usage, though, I think I would probably represent them explicitly, either in a paired array or something like YearlessDate = Struct.new(:month,:day). That way you're not tempted to make mistakes like those mentioned above.
However, I've never had a date that wasn't actually associated with a year. Assuming this is the case for you, then #SeanHill's answer is best: keep the year info but don't display it to the user when it's not appropriate.
You would use the strftime function from the Time class.
time = Time.now
time.strftime("%m/%d")
While #Phrogz answer makes perfect sense, it has a downside:
YearlessDate = Struct.new(:month,:day)
yearless_date = YearlessDate.new(5, 8)
This interface is prone to MM, DD versus DD, MM confusion.
You might want to use Date instead and consider the year 0 as "yearless date" (provided you're not a historian dealing with real dates around bc/ad of course).
The year 0 is a leap year and therefore accommodates every possible day/month duple:
Date.parse("0000-02-29").leap? #=> true
If you want to make this convention air tight, just define your own class around it, here's a minimalistic example:
class YearlessDate < Date
private :year
end
The most "correct" way to represent a date without a year is as a Fixnum between 001 and 365. You can do comparisons on them without having to turn it into a date, and can easily create a date for a given year as needed using Date.ordinal

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;

Resources