Posts

Showing posts from 2022

AWS Cognito fine grained Authorization

https://aws.amazon.com/blogs/security/use-amazon-cognito-to-add-claims-to-an-identity-token-for-fine-grained-authorization/

PST to UTC Date

I had a requirement to return customer usage between two days, but database saved all usage records with end of the day PST time in UTC dates. Since API accepts date only, database has records with UTC, API need to convert date into end of the day and convert same into UTC to retrive right DB records. Eg - Given date - 4/30/2022 Database has record saved as- 5/1/2022 6:59:59 AM +00:00 (4/30/2022 23:59:59 PST) Since DB saved usage dated 4/30 as 5/1 in UTC, API would miss last record if no date conversion implemented. The below code converts given date only string into end of the day for the intended timezone with UTC format to compare records and include last record with given date ranges. //Get Pacific Time from the machine. var pacificTimeZone = "America/Los_Angeles"; //for linux env if (Environment.OSVersion.VersionString.Contains("Windows")) pacificTimeZone = "Pacific Standard Time"; var pstZone = TimeZoneInfo.FindSystemTimeZoneById(pacific...

BFS - Breadth First Search

The below is sample code to iterate through graph nodes using breadth first search - //build graph nodes var tree = new Dictionary<int, List<int>>(); tree.Add(1, new List<int> { 2, 3, 4 }); tree.Add(2, new List<int> { 5 }); tree.Add(3, new List<int> { 6, 7}); tree.Add(4, new List<int> { 8 }); tree.Add(5, new List<int> { 9 }); tree.Add(6, new List<int> { 10 }); //get root element of graph Queue q = new Queue(); q.Enqueue(tree.ElementAt(0).Key); HashSet<int> visitiedNode = new HashSet<int>(); //iterate queue to visit nodes while(q.Count > 0) { //dequeue 1st element var item = Convert.ToInt32(q.Dequeue()); //check if element already visited if (visitiedNode.Contains(item)) continue; //add element to visited list visitiedNode.Add(item); Console.WriteLine(item); //get next adjecent nodes tree.TryGetValue(item, out List<int> adjcentNodes); //enqueue new nodes to the queue if (adjcentNodes != null)...