WeixiangYan/CodeTransOcean huggingface dataset:
Task: Last Friday of each month







Python: import calendar

def last_fridays(year):
    for month in range(1, 13):
        last_friday = max(week[calendar.FRIDAY]
            for week in calendar.monthcalendar(year, month))
        print('{:4d}-{:02d}-{:02d}'.format(year, month, last_friday))



COBOL:        program-id. last-fri.
       data division.
       working-storage section.
       1 wk-date.
        2 yr pic 9999.
        2 mo pic 99 value 1.
        2 da pic 99 value 1.
       1 rd-date redefines wk-date pic 9(8).
       1 binary.
        2 int-date pic 9(8).
        2 dow pic 9(4).
        2 friday pic 9(4) value 5.
       procedure division.
           display "Enter a calendar year (1601 thru 9999): "
               with no advancing
           accept yr
           if yr >= 1601 and <= 9999
               continue
           else
               display "Invalid year"
               stop run
           end-if
           perform 12 times
               move 1 to da
               add 1 to mo
               if mo > 12              
                   move 12 to mo
                   move 31 to da
               end-if
               compute int-date = function
                   integer-of-date (rd-date)
               if mo =12 and da = 31   
                   continue
               else
                   subtract 1 from int-date
               end-if
               compute rd-date = function
                   date-of-integer (int-date)
               compute dow = function mod
                   ((int-date - 1) 7) + 1
               compute dow = function mod ((dow - friday) 7)
               subtract dow from da
               display yr "-" mo "-" da
               add 1 to mo
           end-perform
           stop run
           .
       end program last-fri.
Task: Last Friday of each month




Go: package main

import (
	"fmt"
	"os"
	"strconv"
	"time"
)

func main() {
	y := time.Now().Year()
	if len(os.Args) == 2 {
		if i, err := strconv.Atoi(os.Args[1]); err == nil {
			y = i
		}
	}
	for m := time.January; m <= time.December; m++ {
		d := time.Date(y, m+1, 1, 0, 0, 0, 0, time.UTC).Add(-24 * time.Hour)
		d = d.Add(-time.Duration((d.Weekday()+7-time.Friday)%7) * 24 * time.Hour)
		fmt.Println(d.Format("2006-01-02"))
	}
}






COBOL:        program-id. last-fri.
       data division.
       working-storage section.
       1 wk-date.
        2 yr pic 9999.
        2 mo pic 99 value 1.
        2 da pic 99 value 1.
       1 rd-date redefines wk-date pic 9(8).
       1 binary.
        2 int-date pic 9(8).
        2 dow pic 9(4).
        2 friday pic 9(4) value 5.
       procedure division.
           display "Enter a calendar year (1601 thru 9999): "
               with no advancing
           accept yr
           if yr >= 1601 and <= 9999
               continue
           else
               display "Invalid year"
               stop run
           end-if
           perform 12 times
               move 1 to da
               add 1 to mo
               if mo > 12              
                   move 12 to mo
                   move 31 to da
               end-if
               compute int-date = function
                   integer-of-date (rd-date)
               if mo =12 and da = 31   
                   continue
               else
                   subtract 1 from int-date
               end-if
               compute rd-date = function
                   date-of-integer (int-date)
               compute dow = function mod
                   ((int-date - 1) 7) + 1
               compute dow = function mod ((dow - friday) 7)
               subtract dow from da
               display yr "-" mo "-" da
               add 1 to mo
           end-perform
           stop run
           .
       end program last-fri.
Task: Last Friday of each month






PHP: <?php
function last_friday_of_month($year, $month) {
  $day = 0;
  while(True) {
    $last_day = mktime(0, 0, 0, $month+1, $day, $year); 
    if (date("w", $last_day) == 5) {
      return date("Y-m-d", $last_day);
    }
    $day -= 1;
  }
}
 
function print_last_fridays_of_month($year) {
  foreach(range(1, 12) as $month) {
    echo last_friday_of_month($year, $month), "<br>";
  }
}
 
date_default_timezone_set("GMT");
$year = 2012;
print_last_fridays_of_month($year);
?>
Task: Last Friday of each month






PHP: <?php
function last_friday_of_month($year, $month) {
  $day = 0;
  while(True) {
    $last_day = mktime(0, 0, 0, $month+1, $day, $year); 
    if (date("w", $last_day) == 5) {
      return date("Y-m-d", $last_day);
    }
    $day -= 1;
  }
}
 
function print_last_fridays_of_month($year) {
  foreach(range(1, 12) as $month) {
    echo last_friday_of_month($year, $month), "<br>";
  }
}
 
date_default_timezone_set("GMT");
$year = 2012;
print_last_fridays_of_month($year);
?>






Ada: with Ada.Text_IO, GNAT.Calendar.Time_IO, Ada.Command_Line,
  Ada.Calendar.Formatting, Ada.Calendar.Arithmetic;

procedure Last_Weekday_In_Month is
   
   procedure Put_Line(T: Ada.Calendar.Time) is
      use GNAT.Calendar.Time_IO;
   begin
      Ada.Text_IO.Put_Line(Image(Date => T, Picture => ISO_Date));
   end Put_Line;
   
   use Ada.Calendar, Ada.Calendar.Arithmetic;
   subtype Day_Name is Formatting.Day_Name; use type Formatting.Day_Name;
   
   T, Selected : Time;
   Weekday: Day_Name  := Day_Name'Value(Ada.Command_Line.Argument (1));
   Year : Year_Number := Integer'Value (Ada.Command_Line.Argument (2));
   
begin
   for Month in 1 .. 12 loop
      T := Time_Of (Year => Year, Month => Month, Day => 01);
      while Ada.Calendar.Month(T) = Month loop
	 if Formatting.Day_Of_Week (T) = Weekday then
	    Selected := T;
	 end if;
	 T := T + Day_Count(1);
      end loop;
      Put_Line(Selected);
   end loop;
end Last_Weekday_In_Month;
Task: Last Friday of each month






PHP: <?php
function last_friday_of_month($year, $month) {
  $day = 0;
  while(True) {
    $last_day = mktime(0, 0, 0, $month+1, $day, $year); 
    if (date("w", $last_day) == 5) {
      return date("Y-m-d", $last_day);
    }
    $day -= 1;
  }
}
 
function print_last_fridays_of_month($year) {
  foreach(range(1, 12) as $month) {
    echo last_friday_of_month($year, $month), "<br>";
  }
}
 
date_default_timezone_set("GMT");
$year = 2012;
print_last_fridays_of_month($year);
?>




COBOL:        program-id. last-fri.
       data division.
       working-storage section.
       1 wk-date.
        2 yr pic 9999.
        2 mo pic 99 value 1.
        2 da pic 99 value 1.
       1 rd-date redefines wk-date pic 9(8).
       1 binary.
        2 int-date pic 9(8).
        2 dow pic 9(4).
        2 friday pic 9(4) value 5.
       procedure division.
           display "Enter a calendar year (1601 thru 9999): "
               with no advancing
           accept yr
           if yr >= 1601 and <= 9999
               continue
           else
               display "Invalid year"
               stop run
           end-if
           perform 12 times
               move 1 to da
               add 1 to mo
               if mo > 12              
                   move 12 to mo
                   move 31 to da
               end-if
               compute int-date = function
                   integer-of-date (rd-date)
               if mo =12 and da = 31   
                   continue
               else
                   subtract 1 from int-date
               end-if
               compute rd-date = function
                   date-of-integer (int-date)
               compute dow = function mod
                   ((int-date - 1) 7) + 1
               compute dow = function mod ((dow - friday) 7)
               subtract dow from da
               display yr "-" mo "-" da
               add 1 to mo
           end-perform
           stop run
           .
       end program last-fri.
Task: Date format



C#: using System;

namespace RosettaCode.DateFormat
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime today = DateTime.Now.Date;
            Console.WriteLine(today.ToString("yyyy-MM-dd"));
            Console.WriteLine(today.ToString("dddd, MMMMM d, yyyy"));
        }
    }
}









Ada: with Ada.Calendar;             use Ada.Calendar;
with Ada.Calendar.Formatting;  use Ada.Calendar.Formatting;
with Ada.Text_IO;              use Ada.Text_IO;

procedure Date_Format is
   function Image (Month : Month_Number) return String is
   begin
      case Month is
         when 1  => return "January";
         when 2  => return "February";
         when 3  => return "March";
         when 4  => return "April";
         when 5  => return "May";
         when 6  => return "June";
         when 7  => return "July";
         when 8  => return "August";
         when 9  => return "September";
         when 10 => return "October";
         when 11 => return "November";
         when 12 => return "December";
      end case;
   end Image;
   function Image (Day : Day_Name) return String is
   begin
      case Day is
         when Monday    => return "Monday";
         when Tuesday   => return "Tuesday";
         when Wednesday => return "Wednesday";
         when Thursday  => return "Thursday";
         when Friday    => return "Friday";
         when Saturday  => return "Saturday";
         when Sunday    => return "Sunday";
      end case;
   end Image;
   Today : Time := Clock;
begin
   Put_Line (Image (Today) (1..10));
   Put_Line
   (  Image (Day_Of_Week (Today)) & ", "
   &  Image (Ada.Calendar.Month (Today))
   &  Day_Number'Image (Ada.Calendar.Day (Today)) & ","
   &  Year_Number'Image (Ada.Calendar.Year (Today))
   );
end Date_Format;
Task: Date format

C: #include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define MAX_BUF 50

int main(void)
{
  char buf[MAX_BUF];
  time_t seconds = time(NULL);
  struct tm *now = localtime(&seconds);
  const char *months[] = {"January", "February", "March", "April", "May", "June",
                          "July", "August", "September", "October", "November", "December"};

  const char *days[] = {"Sunday", "Monday", "Tuesday", "Wednesday","Thursday","Friday","Saturday"};

  (void) printf("%d-%d-%d\n", now->tm_year + 1900, now->tm_mon + 1, now->tm_mday);
  (void) printf("%s, %s %d, %d\n",days[now->tm_wday], months[now->tm_mon],
               now->tm_mday, now->tm_year + 1900);
  
  (void) strftime(buf, MAX_BUF, "%A, %B %e, %Y", now);
  (void) printf("%s\n", buf);
  return EXIT_SUCCESS;
}











Ada: with Ada.Calendar;             use Ada.Calendar;
with Ada.Calendar.Formatting;  use Ada.Calendar.Formatting;
with Ada.Text_IO;              use Ada.Text_IO;

procedure Date_Format is
   function Image (Month : Month_Number) return String is
   begin
      case Month is
         when 1  => return "January";
         when 2  => return "February";
         when 3  => return "March";
         when 4  => return "April";
         when 5  => return "May";
         when 6  => return "June";
         when 7  => return "July";
         when 8  => return "August";
         when 9  => return "September";
         when 10 => return "October";
         when 11 => return "November";
         when 12 => return "December";
      end case;
   end Image;
   function Image (Day : Day_Name) return String is
   begin
      case Day is
         when Monday    => return "Monday";
         when Tuesday   => return "Tuesday";
         when Wednesday => return "Wednesday";
         when Thursday  => return "Thursday";
         when Friday    => return "Friday";
         when Saturday  => return "Saturday";
         when Sunday    => return "Sunday";
      end case;
   end Image;
   Today : Time := Clock;
begin
   Put_Line (Image (Today) (1..10));
   Put_Line
   (  Image (Day_Of_Week (Today)) & ", "
   &  Image (Ada.Calendar.Month (Today))
   &  Day_Number'Image (Ada.Calendar.Day (Today)) & ","
   &  Year_Number'Image (Ada.Calendar.Year (Today))
   );
end Date_Format;
Task: Date format


C++: 


#include <vector>
#include <string>
#include <iostream>
#include <ctime>


class Date
{
    struct tm ltime;

public:
    
    Date()
    {
        time_t t = time(0);
        localtime_r(&t, &ltime);
    }
    
    
    std::string getDate(const char* fmt)
    {
        char out[200];
        size_t result = strftime(out, sizeof out, fmt, &ltime);
        return std::string(out, out + result);
    }
    
    
    std::string getISODate() {return getDate("%F");}
    
    
    std::string getTextDate() {return getDate("%A, %B %d, %Y");}
};

int main()
{
    Date d;
    std::cout << d.getISODate() << std::endl;
    std::cout << d.getTextDate() << std::endl;
    return 0;
}










Ada: with Ada.Calendar;             use Ada.Calendar;
with Ada.Calendar.Formatting;  use Ada.Calendar.Formatting;
with Ada.Text_IO;              use Ada.Text_IO;

procedure Date_Format is
   function Image (Month : Month_Number) return String is
   begin
      case Month is
         when 1  => return "January";
         when 2  => return "February";
         when 3  => return "March";
         when 4  => return "April";
         when 5  => return "May";
         when 6  => return "June";
         when 7  => return "July";
         when 8  => return "August";
         when 9  => return "September";
         when 10 => return "October";
         when 11 => return "November";
         when 12 => return "December";
      end case;
   end Image;
   function Image (Day : Day_Name) return String is
   begin
      case Day is
         when Monday    => return "Monday";
         when Tuesday   => return "Tuesday";
         when Wednesday => return "Wednesday";
         when Thursday  => return "Thursday";
         when Friday    => return "Friday";
         when Saturday  => return "Saturday";
         when Sunday    => return "Sunday";
      end case;
   end Image;
   Today : Time := Clock;
begin
   Put_Line (Image (Today) (1..10));
   Put_Line
   (  Image (Day_Of_Week (Today)) & ", "
   &  Image (Ada.Calendar.Month (Today))
   &  Day_Number'Image (Ada.Calendar.Day (Today)) & ","
   &  Year_Number'Image (Ada.Calendar.Year (Today))
   );
end Date_Format;
Task: Date format




Go: package main

import "time"
import "fmt"

func main() {
    fmt.Println(time.Now().Format("2006-01-02"))
    fmt.Println(time.Now().Format("Monday, January 2, 2006"))
}








Ada: with Ada.Calendar;             use Ada.Calendar;
with Ada.Calendar.Formatting;  use Ada.Calendar.Formatting;
with Ada.Text_IO;              use Ada.Text_IO;

procedure Date_Format is
   function Image (Month : Month_Number) return String is
   begin
      case Month is
         when 1  => return "January";
         when 2  => return "February";
         when 3  => return "March";
         when 4  => return "April";
         when 5  => return "May";
         when 6  => return "June";
         when 7  => return "July";
         when 8  => return "August";
         when 9  => return "September";
         when 10 => return "October";
         when 11 => return "November";
         when 12 => return "December";
      end case;
   end Image;
   function Image (Day : Day_Name) return String is
   begin
      case Day is
         when Monday    => return "Monday";
         when Tuesday   => return "Tuesday";
         when Wednesday => return "Wednesday";
         when Thursday  => return "Thursday";
         when Friday    => return "Friday";
         when Saturday  => return "Saturday";
         when Sunday    => return "Sunday";
      end case;
   end Image;
   Today : Time := Clock;
begin
   Put_Line (Image (Today) (1..10));
   Put_Line
   (  Image (Day_Of_Week (Today)) & ", "
   &  Image (Ada.Calendar.Month (Today))
   &  Day_Number'Image (Ada.Calendar.Day (Today)) & ","
   &  Year_Number'Image (Ada.Calendar.Year (Today))
   );
end Date_Format;
Task: Date format





Java: Datetime dtNow = datetime.now();
String strDt1 = dtNow.format('yyyy-MM-dd');
String strDt2 = dtNow.format('EEEE, MMMM dd, yyyy');
system.debug(strDt1); 
system.debug(strDt2); 







Ada: with Ada.Calendar;             use Ada.Calendar;
with Ada.Calendar.Formatting;  use Ada.Calendar.Formatting;
with Ada.Text_IO;              use Ada.Text_IO;

procedure Date_Format is
   function Image (Month : Month_Number) return String is
   begin
      case Month is
         when 1  => return "January";
         when 2  => return "February";
         when 3  => return "March";
         when 4  => return "April";
         when 5  => return "May";
         when 6  => return "June";
         when 7  => return "July";
         when 8  => return "August";
         when 9  => return "September";
         when 10 => return "October";
         when 11 => return "November";
         when 12 => return "December";
      end case;
   end Image;
   function Image (Day : Day_Name) return String is
   begin
      case Day is
         when Monday    => return "Monday";
         when Tuesday   => return "Tuesday";
         when Wednesday => return "Wednesday";
         when Thursday  => return "Thursday";
         when Friday    => return "Friday";
         when Saturday  => return "Saturday";
         when Sunday    => return "Sunday";
      end case;
   end Image;
   Today : Time := Clock;
begin
   Put_Line (Image (Today) (1..10));
   Put_Line
   (  Image (Day_Of_Week (Today)) & ", "
   &  Image (Ada.Calendar.Month (Today))
   &  Day_Number'Image (Ada.Calendar.Day (Today)) & ","
   &  Year_Number'Image (Ada.Calendar.Year (Today))
   );
end Date_Format;
Task: Date format







Python: import datetime
today = datetime.date.today()

today.isoformat()

today.strftime("%A, %B %d, %Y")


"The date is {0:%A, %B %d, %Y}".format(d)

"The date is {date:%A, %B %d, %Y}".format(date=d)

f"The date is {d:%A, %B %d, %Y}"





Ada: with Ada.Calendar;             use Ada.Calendar;
with Ada.Calendar.Formatting;  use Ada.Calendar.Formatting;
with Ada.Text_IO;              use Ada.Text_IO;

procedure Date_Format is
   function Image (Month : Month_Number) return String is
   begin
      case Month is
         when 1  => return "January";
         when 2  => return "February";
         when 3  => return "March";
         when 4  => return "April";
         when 5  => return "May";
         when 6  => return "June";
         when 7  => return "July";
         when 8  => return "August";
         when 9  => return "September";
         when 10 => return "October";
         when 11 => return "November";
         when 12 => return "December";
      end case;
   end Image;
   function Image (Day : Day_Name) return String is
   begin
      case Day is
         when Monday    => return "Monday";
         when Tuesday   => return "Tuesday";
         when Wednesday => return "Wednesday";
         when Thursday  => return "Thursday";
         when Friday    => return "Friday";
         when Saturday  => return "Saturday";
         when Sunday    => return "Sunday";
      end case;
   end Image;
   Today : Time := Clock;
begin
   Put_Line (Image (Today) (1..10));
   Put_Line
   (  Image (Day_Of_Week (Today)) & ", "
   &  Image (Ada.Calendar.Month (Today))
   &  Day_Number'Image (Ada.Calendar.Day (Today)) & ","
   &  Year_Number'Image (Ada.Calendar.Year (Today))
   );
end Date_Format;
Task: Date format








VB: OPTION EXPLICIT


DATA "January", "February", "March", "April", "May", "June", "July"
DATA "August", "September", "October", "November", "December"


DATA "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"


DIM dDate AS DOUBLE, sMonth(1 TO 12) AS STRING, sDay(1 TO 7) AS STRING
DIM i AS INTEGER


FOR i = 1 TO 12
  READ sMonth(i)
NEXT i


FOR i = 1 TO 7
  READ sDay(i)
NEXT i


dDate = NOW

CLS
PRINT "Rosettacode: Display the current date formatted."
PRINT
PRINT "Today
PRINT
PRINT "Use of values in the lists."
PRINT "Today is "; sDay(WEEKDAY(dDate)); ", "; sMonth(MONTH(dDate)); " "; FORMAT$(DAY(dDate)); ", "; FORMAT$(YEAR(dDate)); "."
PRINT
PRINT "Use of just the FORMAT$() function."
PRINT "Today is "; FORMAT$(dDate, "dddd, mmmm d, yyyy"); "."

END




Ada: with Ada.Calendar;             use Ada.Calendar;
with Ada.Calendar.Formatting;  use Ada.Calendar.Formatting;
with Ada.Text_IO;              use Ada.Text_IO;

procedure Date_Format is
   function Image (Month : Month_Number) return String is
   begin
      case Month is
         when 1  => return "January";
         when 2  => return "February";
         when 3  => return "March";
         when 4  => return "April";
         when 5  => return "May";
         when 6  => return "June";
         when 7  => return "July";
         when 8  => return "August";
         when 9  => return "September";
         when 10 => return "October";
         when 11 => return "November";
         when 12 => return "December";
      end case;
   end Image;
   function Image (Day : Day_Name) return String is
   begin
      case Day is
         when Monday    => return "Monday";
         when Tuesday   => return "Tuesday";
         when Wednesday => return "Wednesday";
         when Thursday  => return "Thursday";
         when Friday    => return "Friday";
         when Saturday  => return "Saturday";
         when Sunday    => return "Sunday";
      end case;
   end Image;
   Today : Time := Clock;
begin
   Put_Line (Image (Today) (1..10));
   Put_Line
   (  Image (Day_Of_Week (Today)) & ", "
   &  Image (Ada.Calendar.Month (Today))
   &  Day_Number'Image (Ada.Calendar.Day (Today)) & ","
   &  Year_Number'Image (Ada.Calendar.Year (Today))
   );
end Date_Format;
Task: Date format

C: #include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define MAX_BUF 50

int main(void)
{
  char buf[MAX_BUF];
  time_t seconds = time(NULL);
  struct tm *now = localtime(&seconds);
  const char *months[] = {"January", "February", "March", "April", "May", "June",
                          "July", "August", "September", "October", "November", "December"};

  const char *days[] = {"Sunday", "Monday", "Tuesday", "Wednesday","Thursday","Friday","Saturday"};

  (void) printf("%d-%d-%d\n", now->tm_year + 1900, now->tm_mon + 1, now->tm_mday);
  (void) printf("%s, %s %d, %d\n",days[now->tm_wday], months[now->tm_mon],
               now->tm_mday, now->tm_year + 1900);
  
  (void) strftime(buf, MAX_BUF, "%A, %B %e, %Y", now);
  (void) printf("%s\n", buf);
  return EXIT_SUCCESS;
}
Task: Date format



C#: using System;

namespace RosettaCode.DateFormat
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime today = DateTime.Now.Date;
            Console.WriteLine(today.ToString("yyyy-MM-dd"));
            Console.WriteLine(today.ToString("dddd, MMMMM d, yyyy"));
        }
    }
}
Task: Date format


C++: 


#include <vector>
#include <string>
#include <iostream>
#include <ctime>


class Date
{
    struct tm ltime;

public:
    
    Date()
    {
        time_t t = time(0);
        localtime_r(&t, &ltime);
    }
    
    
    std::string getDate(const char* fmt)
    {
        char out[200];
        size_t result = strftime(out, sizeof out, fmt, &ltime);
        return std::string(out, out + result);
    }
    
    
    std::string getISODate() {return getDate("%F");}
    
    
    std::string getTextDate() {return getDate("%A, %B %d, %Y");}
};

int main()
{
    Date d;
    std::cout << d.getISODate() << std::endl;
    std::cout << d.getTextDate() << std::endl;
    return 0;
}
Task: Date format





Java: Datetime dtNow = datetime.now();
String strDt1 = dtNow.format('yyyy-MM-dd');
String strDt2 = dtNow.format('EEEE, MMMM dd, yyyy');
system.debug(strDt1); 
system.debug(strDt2);
Task: Date format







Python: import datetime
today = datetime.date.today()

today.isoformat()

today.strftime("%A, %B %d, %Y")


"The date is {0:%A, %B %d, %Y}".format(d)

"The date is {date:%A, %B %d, %Y}".format(date=d)

f"The date is {d:%A, %B %d, %Y}"