Both strtotime and mktime are outputting an incorrect timestamp and it's driving me mad.
If I add the following strtotime('2012-10-09');
I get 1349701200
Which is actually
Mon, 08 Oct 2012 13:00:00 GMT
I'm using my localhost, running MAMP. I'm assuming it's a server timezone issue, or something, but I don't understand why, or how to fix it.
Any help?
strtotime uses default timezone to interpret the string. If you want different timezone you could specify it explicitly or change it for all calls:
<?php
if (date_default_timezone_get()) {
echo 'date_default_timezone: ' . date_default_timezone_get()."\n";
}
echo strtotime('2012-10-09')."\n"; # default timezone
echo strtotime('2012-10-09 UTC')."\n";
date_default_timezone_set('UTC');
echo strtotime('2012-10-09')."\n";
?>
Output
date_default_timezone: Europe/London
1349737200
1349740800
1349740800
POSIX timestamp counts number of seconds since 1970-01-01 00:00:00 UTC. For example, midnight (00:00) in New York may be 20:00 in UTC at this time of year (the same POSIX timestamp). But 00:00 in UTC and 00:00 in New York correspond to different moments in time (different POSIX timestamps). Local clocks follow the Sun usually (roughly speaking) and even if it is night where you are; the Sun shines somewhere on Earth.
Related
I'm trying to implement a PowerShell script to compare DateTime from certificate file(Jave Keystore).
However, the DateTime format that I extract from keystore file is quite complex as example below.
Mon Mar 13 06:40:26 CDT 2023
Sat Sep 18 20:41:47 CDT 2027
It includes time and timezone in the String but I actually need only date like 13-Mar-2023.
Could anyone help suggest how I return this String to be DateTime for comparison?
Thanks a lot.
You can use the [datetime]::ParseExact() method for this:
$dateString = 'Mon Mar 13 06:40:26 CDT 2023'
$date = [datetime]::ParseExact($dateString, 'ddd MMM dd HH:mm:ss CDT yyyy', [cultureinfo]'en-US')
$date.ToString('dd-MMM-yyyy')
Result:
13-Mar-2023
CDT means Central Time Zone (UTC - 6), switched to DayLight Saving Time --> Central Daylight Time (CDT) which is five hours behind UTC
How should dates be entered in Jekyll?
I would like to enter the date "August 2018" in a YAML file to be used in Jekyll. I find lots of information on how to format already-existing dates, but pretty much nothing on how to enter them.
The best I have managed to find is Date formatting, which implies that dates entered in ISO 8601 format should be valid. If I run with this, then Wikipedia explicitly states
"2004-05" is a valid ISO 8601 date, which indicates May (the fifth
month) 2004.
This implies that "August 2018" could be entered as 2018-08.
However, when I use my YAML file my_data.yml in my _data folder
date: 2018-08
then Jekyll doesn't recognize it as a date as
{{ site.data.my_data.date | time: '%B %Y' }}
outputs "2018-08" and not "August 2018".
TL;DR: Enter YYYYMM dates such as "August 2018" as Aug 2018.
Searching through the Jekyll repo, I found the date filters. The Ruby on Rails method .to_formatted_s (source) seem to be key to most of them. In the source to that method dates are written as Tue, 04 Dec 2007 00:00:00 +0000 from which I guessed that I should write Aug 2018. Doing so in my_data.yml, the code outputs the expected “August 2018”.
Executing a simple Perl script
use Time::Piece;
my $t = Time::Piece->strptime('08:00 PM AST', "%I:%M %p %Z");
I got the following error: Error parsing time at /usr/local/lib/perl5/Time/Piece.pm line 469.
Is this a bug in the library or there is something wrong with the above code? When I remove AST time-zone from the input string, it works, but when time-zone is left, it fails.
I don't know what exact time-zone will be in input string, so I cannot adjust that part on my end. AST (see Wikipedia) is a proper abbreviation for Atlantic Time Zone, so it should work. But it does not!
The time zone field is ambiguous and cannot be parsed. For instance, CST is the abbreviation for China Standard Time, Central Standard Time, and Cuba Standard Time.
The module documentation says that the strptime method is from FreeBSD, where the %Z format accepts either the local time zone or GMT and nothing else. This may be true of strptime, but I can confirm only that, where I am located, GMT is acceptable while UTC and AST are not.
The solution I would recommend is to preprocess your time strings, replacing the time zone abbreviation with an unambiguous time zone offset. For instance AST (assuming you meant Atlantic Standard Time and not Arabia Standard Time) would be replaced with -0400, since it is four hours behind UTC. Then you can parse it with a %z format specifier and get the correct result.
use Date::Parse;
my $t = str2time('08:00 PM AST');
I am writing a script in PHP which will convert Numbers file into HTML table, but I can not figure out which format is used for date storage. The date cell tag looks like
<sf:d
sf:s="SFTCellStyle-128"
sf:w="84.074219"
sf:h="14"
sf:cell-date="371397519.99999952" />
so the date must be in sf:cell-date attribute, but I can not figure out how to convert it into human readable format. Any ideas? I have never seen date value as float number.
As written in a comment, it is the number of seconds since 01/01/2001 at 00:00:00. Equipped with that knowledge and because this goes hand in hand with the UNIX Epoch all you need to do is to define and use the offset. It should be compatible with nearly every of the existing PHP date functions and objects, for example with date:
define('CELL_DATE_EPOCH_OFFSET', 978307200);
$sf_cell_date = 371397519.99999952;
echo date('r', CELL_DATE_EPOCH_OFFSET + $sf_cell_date);
The output of this little script is (in my timezone):
Mon, 08 Oct 2012 15:58:39 +0200
I hope this is helpful. 978307200 is the unix timestamp for 01/01/2001 00:00:00 UTC, you can get with PHP for example with the following code-example:
$base = new DateTime('01/01/2001 00:00:00 UTC');
echo $base->getTimestamp(), "\n";
in case that was your problem.
im trying to convert a time stamp in seconds from Jan 01 1901 to the current date.
for example,
time stamp 3465468225 translate to a date in 2010. does anyone know of a way to do this in unix/linux? thanks.
In R, it is as simple as this:
> as.POSIXct(3465468225, origin="1901-01-01")
[1] "2010-10-25 15:03:45 CDT"
>
This uses appropriate wrappers around C-level calls gmtime() / localtime() plus time formatting via strftime().
On GNU and POSIX systems you can obtain the date string using seconds since Epoch (1970-01-01 00:00:00 UTC) as:
$ date --date=#1289495920
Thu Nov 11 12:18:40 EST 2010
You should handle the offset since Jan 01 1901 yourself.