Noda time comparisons between zoned datetime and instant - nodatime

I am writing a function to test if a file creation time is before the start of the current day in a different time zone, and fetch if my local file copy is stale.
Checking here Noda Time - Start/end of day with zone I have come up with the below:
bool FileCreationIsBeforeStartOfDayInNewYork()
{
var zone = DateTimeZoneProviders.Tzdb["America/New_York"];
var nowInNewYork = SystemClock.Instance.InZone(zone).GetCurrentZonedDateTime();
var today = nowInNewYork.Date;
var startOfDay = zone.AtStartOfDay(today);
var fileCreationTime = ZonedDateTime.FromDateTimeOffset(File.GetLastWriteTime(Filename).ToUniversalTime());
return fileCreationTime.ToInstant() < startOfDay.ToInstant();
}
The File.GetLastWriteTime method returns the file modification time of the local system clock.
I am confused if I should be converting the file creation time to the systems local time or if stick with UTC, and if my use of ToInstant is correct in this context?

Related

How to get latest Snapshot for a volume in AWS using API

I want only the latest snapshot for a specific volume.
response_v=boto3.client("ec2").describe_snapshots(Filters=[{"Name":"volume-id","Values":["vol-fffffffffff"]}])
How can it be done?
It looks like the describe_snapshots method returns the newest one first but you really shouldn't count on that.
I think you can safely rely on the StartTime field, looking for the greatest value for all snapshots returned.
Snapshots occur asynchronously; the point-in-time snapshot is created immediately
Because of that the "largest" StartTime will be the latest snapshot
I wrote this bit of code to print the snapshot_id with the latest snapshot start time. My python-fu is not the greatest but this works.
import boto3
import datetime
import pytz
utc = pytz.UTC
starttime=datetime.datetime(1,1,1,tzinfo=utc)
snap_id = ""
volume_id = "<put your volume id here or write something more elegant to pass it in>"
region = 'us-east-1'
session = boto3.Session(profile_name='default')
ec2 = session.client('ec2', region_name=region)
response = ec2.describe_snapshots(Filters=[{"Name":"volume-id","Values":[volume_id]}])
# print(response['Snapshots'])
for snap in response['Snapshots']:
if snap['StartTime'] > starttime:
snap_id = snap['SnapshotId']
starttime= snap['StartTime']
print(snap_id)
References
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-creating-snapshot.html

Node JS Error: Moment Timezone has no data for 2018-08-08T10:00:00

I am trying to initialize time of los_angles time zone in moment. But it is throwing the following error:
Moment Timezone has no data for 2018-08-08T10:00:00
Following is my code:
moment().tz("2018-08-08T10:00:00","YYYY-MM-DDTHH:mm:ss",true,"America/Los_Angeles").format("YYYY-MM-DDTHH:mm:ss");
You are supposed to remove () at moment from moment().tz(..). The correct one is moment.tz(..) as in
moment.tz("2018-08-08T10:00:00","YYYY-MM-DDTHH:mm:ss",true,"America/Los_Angeles").format("YYYY-MM-DDTHH:mm:ss");
it gives me result
"2018-08-08T10:00:00"
Ref:
http://momentjs.com/timezone/
Can you try this:
moment.tz("2018-08-08T10:00:00","YYYY-MM-DDTHH:mm:ss",true,"America/Los_Angeles").format("YYYY-MM-DDTHH:mm:ss");
I got a very similar error (Moment Timezone has no data for 1624031790031) and solved it by 3 steps:
Import "moment-timezone", not "moment".
const momentTimezone = require("moment-timezone");
Load the moment timezone data.
You can add time zones one at a time:
momentTimezone.tz.add('America/Los_Angeles|PST PDT|80 70|0101|1Lzm0 1zb0 Op0');
Or you can add them as an array:
momentTimezone.tz.add([
"America/Los_Angeles|PST PDT|80 70|0101|1Lzm0 1zb0 Op0",
"America/Port_of_Spain|LMT AST|46.4 40|01|-2kNvR.U|43e3"
]);
I saved an abbreviated version (only the time zones I needed) of the following time zone data into a JSON file
https://github.com/moment/moment-timezone/blob/develop/data/packed/latest.json
then added those time zones to moment-timezone:
const momentTimezoneData = require("../utils/momentTimezoneData");
momentTimezone.tz.add(momentTimezoneData.zones);
Make sure your time zone is one you've loaded. Apparently our timezone had not; it had been a unix timestamp 1624031790031. After making it one of the time zones (e.g. "America/Chicago") we'd loaded in #2 above, it all worked.
const timeInTheZone = momentTimezone
.tz("2021-06-24T04:34:36.380Z", "America/Chicago")
.format('YYYY-MM-DD HH:mm');

How to prevent Execution usage limit in scheduled scripts

I am using the scheduled script which will create the custom records based on criteria. every time when the schedule script runs it should create approx. 100,000 records but the script is timing out after creating 5000 or 10000 records. I am using the below script to prevent the script execution usage limit but even with this also the script is not working. can any one please suggest some thing or provide any information. any suggestions are welcome and highly appreciated.
In my for loop iam using the below script. with this below script included the scheduled script is able to create up to 5000 or 10000 records only.
if (nlapiGetContext().getRemainingUsage() <= 0 && (i+1) < results.length )
{
var stateMain = nlapiYieldScript();
}
If you are going to reschedule using the nlapiYieldScript mechanism, then you also need to use nlapiSetRecoveryPoint at the point where you wish the script to resume. See the Help documentation for each of these methods, as well as the page titled Setting Recovery Points in Scheduled Scripts
Be aware that nlapiSetRecoveryPoint uses 100 governance units, so you will need to account for this in your getRemainingUsage check.
#rajesh, you are only checking the remaining usage. Also do check for execution time limit, which is 1 hour for any scheduled script. Something like below snippet-
var checkIfYieldOrContinue = function(startTime) {
var endTime = new Date().getTime();
var timeElapsed = (endTime * 0.001) - (startTime * 0.001);
if (nlapiGetContext().getRemainingUsage() < 3000 ||
timeElapsed > 3500) { //3500 secs
nlapiLogExecution('AUDIT', 'Remaining Usage: ' + nlapiGetContext().getRemainingUsage() + '. Time elapsed: ' + timeElapsed);
startTime = new Date().getTime();
var yieldStatus = nlapiYieldScript();
nlapiLogExecution('AUDIT', 'script yielded.' + yieldStatus.status);
nlapiLogExecution('AUDIT', 'script yielded reason.' + yieldStatus.reason);
nlapiLogExecution('AUDIT', 'script yielded information.' + yieldStatus.information);
}
};
Inside your for loop, you can call this method like-
var startTime = new Date();
if ((i+1) < results.length ) {
//do your operations here and then...
checkIfYieldOrContinue(startTime);
}
I have a script that lets you process an array like a forEach. The script checks each iteration and calculates the maximum usage and yields when there is not enough usage left to cover the max.
Head over to https://github.com/BKnights/KotN-Netsuite and download simpleBatch.js

How to get Log4perl rotating my logs daily?

I'm reading up on Log4perl and want to try and use it for simple log management of my Perl scripts running on a Linux box. I've also read up on newsyslog and logrotate but want to use Log4perl if at all possible.
I'm trying to configure the /etc/log4perl.conf file so that it:
Defines a widget logger (INFO level) that will write all output to /opt/myapp/logs/myapp-<datetime>.log, where <datetime> is a date/time formatted string like 2012-12-20
This myapp-<datetime>.log file needs to be rotated daily (preferably at midnight), where the old file is deleted, and a new file is created with <datetime> + 1. For instance, myapp-2012-12-20.log would be replaced with myapp-2012-12-21.log, etc.
Here's my best attempt which I believe is close, but is still missing some configuration:
#####/etc/log4perl.conf############################################################
log4perl.logger.widget = INFO, MyAppLogAppender
log4perl.appender.MyAppLogAppender = Log::Log4perl::Appender::File
log4perl.appender.MyAppLogAppender.filename = /opt/myapp/logs/myapp-???.log
log4perl.appender.MyAppLogAppender.layout = Log::Log4perl::Layout::SimpleLayout
###################################################################################
How do I configure log4perl.appender.MyAppLogAppender to rotate once a day, delete the old file, and create a new one with a correct timestamp? Thanks in advance.
Here's an example of a Log::Log4perl configuration file, defining a daily rollover at midnight (date pattern yyyy-MM-dd), keeping a maximum of 5 saved logfiles around, at WARN level, and dumping everything to screen:
log4perl.logger = TRACE, Screen, Logfile
log4perl.appender.Logfile = Log::Dispatch::FileRotate
log4perl.appender.Logfile.Threshold = WARN
log4perl.appender.Logfile.filename    = test.log
log4perl.appender.Logfile.max         = 5
log4perl.appender.Logfile.DatePattern = yyyy-MM-dd
log4perl.appender.Logfile.TZ          = PST
log4perl.appender.Logfile.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.Logfile.layout.ConversionPattern = %d %m %n
log4perl.appender.Screen = Log::Log4perl::Appender::Screen
log4perl.appender.Screen.stderr = 0
log4perl.appender.Screen.utf8 = 1
log4perl.appender.Screen.layout = Log::Log4perl::Layout::PatternLayout::Multiline
log4perl.appender.Screen.layout.ConversionPattern = [%p] %m %n
(reference: https://metacpan.org/module/Log::Log4perl::FAQ#How-can-I-roll-over-my-logfiles-automatically-at-midnight-)
There is a gotcha in Log::Dispatch::FileRotate, if your daily job is run later in the day (say 23:00) and takes 2h (so ends at about 01:00), the log rotation will never happens with a "day" pattern like :
log4perl.appender.Logfile.DatePattern = yyyy-MM-dd
A simple workaround is to use an "hourly" pattern like this :
log4perl.appender.Logfile.DatePattern = yyyy-MM-dd-HH
So when the next run starts at 23:00, the log file get rotated as more than one hour has passed.

Twitter Search API - Searching by Time

Is there any way to send a query param to Twitter, telling it to only return search results within a specified period of time? For example, give me the results for this "keyword" tweeted between 12 pm and 3 pm ET on July 24, 2011? If Twitter doesn't allow you to search by time -- and only by date -- then is there anything in the results that will allow you to see the exact time when the user made that tweet?
As far as I can tell, there is not a way to specify time (more specific than the date). However, after getting your list of tweets, you can remove those that don't fall within the specified time range by comparing each tweet's timestamp.
This is how I would do it in ruby with the twitter gem:
require 'twitter'
require 'time'
start_time = Time.now - 3*3600
end_time = Time.now
search = Twitter::Search.new.contains('test')
search.since_date(start_time.strftime("%Y-%m-%d"))
search.until_date(end_time.strftime("%Y-%m-%d"))
tweets = search.fetch
tweets.delete_if { |t| Time.parse(t.created_at) < start_time }
tweets.delete_if { |t| Time.parse(t.created_at) > end_time }

Resources