How to retrieve/decode timestamp from firebase database key? [duplicate] - firebase-realtime-database

This question already has answers here:
Can you get the timestamp from a Firebase realtime database key?
(3 answers)
Closed 2 years ago.
I didn't save timestamp in a some values and i need to find out timestamp from firebase database generated key. How to do this?

After googling for some time and not finding the answer I decided to have a look at firebase's source code.
Once I found how keys are generated (https://github.com/firebase/firebase-js-sdk/blob/master/packages/database/src/core/util/NextPushId.ts)
I found out that timestamp is converted to a 8-symbol string at the beginning of a key + some random symbols are added after that.
So i've written a small script to decode timestamp.
const decodeFirebaseKey = key => {
// chars firebase use for generating keys
const CHARS = '-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz';
let timestamp = 0
// we only need to decode first 8 symbols
for (let i = 0; i <= 7; i++) {
const index = CHARS.indexOf(key[i])
timestamp = timestamp * 64 + index
}
return timestamp
}
Hope it will save you some time.
I've also uploaded script to a gist: https://gist.github.com/Stas-Buzunko/b4f2ed1dc122cdb1867e13a731fc5dcf

Related

What type of date format is this? And how to convert it?

I found strange date codes in a SQL database used by an old iOS app called Memo Lite. The column was labeled LMT, that might be an abbreviation for last modified time. How are they constructed and how can they be converted to readable formats, like yyyy-mm-dd HH:mm:ss?
I am aware of both Unix time and how Google Sheets handles time.
Example of date/time codes:
357931095.942149 = 2012-05-05
330432567.859129 = 2011-06-22
293964817.803674 = 2010-04-26
I don't know if the format has time, it looks so but it is not showed in the app.
Date/time format codes, reference example:
https://support.google.com/docs/answer/3094139?hl=en
A little math shows it's seconds since an epoch. Not the usual 1970-01-01, but 2001-01-01.
2001-01-01 + 293964817 seconds = 2010-04-25 08:53:37
2001-01-01 + 330432567 seconds = 2011-06-22 10:49:27
2001-01-01 + 357931095 seconds = 2012-05-05 17:18:15
The decimal portion is microseconds. For example, 293964817.803674 is 2010-04-25 08:53:37.803674.
You mentioned it's an iOS app, so it's likely CFAbsoluteTime.

WriteXLSX gem not print date format properly print as normal number format

Need to print date in xlsx file mm/dd/yyyy format, added the format using add_format method, but its printing as '2019-09-27 elabrated date
workbook = ::WriteXLSX.new(file, strings_to_urls: false)
worksheet = workbook.add_worksheet('Sheet 1')
row=column=1
date_val = Date.today
date_format = workbook.add_format({'num_format': 'dd/mm/yy'})
worksheet.write(1 + row, column, date_val, date_format)
workbook.close
then i tried write_date_time method, code sample below
workbook = ::WriteXLSX.new(file, strings_to_urls: false)
worksheet = workbook.add_worksheet('Sheet 1')
row=column=1
date_val = Date.today
date_format = workbook.add_format({'num_format': 'dd/mm/yy'})
worksheet.write_date_time(1 + row, column, item[:value]&.iso8601, date_format)
workbook.close
The issue with what you are doing is that you are passing a date object, but if you have read the Date Time documentation then the first 2 lines says this -
A date/time in Excel is a real number plus an Excel number format.
WriteXLSX doesn’t automatically convert date/time strings in write() to an Excel date/time.
Now, to answer your question you need to convert the date to a number which the Excel understands.
date_time = DateTime.now.strftime('%Y-%m-%dT00:00:00.000Z')
I have added 00:00:00.000 as you are not concerned with time if you want time also then check strftime documentation
date_time_number = worksheet.convert_date_time(date_time)
format2 = workbook.add_format(:num_format => 'dd/mm/yy')
worksheet.write(row, col, number, format2)
This should solve it for you
This helped me understand how to use convert_date_time and its mention is at the bottom of the Date Time documentation

How do I know the difference of days, months, and years from a date, can I use subtraction? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm writing an algorithm where I'm going to get a whole number and with that I want to determine a date to expire a certain content. I'm already getting the date that the content was published.
Example:
publication date: 01/05/2019
days to expire: 30
result of the program: 01/06/2019 // date of expiry of the content
NOTE: The value of "Days to expire" is entered by the user.
How can I solve this? Do you have any documentation or function to help me?
There are a handful of ways to accomplish what you're trying to do -- Here are a few possibilities.
Using Time (my recommendation so you have more flexibility later depending on how your needs change and how you end up expiring content):
You can convert the "days to expire" to seconds and then add that to your time object. For example:
require "time"
t = Time.parse("01/05/2019")
days_to_expire = 30
expiration = t + (60 * 60 * 24 * days_to_expire)
Using Date:
require "date"
d = Date.parse("01/05/2019")
days_to_expire = 30
expiration = d + days_to_expire
Using built-in helpers from Rails Active::Support:
t = Time.parse("01/05/2019")
days_to_expire = 30
expiration = t + days_to_expire.days
You can learn more about Ruby Time here and you can learn about the helpers built-in to Time and Numeric by Rails here and here.
I'll answer this assuming the project is in Rails since this question is tagged as a Rails one. You could do something like this:
t = Date.parse("01/05/2019")
days_to_expire = 30 # Obtained from user input in your case
result = t + days_to_expire.days # Similar to result = t + 30.days
You could check out more about Date in the API docs here

How to specify a date range for the Google Adwords CLICK_PERFORMANCE_REPORT

I am trying to query the adwords api (v201603) and am using/extending the Java examples provided.
The problem I am encountering is being able to specify a specific date to download the report data for the CLICK_PERFORMANCE_REPORT report. This report is restricted to returning a single days worth of data at a time (for up to the last 90 days).
I can get todays and yesterdays data using the specifiers : ReportDefinitionDateRangeType.TODAY and ReportDefinitionDateRangeType.YESTERDAY, but what I really need to do is be able to specify a specific date within the last 90 days in order to get as much historical data as possible.
I have tried the following:
DateRange dr = new DateRange();
dr.setMin("20160401");
dr.setMax("20160402");
selector.setDateRange(dr);
reportDefinition.setDateRangeType(ReportDefinitionDateRangeType.CUSTOM_DATE);
reportDefinition.setReportType(ReportDefinitionReportType.CLICK_PERFORMANCE_REPORT);
reportDefinition.setDownloadFormat(DownloadFormat.CSV);
Both with and without the custom date specifier - and either gives the following error:
Report was not downloaded due to: HTTP Response Code: 400, Trigger: A single day DateRange is required for reportType: CLICK_PERFORMANCE_REPORT, Type: ReportDefinitionError.INVALID_DATE_RANGE_FOR_REPORT
Any help much appreciated, thanks in advance
You're going to have to use DURING and the same date string.
... + 'DURING 20161004,20161004';
Single Day Iterator for AdWords Scripts CLICK_PERFORMANCE_REPORT:
var start = new Date("12/01/2016");
var end = new Date("12/18/2016");
while(start < end){
var newDate = start.setDate(start.getDate() + 1);
start = new Date(newDate);
singleDay = start.toISOString().substring(0, 10).replace(/-/g,"");
console.log(singleDay);
}
Utilities.formatDate(start, timeZone, "yyyyMMdd"); formats date object as an 8-digit integer, so:
singleDay = Utilities.formatDate(start, timeZone, "yyyyMMdd");
Use singleDay in 'DURING ' + singleDay + ',' + singleDay);
Working with Dates and Times #Reporting — AdWords Scripts

Group UITableView by date without using coredata

I have an NSMutableArray with the latest 30 days records fetched from a DB.
Each record consists of a string and a creation date.
Now I want those records to feed an UITableView that will have a section for each different day and one cell per section simply showing how many records exist for each day.
Well, it's been hours of thinking and I'm not able yet to figure out how to do it. All the questions I've found on stackoverflow are related to core data, but I don't use it in my app.
Any help will be appreciated.
//after fetching data,write below code<br>
int cArray[30]; //create it as global array.cArray stores number of records that fall in particular date. if cArray[j]=10, it means for j'th date, there are 10 records.
// initialise all to zero.
for (int i=0; i<30; i++) {
cArray[i]=0;
}
NSArray *myArray;// my array stores your records.
[myArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
Record *rec = (Record*)obj;
cArray[(int)(rec.creationDate)]++;
}];
And later you can use cArray to show number of records that fall in a particular date.
Hope this helps.
I have implemented my data model similar to this case. I have an array of the NSDateComponents for each day and have a dictionary which keys is the date components that is stored in the array and values is the arrays that is store the data for each day. This approach is working pretty well for me.
You can group your data according to date. Then you can store grouped array on a dictionary and the key will be the date and this key will be your table header and the value will be the list of data at that date. I hope you got this approach.

Resources