doeNET Framework 农历 ChineseLunisolarCalendar
C:\Program Files (x86)\MSBuild\14.0\Bin\csc.exe test.cs
# test.cs
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO; class Program
{
private static readonly ConsoleColor DefaultForegroundColor = Console.ForegroundColor;
private static readonly int DefaultPreSaleDays = ;
private static readonly string WebSite = "http://www.12306.cn"; private static int Main(params string[] args)
{
var preSaleDays = ;
if (args.Length > )
{
if (!int.TryParse(args[], out preSaleDays))
{
ShowHelpAndExit(); //第一个参数错误
}
else if (args.Length > )
{
DateTime argsDate; //出行日期
if (args[].Trim().Equals("now", StringComparison.OrdinalIgnoreCase))
{
argsDate = DateTime.Now.AddDays(preSaleDays - ); //出行日期为now的话,计算出今天可预订的出行日期
}
else if (!DateTime.TryParse(args[], out argsDate))
{
ShowHelpAndExit(); //第二个参数错误
}
Console.WriteLine($"\n * 火车票预售期为:{preSaleDays}天\n\n出行日期:{FormatDate(argsDate)}");
OutputBookingDay(preSaleDays, argsDate); //输出预售日期
var paused = true;
if (args.Length > )
{
if (args[].Trim().Equals("nopause", StringComparison.OrdinalIgnoreCase))
{
paused = false; //第三个参数为 nopause 则不暂停直接退出,用于配合命令提示符
}
}
if (paused)
{
Console.Write("\n按任意键退出...");
Console.ReadKey(true);
}
Environment.Exit(); //正常退出
}
}
else
{
Console.WriteLine("计算从哪天起应该购买预售火车票"); //未带参数,输出标题
Console.WriteLine();
}
if (preSaleDays <= )
{
Console.Write($"火车票预售期(缺省为{DefaultPreSaleDays}天):");
var input = Console.ReadLine()?.Trim(); //手动输入预售期
InputToExit(input);
if (!int.TryParse(input, out preSaleDays) || preSaleDays <= )
{
preSaleDays = DefaultPreSaleDays; //输入错误,预售期设置为缺省天数
ConsoleCursorRelativeLine(-); //光标移动到上一行行首。
var point = ConsoleCursorGetCurrentPosition();
if (point != null)
{
ConsoleCursorFillLines(point.Value.Top, point.Value.Top, ' '); //清除光标所在行的显示字符(用空格填充)
Console.Write($"火车票预售期(缺省为{DefaultPreSaleDays}天):{preSaleDays}\n");
}
}
}
Console.WriteLine($"\n * 火车票预售期为:{preSaleDays}天");
Console.WriteLine($" * 今天可以预定 {FormatDate(DateTime.Now.AddDays(preSaleDays - 1))} 的火车票");
Console.WriteLine(" * 输入 exit 可退出程序,输入 g 打开12306订票网站\n");
while (true)
{
Console.Write("出行日期:"); //输入出行日期
var input = Console.ReadLine()?.Trim();
InputToExit(input); //若输入exit则退出
if (input != null && input.Equals("g", StringComparison.OrdinalIgnoreCase))
{
Process.Start(WebSite); //输入g打开12306网站
Console.WriteLine($"正在打开 {WebSite}");
}
else
{
DateTime dest;
if (DateTime.TryParse(input, out dest))
{
ConsoleCursorRelativeLine(-);
var point = ConsoleCursorGetCurrentPosition();
if (point != null)
{
ConsoleCursorFillLines(point.Value.Top, point.Value.Top, ' '); //清除前一行显示字符
Console.WriteLine($"出行日期:{FormatDate(dest)}"); //更新输出日期
}
OutputBookingDay(preSaleDays, dest);
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("输入错误"); //显示输出错误
Console.ForegroundColor = DefaultForegroundColor;
ConsoleCursorRelativeLine(-); //光标向上移动3行
var point = ConsoleCursorGetCurrentPosition();
if (point != null)
{
ConsoleCursorFillLines(point.Value.Top, point.Value.Top + , ' ');
}
}
}
Console.WriteLine();
}
} static void InputToExit(string input)
{
if (input.Equals("exit", StringComparison.OrdinalIgnoreCase))
{
Environment.Exit(); //用户退出
}
} static void ShowHelp()
{
//帮助提示
var appname = Path.GetFileName(Environment.GetCommandLineArgs()[]);
Console.WriteLine(" * 参数格式:");
Console.WriteLine($" {appname} <火车票预售期>");
Console.WriteLine($" {appname} <火车票预售期> <出行日期> [nopause]");
Console.WriteLine($" {appname} <火车票预售期> now [nopause]");
Console.WriteLine($"\n * 例子:预售期{DefaultPreSaleDays}天,查看今天可以预定哪天火车票");
Console.WriteLine($" {appname} 60 now");
Console.WriteLine("\n * 批处理:");
Console.WriteLine($" {appname} 60 now nopause|Find \"出行日期:\"");
Console.WriteLine();
} static void ShowHelpAndExit(int exitcode)
{
ShowHelp(); //显示帮助提示然后按任意键退出
Console.Write("按任意键退出...");
Console.ReadKey(true);
Environment.Exit(exitcode); //返回错误码(errorlevel)
} static string FormatDate(DateTime date)
{
ConsoleColor? cc;
return FormatDate(date, out cc); //格式化日期
} private static readonly string[] DayOfWeek = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
private static readonly string[] DayOfToday = { "前天", "昨天", "今天", "明天", "后天" }; private static string FormatDate(DateTime date, out ConsoleColor? consoleColor)
{
var d = (date.Date - DateTime.Now.Date).Days;
var tip = d>=- && d<= ? $"({DayOfToday[d+2]})" : $"({DayOfWeek[(int) date.DayOfWeek]})";
consoleColor = d >= ? ConsoleColor.Green : ConsoleColor.Yellow;
var chinaCalendarString = ConvertToChineseLunisolarCalendar(date);
return $"{date:yyyy-M-d}{tip}{chinaCalendarString}";
} private static void OutputBookingDay(int days, DateTime destDate)
{
var date = destDate.AddDays(-days + );
ConsoleColor? cc;
var formattedDest = FormatDate(date, out cc);
Console.ForegroundColor = DefaultForegroundColor;
Console.Write("预售日期:"); //用缺省前景色显示“预售日期:”这几个字
if (cc.HasValue)
{
Console.ForegroundColor = cc.Value; //更换前景色
}
Console.WriteLine(formattedDest); //按格式输出预售日期
Console.ForegroundColor = DefaultForegroundColor; //还原缺省前景色
} private static void ConsoleCursorRelativeLine(int rows)
{
//将ConsoleCursor操作封装成方法,加入try语句,防止在非控制台下输出造成异常,下同
try
{
var t = Console.CursorTop + rows; //相对行号
Console.SetCursorPosition(, t); //移动光标到相对行
}
catch
{
// ignored
}
} private static void ConsoleCursorFillLines(int startLine, int endLine, char @char)
{
var d = endLine - startLine + ;
if (d > )
{
var consoleCursorGetCurrentPosition = ConsoleCursorGetCurrentPosition();
if (consoleCursorGetCurrentPosition != null)
{
var point = consoleCursorGetCurrentPosition.Value;
Console.SetCursorPosition(, startLine); //光标移动到起始行
Console.Write(new string(@char, Console.BufferWidth*d)); //用字符填满指定行数(d)
Console.SetCursorPosition(point.Left, point.Top); //返回光标原来的位置
}
}
} private static Point? ConsoleCursorGetCurrentPosition()
{
try
{
return new Point(Console.CursorLeft, Console.CursorTop); //返回光标位置
}
catch
{
// ignored
}
return null; //失败
} private struct Point
{
public readonly int Left;
public readonly int Top; public Point(int left, int top)
{
Left = left;
Top = top;
}
} private static readonly ChineseLunisolarCalendar ChinaCalendar = new ChineseLunisolarCalendar();
private static readonly string[] M1 = { "正月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "腊月" };
private static readonly string[] D1 = { "初", "十", "廿" };
private static readonly string[] D2 = { "一", "二", "三", "四", "五", "六", "七", "八", "九" };
private static readonly string[] D3 = { "初十", "二十", "三十" }; private static string CcDay(int d)
{
var dd = d%;
return dd == ? $"{D3[d/10-1]}" : $"{D1[d/10]}{D2[dd-1]}";
} private static string CcMonth(int y, int m)
{
var leapMonth = ; if (ChinaCalendar.IsLeapYear(y))
{
for (int i = ; i <= m; i++)
{
if (ChinaCalendar.IsLeapMonth(y, i))
{
leapMonth = i;
break;
}
} if (m == leapMonth)
{
return $"闰{M1[m - 2]}";
} if (leapMonth > )
{
return $"{M1[m - 2]}";
}
} return $"{M1[m - 1]}";
} private static string ConvertToChineseLunisolarCalendar(DateTime date)
{
if (date > ChinaCalendar.MaxSupportedDateTime || date < ChinaCalendar.MinSupportedDateTime)
{
return "";
}
var y = ChinaCalendar.GetYear(date);
var m = ChinaCalendar.GetMonth(date); // 1~13
var d = ChinaCalendar.GetDayOfMonth(date); // 1~31
return $"{CcMonth(y, m)}{CcDay(d)}";
}
}
doeNET Framework 农历 ChineseLunisolarCalendar的更多相关文章
- windows类书的学习心得
原文网址:http://www.blogjava.net/sound/archive/2008/08/21/40499.html 现在的计算机图书发展的可真快,很久没去书店,昨日去了一下,真是感叹万千 ...
- ChineseLunisolarCalendar 农历日期
#region 农历日期 static ChineseLunisolarCalendar cCalendar = new ChineseLunisolarCalendar(); /// <sum ...
- C# 阳历转农历
你妹的sb 原文 C#(ASP.NET)公历转农历的简单方法 Dot Net 平台,对全球化的支持做的非常好,不得不称赞一个 通常,将公历转为农历,是个非常烦的事情,需要整理闰年.闰月等的对照表. 在 ...
- 使用ChineseLunisolarCalendar 对象由年份获得生肖名,Datetime.now.tostring获得星期几
一:使用ChineseLunisolarCalendar 对象由年份获得生肖名,截图 二:代码 using System; using System.Collections.Generic; usin ...
- ASP.NET农历时间显示(两)
在拍摄的月球时前(http://blog.csdn.net/yysyangyangyangshan/article/details/6802950),只是没有进行封装使用起来须要手动改动. 本次进行简 ...
- ASP.NET如何显示农历时间
ASP.NET如何显示农历时间 CS部分代码如下: 代码如下: public string ChineseTimeNow = ""; public string ForignTi ...
- 由.Net类库提供的农历计算
由.Net类库提供的农历计算(C#农历) 2007-11-21 12:47:00 标签:.Net 类库 农历计算 C#农历 休闲 ...
- c# 公元转农历
void Main() { ,,); GetLunarDisplay(date).Dump(); } public List<string> GetLunarYearList() { va ...
- C#项目获取当前时间的农历时间
https://blog.csdn.net/cstester/article/details/7407044 using System.Globalization; class CnCanlendar ...
随机推荐
- JDBC 入门
1. JDBC 简介 JDBC (Java DataBase Connectivity) 就是 Java 数据库连接, 说白了就是用 Java 语言向 数据库发送 SQL 语句. JDBC 其实是访问 ...
- Python 网络编程——socket
一 客户端/服务器架构 客户端(Client)服务器(Server)架构,即C/S架构,包括 1.硬件C/S架构(打印机) 2.软件C/S架构(web服务) 理想/目标状态—— 最常用的软件服务器是 ...
- 我的Android进阶之旅------>Android百度地图定位SDK功能学习
因为项目需求,需要使用百度地图的定位功能,因此去百度地图开发平台下载了百度地图的Android定位SDK最新版本的开发包和示例代码学习. Android 定位SDK地址:http://develope ...
- SQL Server误删表查看
SQL Server误删表查看 转自:http://blog.51cto.com/aimax/2134572 SQL Server 完全恢复模式 下恢复误删除的表,进行 精准 恢复 1. 找出被 ...
- ABAP锁,数据库锁
原文出自 江正军 技术博客,博客链接:www.cnblogs.com/jiangzhengjun ABAP数据锁定 SM12锁查看与维护 通用加锁与解锁函数 ABAP程序锁定 数据库锁 锁的分类和兼容 ...
- 我的第四个Python小程序
简单的登陆程序: # Author: fansik # Description: Simply log in to small programs # Date: 2017/9/29 _username ...
- mysql数据库补充知识1 安装数据库破解数据库密码已经创建用户
一.安装MYSQL数据库 1.yum安装 #二进制rpm包安装 yum -y install mysql-server mysql 2.源码安装 1.解压tar包 cd /software tar ...
- 剑指offer 面试64题
题目:64题 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 解法一:利用Python特性 # -*- ...
- Android:日常学习笔记(9)———探究广播机制
Android:日常学习笔记(9)———探究广播机制 引入广播机制 Andorid广播机制 广播是任何应用均可接收的消息.系统将针对系统事件(例如:系统启动或设备开始充电时)传递各种广播.通过将 In ...
- 04 Spring框架 依赖注入(一)
整理了一下之前学习spring框架时候的一点笔记.如有错误欢迎指正,不喜勿喷. 上一节我们讲了几个bean的一些属性,用来限制我们实例创建过后的状态. 但是细心的我们会发现其实上面demo创建的实例并 ...