Laravel 5 and Carbon discrepancy on Forge - laravel-5

Hopefully I'm not mad and I'm only missing something. I have a project on Laravel 5.0 and I have a requestExpired function called every time I have an incoming request. Now, to calculate the difference between current time on the server and the timestamp within the request I'm using:
$now = Carbon::now('UTC');
$postedTime = Carbon::createFromTimestamp($timestamp, 'UTC');
For some reason request is always rejected because it's expired. When I debug these two lines from above and just dump data, I get:
REQUEST'S TIMESTAMP IS: 1423830908279
$NOW OBJECT: Carbon\Carbon Object
(
[date] => 2015-02-13 12:35:08.000000
[timezone_type] => 3
[timezone] => UTC
)
$POSTEDTIME OBJECT: Carbon\Carbon Object
(
[date] => 47089-05-28 09:37:59.000000
[timezone_type] => 3
[timezone] => UTC
)
Any ideas why $postedTime is so wrong? Thanks!

To answer my own question: for some strange reason webhook calls from remote API have 13 digits long timestamps and that's why my dates were so wrong.

Related

Laravel Importing CSV throws error on rows with dates

Import.php
...
return new Statement([
'account_number' => $row['accountno'],
'account_name' => $row['name'],
'reading_date' => \Carbon\Carbon::createFromFormat('m/d/Y', $row['billdate']),
'due_date' => \Carbon\Carbon::createFromFormat('m/d/Y', $row['duedate']),
]);
...
Error:
Illuminate\Database\QueryException PHP 8.1.6 9.37.0
SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: '10/18/2022' for column `mubsdb`.`statements`.`due_date` at row 1
INSERT INTO
`statements` (`due_date`, `reading_date`)
VALUES
( 10 / 18 / 2022, 10 / 03 / 2022),
(
10 / 18 / 2022,
10 / 03 / 2022
),
( 10 / 18 / 2022, 10 / 03 / 2022),
( 10 / 18 / 2022, 10 / 03 / 2022),
(10/18/2022, 10/03/2022), (10/18/2022, 10/03/2022), (10/18/2022, 10/03/2022),
DB Structure:
Name Type Null Default
reading_date date Yes NULL
due_date date Yes NULL
I'm trying to import and save csv rows to my DB but I get error with dates. I tried \Carbon\Carbon::createFromFormat('m/d/Y', $row['billdate']) and \Carbon\Carbon::parse($row['billdate'])->format('Y-m-d') but neither seems to work
weirdly, this worked.
'reading_date' => $row['billdate'] ? \Carbon\Carbon::createFromFormat('m/d/Y', $row['billdate'])->format('m/d/Y') : null,
'due_date' => $row['duedate'] ? \Carbon\Carbon::createFromFormat('m/d/Y', $row['duedate'])->format('m/d/Y') : null,
If you're using the newest version of laravel-excel, then you'll notice at this page a date column is exported using Date::dateTimeToExcel:
// ...
Date::dateTimeToExcel($invoice->created_at),
// ...
That is because date is stored as numbers in excel, thus a datetime object needs to be converted first in order to show the value correctly.
This rule also exists in import. So personally I would add a rule in the import class to make sure that the date we're receiving is a number (which actually a date format in excel):
use Maatwebsite\Excel\Concerns\WithValidation;
class MyImport implements WithValidation
{
public function rules(): array
{
return [
'created_at' => ['numeric'],
];
}
}
And then, when about to importing the data using model, convert the number to datetime before modifying it with Carbon:
use PhpOffice\PhpSpreadsheet\Shared\Date;
// ...
return new Import([
// ...
'created_at' => Carbon::instance(Date::excelToDateTimeObject($row['created_at'])),
// or any other Carbon methods
// ...
]);

looking for filename having timestamp as today -1

ftp.gettextfile('ReceiveLog_ABC-4444_yyyymmdd.log','upsmi.csv')
Today is 20161103.
How can I get ReceiveLog_ABC-4444_20161102.log?
I want to know if date were 20161201, how to look for 20161130 file?
You can use the Date library to handle this:
require 'date'
date_format = '%Y%m%d'
date = Date.parse('20161103', date_format)
# => #<Date: 2016-11-03 ((2457696j,0s,0n),+0s,2299161j)>
previous_date = date - 1
# => #<Date: 2016-11-02 ((2457695j,0s,0n),+0s,2299161j)>
previous_date.strftime(format)
# => "20161102"
This handles incrementing and decrementing to properly account for the lengths of the months and the start/end of the year.

how to get previous day in UTC format using ruby

How to get previous day on UTC time using ruby?
Currently I'm using Time.now.utc.iso8601 to get UTC time format in ruby, I need previous day in same UTC format. Can someone help me with sample code to get previous day?
> Time.now.utc
=> 2015-03-22 19:00:46 UTC
> Time.now.utc - 86400
=> 2015-03-21 19:00:51 UTC
> (Time.now.utc - 86400).iso8601
=> "2015-03-21T19:00:59Z"

Ruby on Rails 3: Why is the conversion from Date to seconds different from Time to seconds

In Ruby I am trying to convert a Date into a format that is usable by the HighCharts JavaScript charting library. Odd thing is when I convert the Date to seconds it converts differently than when I convert a Time to seconds and differently when I convert a DateTime to seconds. Due to this difference in conversion the dates displayed on the Graph can be as much as 1 date behind.
I am sure this has something to do with Rails and how it handles conversion from UTC to Local. If someone could explain to me the details I would greatly appreciate it.
In my examples below I use the same date '2011/05/02' but the seconds come out to be different.
Examples:
Date.new(2011, 5, 2).to_time.to_i * 1000
=> 1304265600000
=> 05/01/2011
Time.utc(2011, 5, 2).to_i * 1000
=> 1304294400000
=> 05/02/2011
Date.new(2011, 5, 2).to_datetime.to_i * 1000
=> 1304294400000
=> 05/02/2011
ruby-1.9.2-p180 :106 > Time.utc(2011, 5, 2)
=> 2011-05-02 00:00:00 UTC
ruby-1.9.2-p180 :107 > Date.new(2011, 5, 2).to_time
=> 2011-05-02 00:00:00 +0300
Date.to_time generates Time with timezone. That's your difference.
The quick fix/hack that popped instantly into my mind is:
Date.new(2011, 5, 2).to_time.utc.midnight
Edit:
http://apidock.com/rails/Date/to_time
Date.new(2011, 5, 2).to_time(:utc)

ROR + Ruby Date From XML API

By using XML API, I got date-time as "2008-02-05T12:50:00Z". Now I wanna convert this text format into different format like "2008-02-05 12:50:00". But I am getting proper way.
I have tried this one :: #a = "2008-02-05T12:50:00Z"
Steps
1. #a.to_date
=> Tue, 05 Feb 2008
2. #a.to_date.strftime('%Y')
=> "2008"
3. #a.to_date.strftime('%Y-%m-%d %H:%M:%S')
=> "2008-02-05 00:00:00
Suggest some thing ?
The to_date method converts your string to a date but dates don't have hours, minutes, or seconds. You want to use DateTime:
require 'date'
d = DateTime.parse('2008-02-05T12:50:00Z')
d.strftime('%Y-%m-%d %H:%M:%S')
# 2008-02-05 12:50:00
Use Ruby's DateTime:
DateTime.parse("2008-02-05T12:50:00Z") #=> #<DateTime: 2008-02-05T12:50:00+00:00 (353448293/144,0/1,2299161)>
From there you can output the value in any format you want using strftime. See Time#strftime for more info.

Resources