Showing posts with label Foreach. Show all posts
Showing posts with label Foreach. Show all posts

Saturday, September 22, 2018

Date Time operations with PowerShell


In this article, I’ll go through various date time operations you can perform using PowerShell.

Get-Date

You can use Get-Date cmdlet to get today’s date.




Below commands can be used to display only Date or Time.

Get-Date -DisplayHint Time
Get-Date -DisplayHint Date


You can also specify format to display date and time. You can use –Format parameter with Get-Date and specify ‘G’ format specifier to get General short date and short time.

Get-Date -Format G

You can use –UFormat parameter to specify custom format of date time.

Get-Date -UFormat "%d, %B, %Y, %A %r %t"

You can use foreach with get-date to get format as per your need.

Get-Date -Format F
Get-Date -Format F | foreach {$_ -replace ":", "-"}

You can find for all the properties of DateTime using Get-Member cmdlets.

Get-Date | Get-Member -MemberType Properties

You can get date time values as per your need using properties and methods of Date Time object.

Get-Date).TimeOfDay


Below are some output of Get-Date properties and methods can be used as per your requirements.

Properties/Methods
Output
(Get-Date).Date
02 March 2018 00:00:00
(Get-Date).Day
2
(Get-Date).DayOfWeek
Friday
(Get-Date).DayOfYear
61
(Get-Date).Hour
18
(Get-Date).Minute
48
(Get-Date).Month
3
(Get-Date).Second
5
(Get-Date).Millisecond
227
(Get-Date).IsDaylightSavingTime()
False
(Get-Date).ToShortDateString()
02-03-2018
(Get-Date).ToShortTimeString()
06:54 PM
(Get-Date).ToUniversalTime()
02 March 2018 13:26:17

You can add or remove date or time to current display date time using below various methods.

Methods
Output
(Get-Date).AddDays(10)
12 March 2018 19:01:09
(Get-Date).AddDays(-10)
20 February 2018 19:01:52
(Get-Date).AddYears(2)
02 March 2020 19:07:18
(Get-Date).AddMonths(3)
02 June 2018 19:04:48
(Get-Date).AddHours(3)
02 March 2018 22:03:48
(Get-Date).AddMinutes(23)
02 March 2018 19:28:54
(Get-Date).AddSeconds(40)
02 March 2018 19:06:58

You can find difference between two dates using below command.

(Get-Date).DayOfYear - (Get-Date -year 2018 -month 11 -date 20).DayOfYear



You can also create New-TimeSpan object to represent date at some interval of days or time. You can also check date time of that timespan.

(Get-Date) - (New-TimeSpan -Days 20)
(Get-Date) - (New-TimeSpan -Days -20)

Set-Date

You can change system date and time using Set-Date cmdlet. You can specify new date and time to Set-Date and it will change your system date and time. You need administrator privileges to execute Set-Date command.

Set-Date -Date (Get-Date).AddDays(5)
Set-Date -Date (Get-Date).AddDays(-5)

I hope you have now some knowledge about some basic date and time operations that you can perform in PowerShell. Thank you for reading this article. Please leave your feedback in comments below.

Reference –

See Also –

Friday, June 1, 2018

Observer Pattern


Observer design pattern is most suitable for publishing notification to its subscribers. Observer pattern also known as publisher-subscriber scenario where publisher notifies change to its all subscribers. Observer Pattern falls under behavioral pattern of GOF (Gang of Four) patterns.

When to use –


Observer pattern used when you have subject, which observed by many observers. When subject’s state changed, it notifies to all its subscribers. The observers register with subject (provider), and whenever the change occurs in subject (provider), the subject (provider) automatically notifies all observers.

To simplify Observer pattern, I have used most common example of stock price change notification. In this example, the watcher can register to watch on any stocks and whenever the stock price changes, the watcher will get notify.

Major components of Observer pattern –


Observer – This interface defines an update operation. This update operation will be called when subject’s state changes.
Concrete Observer – The class implements observer interface and implements actual update operation.
Subject – Subject is an abstract class. It has information about its observers. It also provides mechanism to subscribe and unsubscribe observers.
Concrete Subject – This class maintains its own state and calls base class notify method to update its observers.

Let’s have a look on below example of Observer design pattern.

Code –
//Observer
public interface IStockPriceWatcher
{
    string Name { get; set; }
    Subject Subject { get; set; }
    void Update(Subject sub);
}
//concrete observer
public class Watcher : IStockPriceWatcher
{
    public string Name { get; set; }
    public Subject Subject { get ; set ; }
    public Watcher(string name)
    {
        Name = name;
    }
    public void Update(Subject sub)
    {
        Console.WriteLine("{0}, {1} price udated to {2}",Name, Subject.Name, Subject.Price);
    }
}
//subject
public abstract class Subject
{
    private string name;
    private double price;
    private List<IStockPriceWatcher> investors = new List<IStockPriceWatcher>();
    public Subject(string Name, double Price)
    {
        name = Name;
        price = Price;
    }
    public string Name
    {
        get { return name; }
    }
    public double Price
    {
        get { return price; }
        set
        {
            if (price != value)
            {
                price = value;
                Notify();
            }
        }
    }
    public void Subscribe(IStockPriceWatcher observer)
    {
        investors.Add(observer);
        observer.Subject = this;
        Console.WriteLine("{0} subscribed to {1}", observer.Name, name);
    }
    public void UnSubscribe(IStockPriceWatcher observer)
    {
        investors.Remove(observer);
        Console.WriteLine("{0} unsubscribed from {1}", observer.Name, name );
    }
    public void Notify()
    {
        investors.ForEach(x => x.Update(this));
    }
}
//concrete subject
public class TCS : Subject
{
    public TCS(string name, double price) :base (name, price) { }
}
//concrete subject
public class Infosys : Subject
{
    public Infosys(string name, double price) : base(name, price) { }
}
//concrete subject
public class Wipro : Subject
{
    public Wipro(string name, double price) : base(name, price) { }
}
class Program
{
    static void Main(string[] args)
    {
        //subjects
        TCS tcs = new TCS("TCS", 3000);
        Infosys infy = new Infosys("Infosys", 1100);
        Wipro wipro = new Wipro("Wipro", 350);

        //watchers
        Watcher watcher1 = new Watcher("Watcher1");
        Watcher watcher2 = new Watcher("Watcher2");
        Watcher watcher3 = new Watcher("Watcher3");

        Console.WriteLine(new string('-',30));
        tcs.Subscribe(watcher1);
        infy.Subscribe(watcher2);
        wipro.Subscribe(watcher3);

        Console.WriteLine(new string('-', 30));
        tcs.Price = 3100;
        infy.Price = 1050;
        wipro.Price = 355;

        Console.WriteLine(new string('-', 30));
        tcs.UnSubscribe(watcher1);
        infy.UnSubscribe(watcher2);

        Console.WriteLine(new string('-', 30));
        tcs.Price = 3170;
        infy.Price = 1060;

        Console.WriteLine(new string('-', 30));
        tcs.Subscribe(watcher2);
        infy.Subscribe(watcher1);

        Console.WriteLine(new string('-', 30));
        tcs.Price = 3200;
        infy.Price = 1070;

        Console.Read();
    }
}

Output –


As per this example, the client class creates multiple subjects (stocks) and watchers (subscribers). The client class is also responsible for subscribe and unsubscribe watchers to particular subject (stock). After successful registration, whenever subject’s state (stock price) changes, the respective subscribers will get notify about change.

You can download full code from Gist.

I hope this article helps you to know more about Observer Design Pattern. Please leave your feedback in comments section below.

References –
DoFactory

See Also –

Creational Patterns
Structural Patterns
Behavioral Patterns