Sorting records based on modified timestamp? - ruby-on-rails-3

I am trying to sort a list of records that have been created using a screen scraping script. The script adds the following style of date and time (timestamp) to each record:
13 Jan 14:49
The script runs every 15 minutes, but if I set the sort order to 'time DESC' it doesn't really make sense because it lists the records as follows:
13 Jan 14:49
13 Jan 12:32
13 Jan 09:45
08 Feb 01:10
07 Feb 23:31
07 Feb 06:53
06 Feb 23:15
As you can see, it's listing the first figure correctly (the day of the month in number form) but it's putting February after January. To add to the confusion it's putting the latest date in February at the top of the February section.
Is there a better way of sorting these so they are in a more understandable order?

If you are storing the values in a database, simply use the column type datetime when creating the field. The database will treat the field as time and will sort the values chronologically.
Otherwise, if you are storing the values elsewhere, for example in a flat file, convert the formatted time to unix time. Unix time is an integer, thus you can sort it easier.
Time.parse("13 Jan 09:45").to_i
# => 1326444300
Time.parse("08 Feb 01:10").to_i
# => 1328659800
You can always convert a unix time to a Time instance.
Time.at(1328659800).to_s
# => "2012-02-08 01:10:00 +0100"

Related

Awk Sort by Today's date (not alphabetical)

I have the following lines in a file. The first columns look like this (these are dates and time):
May 29 23:14:39
Dec 20 19:45:15
Nov 3 13:15:19
Sep 8 10:34:15
Mar 9 18:39:20
Jan 17 19:34:59
I would like to use awk to sort it by today's date. For example today is November 03 (Nov 3). Tomorrow it will be November 04 (Nov 4). The dates will be there accordingly as the days change and the file changes along. Now I would like the first line to be the today's date all the time / always. Is that even possible to sort it out like this using either awk, sed and the like?
Alphabetical sort does not work because it does 1,2,3,4 and so on and if today is January 20 for example, my first line will be January 1 anyway (not January 20) because it will do alphabetical order / sort.
Would appreciate any help / suggestions / pointers. Many thanks in advance.
P.S. Let me edit this as asked by Cyrus.
Well, the following code actually works, but... sed '/reject/!d' file.txt | sort -r -k2'... It goes like this: November 1, November, 2, November 3, and then October 31. I guess it grabs numbers like so 1,2,3,31. If I could get it to solve this it would also work. Thanks.
My desired output is to sort it by date with the current today's date to be the first line in my file all the time. However, the code above would also work for me if I could get it to count November 1, November 2, November 3, November 4 (instead of November 1, November 2, November 3, October 31).
P.S.S. That's another edit as per Ed's Morton request.
The dates are all there. I do not need to add anything. The only requirement is for today's date line to be the first one in that file all the time and then sort it out backwards in descending order. For example today is November 05, 2022 and this is the first line in the file like this Nov 5 12:45:89. Then all the other lines are for November 4, 3, 2, 1. Then all the other lines are for October 31, 30, 29, 28 and so on. It is supposed to go backwards and it has to start from the current date, that is from "today" all the time. For example tomorrow the first line has to be Nov 6 and everything else backwards. Then the day after tomorrow the first line has to be Nov 7 and everything else backwards and so on and so forth. I do not need to truncate anything. I do not need to add anything. I do not need to delete anything. All the data has to stay there and go backwards starting from the "current" day, whether it is today, tomorrow, after tomorrow and so on. I hope it's clear enough. Thanks.
It sounds like this, using any POSIX compliant versions of the tools, might be what you want but without expected output in the question it's a guess:
$ cat tst.sh
#!/usr/bin/env bash
awk -v today="$(date +'%F')" '
BEGIN {
OFS = "\t"
split(today,d,/-/)
year = d[1]
}
{
mthNr = index(" JanFebMarAprMayJunJulAugSepOctNovDec",$1) / 3
date = sprintf("%04d-%02d-%02d", year, mthNr, $2)
}
date <= today {
print date, $3, $0
}
' "${#:--}" |
sort -rk1,2 |
cut -f3-
$ ./tst.sh file
Nov 3 13:15:19
Sep 8 10:34:15
May 29 23:14:39
Mar 9 18:39:20
Jan 17 19:34:59
The above uses a DSU approach to solve the problem. It's sorting on both date and time so for the same date the output is similarly ordered by the time that day.

Convert string to date SSRS

I have issue converting parameter string to date.
I have list of data as:
Jul 28 2017 Call
Jan 8 2018 SMS
Apr 24 2018 Call
Jul 2 2018 E-Mail
Jul 13 2018 Call
Oct 1 2018 Call
Nov 27 2018 E-Mail
Dec 31 2018 Call
Jan 1 2019 SMS
Apr 1 2019 SMS
Jun 4 2019 SMS
I want them to be presented as eg. 06/04/2019 if possible
I tried LSet(Format(Parameters!DateInfo.Label,"MM/dd/yyyy"),12) in expression, but when I run the report it is showing me just like this MM/dd/YYYY.
For your parameter, you probably should be using a dataset for the dates with the Value set to use a date field and the Label using the string representation of the date (CONVERT(CHAR(10), THEDATE, 110) AS DATE_LABEL).
I'm guessing the user isn't typing the parameter values in which means that there's already a dataset for the dates. Add another column to the dataset with the date as a date field to use as the Value while using the text as the Label.
If you cannot fix that and still need to convert the text field into a date, you could use the CDATE function which will convert text into a date field.
=Format(CDATE(LEFT(Parameters!DateInfo.Label, 12)),"MM/dd/yyyy")

Getting error ERROR: date/time field value out of range: "31 APR 2001 in Postgres

I have a varchar field where dates are stored in the database where I need data from with values like 31 APR 2001 I run the following query
select date(trim(contact_date)) from clients where date(trim(contact_date)) < '2002-03-12';
Whenever it hits 31 APR 2001 I get the error ERROR: date/time field value out of range: "31 APR 2001
Are there any workaround so that I won't get this error. Since all other dates get return perfectly.
The version of postgres I use is
PostgreSQL 8.1.22
There are 30 days in the month of April.
there is no 31 in April month, prevent it from hitting 31 check it before query

Convert GMT dates to BST on the fly in SQL Server 2000

I have a table which contains a value for every 30 minutes in a month, e.g.
20/03/2010 00:00 12
20/03/2010 00:30 14
etc
All of this data is stored in GMT
I need to do a select on this table for the data in bst/clock time
for example
select *
from tbl
where dt between '01 April 2010' and '30 April 2010 23:30'
when in BST as the date range above is the dates need to be converted
I also need a way of taking the changeover hour and the end of March and October into account
Unfortunatly I cannot upgrade SQL server
Is there any way that I can do this in SQL for SQL Server 2000?
something like a function I could do?
select *
from tbl
where fnConvertToClockTime(dt) between '01 April 2010' and '30 April 2010 23:30'
When in GMT the function would return the exact date from the table
Cheers
Rupert
I have the same problem and am investigating the use of Calendar tables for such convserions. Store all dates in UTC time then convert as necessary, I'm probably going to create VIEWS for the different time zones to avoid parameter passing but I guess a stored procedure would also work.
I found some useful information here: how-do-i-convert-local-time-to-utc-gmt-time
And an associated article on Calendar tables here: why-should-i-consider-using-an-auxiliary-calendar-table

When was this clock bought?

The clock on the gym wall also shows the day name and the day of the month. This morning it showed Tuesday - 23.
The day obviously rotates through a cycle of 7 - and showed "Tuesday" correctly. The day of the month, though, presumably rotates through a cycle of 31 and showed "23" incorrectly - today is the 1st December (ie. not the 31st November). So this error has been slowly accruing over time.
Given the assumption that no-one has ever reset the clock, what's the most elegant, quick, optimised way of suggesting some of the possible dates for when this clock was bought brand new.
(Bonus credit for showing when the clock will again show the correct day/number combination.)
01-Oct-17 is when the clock will again show the correct day/number combination.
The day of the week (i.e. Tuesday, ... etc) will always be correct, so it is irrelevant to your problem.
Assuming non leap year, you can build a table of 12 rows (1 per month) containing the number of days in this month minus 31.
Jan 0
Feb -3
Mar 0
Apr -1
May 0
Jun -1
Jul 0
Aug 0
Sep -1
Oct 0
Nov -1
Dec 0
You can build a table of the displayed date for every 1st of the month, by adding to the day of the previous month the related number in this list. If the number is negative or equal to zero, add 31 to the figure.
i.e. from the 1st Dec 09 (date at which the clock is displaying 23), you can go to the 1st Jan 10.
You look at this table and find the figure next to Dec, it is 0.
Add 0 to 23 and you know that on the 1st Jan 10, the clock will be displaying 23.
From the 1st Jan 09, you know that the date which will be displayed on the 1st Feb 10 is 23.
From the 1st Feb 10, you can compute the value for the 01 Mar 10, it is 23 + (-3) = 20.
... etc
So, now, at every start of month where you get a value of 1 in this table, you know that the dates in this month will be correct.
If you have to include leap year, you need a second table with the values for a leap year or make an exception for February.
If you want to use this computation for previous dates, substract the figure from the table and when the number you obtain is over 31, just substract 31 to get the day number.
Using these tables and taking in account leap years.
The last past date at which the clock was correct was the 30 September 08 (it was correct between the 01-Jul-08 and the 30-Sep-08)
The next date at which it will be correct will be the: 01-Oct-17 and it will still be correct on the 30-Nov-17.
Now = 1 Dec 2009.
1st day of the month minus 23rd of past month = 8 days (assuming 31 day month).
Moving back counting non-31-days month...
Nov, Sep, June, Apr, Feb (X3), Nov = 8 days offset
So it was bought before Nov 2008?
I didn't code a single line for it, so pardon me if the answer is way off.
In Excel, you can test any date in A2 to see whether the clock will be correct on that date, with the formula =MOD(A2+19,31)+1=DAY(A2)